My custom defined protocol (phpfile) lets me open php-files in an editor through the browser. The only problem is, it gives the full url, that is different in each browser, i've seen:
phpfile:/[file]
phpfile:/[file]/
phpfile://[file]/
phpfile://[file]
All these need to be converted to just [file]. The problem is that I get a syntax error. What is the correct syntax?
set var=%1
if("%var:~0,9%"=="phpfile:/")
{
set url = %var:~9%
}
else
{
set url = %var:~10%
}
if(%var:-1,1% == "/")
{
url = %url:~0,-1%
}
START "" "C:\Program Files (x86)\NuSphere\PhpED\7.0\phped.exe" url
=== Edit ===
I now have the following, but it is adding "" at the end of the url
SETLOCAL enabledelayedexpansion
set var=%1
if %var:~0,9%=="phpfile:/" (
set url = %var:~9%
) else (
set url = %var:~10%
)
if "%var:~-1%"=="\" (
set url = %url:~0,-1%"
)
if "%var:~-1%"=="/" (
set url = %url:~0,-1%"
)
START "" "C:\Program Files (x86)\NuSphere\PhpED\7.0\phped.exe" %url:"=%
That's because you wrote some horrible bastard between batch files and C but certainly no language that is in use anywhere.
Try the following:
setlocal enabledelayedexpansion
set "var=%~1"
if "!var:~0,9!=="phpfile:/" (
set "url=!var:~9!"
) else (
set "url=!var:~10!"
)
if "!var:-1,1!"=="/" (
set "url=!url:~0,-1!"
)
START "" "C:\Program Files (x86)\NuSphere\PhpED\7.0\phped.exe" "!url!"
Delayed expansion was used primarily to deal better with some characters that cause trouble in batch files. Generally though, I wonder how you would ever have expected your code to work.
There were quite a few problems in your code, this should fix things:
set var="%1"
if %var:~0,9%=="phpfile:/" (
set url="%var:~9%"
) else (
set url="%var:~10%"
)
if "%var:-1,1%"=="/" (
set url="%url:~0,-1%"
)
START "" "C:\Program Files (x86)\NuSphere\PhpED\7.0\phped.exe" url
You can check the syntax of batch file commands using help <command>. IF statements don't use curly braces, or require round brackets around the conditional statement.
Related
So I map a drive to T \server01\tfolder
I need a script which does the following
If T is mapped to \server01\tfolder then map it to server02\tolder. But if drive is mapped to \server02\tfolder then map it to \server01\tfolder
I am using an else switch but already but can't for the life of me work out how I get it to confirm which its being mapped to already and then map it to the alternate.
Any ideas?
for /f "tokens=2" %%a in ('net use t: ^|find "\\"') do set "current=%%a"
if "%current%" == "\\server01\tfolder" (
set "new=\\server02\tfolder"
) else (
set "new=\\server01\tfolder"
)
echo switching from %current% to %new%
I want to validate these two custom attributes using any iteration method.
:: Validate Custom Attribute
set HPSUT_CA=""#HPSUT_Windows#
set SUT_CA=""#SUT_Windows#
if any CA Value is Empty it should exit the code, If any CA value contains it should goto :START_INSTALL
:START_INSTALL
:: windows map network drive
set HPSUTx64=Z:\Media\HPSUT\#HPSUT_Windows#
set SUTx64=Z:\Media\SUT\#SUT_Windows#
and then it should check for
:: Check for Media Server
if exist %HPSUTx64% (
echo HPSUT %HPSUTx64% exists in Media Server.
goto :INSTALLHPSUT
) else if exist %SUTx64%(
echo SUT %SUTx64% exists in Media Server.
goto :INSTALLSUT
) else (
echo SUT SUTx64 not found. Please Check for SUT files in Media Server.
exit 1
)
Help on this how to Handle this case
:: Check for Media Server
if exist %HPSUTx64% (
echo HPSUT %HPSUTx64% exists in Media Server.
goto :INSTALLHPSUT
) else if exist %SUTx64%(
echo SUT %SUTx64% exists in Media Server.
goto :INSTALLSUT
) else (
echo SUT SUTx64 not found. Please Check for SUT files in Media Server.
exit 1
)
I want to replace OLD with %NEW% in test.txt by using a batch file:
#echo off
set /p NEW=""
set InputFile=test.txt
find OLD
replace %NEW%
my test.txt contains various characters like ~ : ; _ .which I attribute to various solutions clapping out.
I have attempted to use BatchSubstitude.bat to achieve this but it does not "find" OLD and does not replace any text.
I have also attempted to use fart.exe but I receieve an error when trying to perform this:
find_string="OLD"
replace_string="test"
actual find_length=3
actual replace_length=4
processing \filepath\,test.txt
skipping binary file: \filepath\test.txt
The documentation for this is sparse and nothing I could google furiously would provide a solution.
The reason I am using batch for the moment is that I perform various windows CE actions such as cecopy, pdel and rapistart for windows CE deployment over activesync and batch seems to be the easiest way to to get the result that I am after - and I'm a total noob at batch.
Does anyone know how this can be achieved with batch and perhaps explain how the various functions work to achieve this?
Cheers!
Try this:
#set #a=0 /*
#echo off
set /p NEW=""
set InputFile=test.txt
cscript //nologo //E:JScript "%~F0" "%NEW%" < "%InputFile%" > output.tmp
move /Y output.tmp "%InputFile%"
#goto :EOF */
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/OLD/g,WScript.Arguments(0)));
Do not download anything from sourceforge so better to skip fart-it.
The method in the dostips is not so robust when special characters are used (&<>|^) as it is pointed there.Here's a simple batch/jscript that will replace a content in a file (you can change the hardcoded things like file location):
0</* :
#echo off
set "file=c:\test.txt"
set "find=OLD"
set /p "replace=NEW: "
cscript /nologo /E:jscript "%~f0" %file% %find% %replace%
echo ==replaced==
exit /b */0;
var FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
if (ARGS.Length < 3 ) {
WScript.Echo("Wrong arguments");
WScript.Quit(1);
}
var filename=ARGS.Item(0);
var find=ARGS.Item(1);
var replace=ARGS.Item(2);
var readStream=FSOObj.OpenTextFile(filename, 1);
var content=readStream.ReadAll();
readStream.Close();
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
var newConten=replaceAll(find,replace,content);
var writeStream=FSOObj.OpenTextFile(filename, 2);
writeStream.WriteLine(newConten);
writeStream.Close();
Check also this more advanced tools
FindRepl
JRepl
I am having some troubles and I hope someone can help. I have a game server that I am running on a dedicated server. I have a batch file that is currently running the server. The server needs to be restarted 4 hours after the server starts. What I am struggling with is the statements to get the batch file to run my restart batch file in 4 hours? If the batch file says run my restart batch in 4 hours my life will be saved.
here is my current batch file.
#echo off
if "%configdone%"=="1" (
GOTO :EOF
)
set configdone=0
set skbt_debug=2
set keepalive_database=1
set keepalive_bec=0
set keepalive_asm=0
set keepalive_ts=0
set keepalive_hc=0
set serverport=2302
set bindtoip=0
set serverip=118.217.115.72
set bec_flag_dsc=1
set teamspeak_port=2310
set asm_log_interval=5
set serverStartTimeout=10
set db_backup_interval=60
set use_zip_logs=1
set use_zip_backups=1
set databasebackupfolder="E:\Overpoch Server 1\Backup"
set logfilebackupfolder="C:\apps\epoch_log_backups"
set manual_timeout_length=2
set auto_timeout_length=10
set auto_restart_delay=5
set cleanWerDialogs=1
set hclaunchparams=-connect=1 -ip=199.217.115.72 -port=2312 -client -nosound -mod=#Epoch;#EpochHive;#MAS;
set armaserverexe=arma3server.exe
set hcexename=arma3serverHC.exe
set teamspeakfilename=ts3server_win64.exe
set redisexename=redis-server.exe
set becexename=bec.exe
set asmexename=ArmaServerMonitor.exe
set databasefile_name=dump.rdb
set asm_log_file=asm_performance.log
set armapath="C:\servers\epochServer"
set hcarmapath="C:\servers\epochServer"
set teamspeakpath="C:\apps\teamspeak"
set redispath="C:\servers\epochServer\DB"
set asmpath="C:\servers\epochServer"
set Battleyepath="C:\servers\epochServer\SC\BattlEye"
set LogPath="C:\servers\epochServer\SC"
set becpath="C:\servers\epochServer\BEC"
set databasefile="C:\servers\epochServer\DB\dump.rdb"
set servercfgpath="C:\servers\epochServer\SC\config.cfg"
set serverbasicpath="C:\servers\epochServer\SC\basic.cfg"
set profilepathname="SC"
set cli_username="SC"
if %bindtoip%==1 (
set ip_param= -ip=%serverip%
) else (
set ip_param=
)
set mod_string=-mod=#Epoch;#EpochHive;#MAS;
set servercommandline=%armaserverexe% "%mod_string:"=%" "-config=%servercfgpath:"=%" %ip_param% -port=%serverport% "-profiles=%profilepathname:"=%" "-cfg=%serverbasicpath:"=%" "-name=%cli_username:"=%" -autoinit
set serverAffinity=0,1,2,3,4,5,6,7
set becAffinity=0,1,2,3,4,5,6,7
set hcAffinity=0,1,2,3,4,5,6,7
set redisAffinity=2,3,4,5,6,7,0,1
set teamspeakAffinity=0,1,2,3,4,5,6,7
set asmAffinity=0,1,2,3,4,5,6,7
set serverPriority=normal
set becPriority=normal
set hcPriority=normal
set redisPriority=normal
set teamspeakPriority=normal
set asmPriority=normal
This is my restart batch
#echo off
call "C:\servers\EPOCHS~2\batch\ULTSGS~1.CMD"
cd /D %armapath%
if %skbt_debug% GTR 2 (
call :FUNC NOVAR BatchLogWrite 3__AUTO_RESTART__EVENT__INITIALIZE====================================
)
call batch_lib\lib\setauto.bat
call batch_lib\lib\stop_all.bat
goto :EOF
:FUNC
set currentDir=%CD%
cd "%armapath%/batch_lib/gbl_func"
rem %1 = return var, %2 = function, %3 = args
set returnvarname=%1
set funcname=%2
set argString=%3
set argString=%argString:__= %
set argString=%argString:"=%
set argString=%argString:(=[%
set argString=%argString:)=]%
set args=%argString%
if "%argString%"=="__=" set args=
if "%argString%"=="" (
set args=
)
set filename=%funcname%.cmd
set val1=
for /f %%I in ('%filename% "%args%"') do (
set "val1=%%I"
)
set "%1=%val1%"
cd %currentDir%
goto :EOF
Thanks for your help!
Jayson
What's wrong with just - waits 4 hours then reboots.
timeout /t 14400
shutdown /r /f /t 0
I have a requirement where i need to install a application using a batch file.
when i run this batch file it asks for inputs and that batch file internally calls a powershell script which ask the inputs, if i hit a enter key for some inputs it takes the default values.
Can any one let me know how to store all inputs in a file and pass it to a batch file and it should also support the same for powershell script as well as the default values.
BATCHFILE.cmd
#echo off
setlocal EnableDelayedExpansion
set scriptsPath=%~dp0
set scriptsPathShort=%~dps0
set psPath=%windir%\System32\WindowsPowerShell\v1.0\powershell.exe
:params
set Path=%~1
set psVersion=0
if "!psVersion!" == "2.0" "!psPath!" -version !psVersion! -command "!scriptsPathShort!install\install-driver.ps1" '!Path!' '****'
if not !errorlevel! == 0 goto error
goto end
install-driver.ps1
param
(
[string] $scriptsBasePath = $(throw "Missing parameter: scriptsBasePath"),
[string] $Path = $(throw "Missing parameter: packagePath"),
[string] $packageRootFolder = $(throw "Missing parameter: packageRootFolder")
)
$configFileName = "install.config"
**install.config**
<Param name="ClientDir" required="1"
label="Client Directory"
desc="Choose a unique name which will be used by the application to create folder in which client files will be stored" />
<Param mode="adv" name="lPswdEncryptionMode" required="1" defaultValue="2"
label="Password Encryption Mode" />
<Param componentType="webserver" name="WebsiteName" required="1" defaultValue="Default Web Site"
label="Website Name"
desc="Enter site name under which the website component will be installed" cloud="1" />
This is only piece of code just to have a idea .
it calls a config file where i have few parameters.
Can any one please let me know how to pass the variables ?
Here is how I gather variables from my config/ini files into powershell.
$SETTINGS = Get-Content .\install.config
$scriptsBasePath = $SETTINGS[0]
$Path = $SETTINGS[1]
$packageRootFolder = $SETTINGS[2]
If install.config is not in the same directory, substitute the full path.