I have the following code which doesn't seem to be working properly - is someone able to assist with how to run command-lines in batch files
#echo off
set changeFrom=321
set changeTo=123
set origFile=config.txt
set newFile=config1.txt
test.bat %changeFrom% %changeTo% %origFile%>%newFile%
del %origFile%
ren %newFile% %origFile%
::end
I have a file "test.bat" which has code to replace strings in a file - but I Don't get how it can work ?
You need to use call to execute the second bat file from the first like this:
call test.bat %changeFrom% %changeTo% %origFile%>%newFile%
without call the first batch script will exit when the second one exits.
Related
Essentially I have two .bat scripts that work just fine individually, however I wanted to combine the two scripts into one action as i have it assigned to a hotkey and want all of this with one press of a key, not two but am having problems because the scripts are in different directories.
Below are my two scripts:
BLAH-Script.bat
RE4_UHD_BLAH_Tool.exe -p
DEL /F /S /Q /A "F:\r101\r101_09.AEV"
and the second script:
UDAS_TOOL.bat:
DEL /F /S /Q /A "F:\st1\rl01.udas"
ping -n 1 localhost
DEL /F /S /Q /A "F:\UDAS Tool\r101.udas"
UDAS_Tool.exe -p
Now, both of these work great on their own because each of the scripts calls on an .exe that is in the same DIR as the script that calls them, but trying to get them into one .bat file has not worked using the CALL function because one of the scripts exists in an outside directory.
I have tried to make a new .bat script that would execute both bat scripts in one:
call "F:\pack\BLAH-Script.bat"
ping -n 1 localhost
call "F:\pack\UDAS\UDAS_TOOL.bat"
However this does not work.The files are not packaged by the exe tools as intended.
To try and figure out the problem I added pause command between scripts:
call "F:\pack\BLAH-Script.bat"
pause
call "F:\pack\UDAS_TOOL.bat"
This command allowed me to read the output before the window was closed and yielded the culprit. In the first script I am getting the following error:
F:\UD - r101-->RE4_UHD_BLAH_Tool.exe -p
'RE4_UHD_BLAH_Tool.exe' is not recognized as an internal or external command,
operable program or batch file.
I believe what is happening it that each file works fine if run in its own directory because they are in the same DIR as the .exe they are calling, but the first .bat file will not execute because the third bat file ( the one that is that is calling the first & second) is not in the same DIR as the first.
I have tried:
CD /D "F:\pack"
call "F:\pack\BLAH-Script.bat"
pause
CD /D "F:\pack"
call "F:\pack\UDAS\UDAS_TOOL.bat"
with the same error.
How do I get the directories sorted out ?
In theory, you could simply fully-qualify the executable path, or make sure that the executables are located on the path.
However, some programs assume that the executable is in the current directory, and expect that configuration files, etc. are similarly in that directory, so what I'd do is
in each batch, after the initialisation ceremony (#echo off/setlocal...) add a
pushd "actualexcutablelocation"
and ensure that on exit from the batch,
popd
is executed to restore to the original directory.
pushd switches current directory to the nominated directory until a popd is executed to return to the original.
I would like to have a run.bat file:
C:\> run functions
will run \exec\functions.bat
C:\> run patches
will run \exec\patches.bat
C:\> run
will run \exec\patches.bat and \exec\functions.bat (yes, in a predefined order)
How can I do so? Call batch command doesn't seem to be working
Thanks in advance :-)
call will work just fine:
rem if no parameter is specified run everything
if "%1"=="" goto :run_all
rem only run the batch file that was specified
call "execs\%1.bat"
goto :eof
:run_all
call "execs\patches.bat"
call "execs\functions.bat"
a_horse_with_no_name did the hard work - I just wrapped it a different way.
#echo off
rem if no parameter is specified run everything
if "%~1"=="" (
call "\execs\patches.bat"
call "\execs\functions.bat"
) else (
call "\execs\%1.bat"
)
I am having trouble with a batch file. I have 2 files the first batch file runs and it creates the directory and copies the files needed. It appears to call the second batch file correctly because it opens the instructions.txt but then it stops. I will be running the first batch file from a CD and then the dbinstall.bat from the C:\testing folder.
this is my setup.bat
#echo off
md "C:\testing"
xcopy *.* C:\testing
CALL "C:\testing\dbinstall.bat"
Which in turns should call and run this
REM ***PLEASE REPLACE %DWVerFileName.exe WITH THE PROPER VERSION OF THE EXE FILE***
REM ***MAKE SURE THE 7z FILE INCLUDES THE CUSTOMER NAME AND THEN CHANGE %filename%.7z TO THE FILE NAME***
CALL "C:\testing\Instructions.rtf"
start /b /wait "C:\testing\7z423.exe"
SET AppExePath="%ProgramFiles(x86)%\7-zip\7z.exe"
IF NOT EXIST %AppExePath% SET AppExePath="%ProgramFiles%\7-zip\7z.exe"
%AppExePath% e database.7z
start /b /wait "setup.exe"
SQLCMD -E -S touch -Q "RESTORE DATABASE testing FROM DISK='C:\testing\database.bak'"
I am stuck and any help would be appreciated. Thanks
Since we have no idea of what "it stops" means, or where "it stops", I'd guess
CALL "C:\testing\Instructions.rtf"
should be
start "instructions" "C:\testing\Instructions.rtf"
which would then invoke whatever program is associated with .rtf, no doubt dislaying the instructions and keeping the displaying mechanism open while the 7z423 executable runs.
My batch file is throwing error when run, but when I run the commands one by one manually in command line then I get no error. Here's the batch file 'test.bat'.
echo "test"
cd "c:\packages"
pause
for /R c:\packages %F in (*.msi) do set /A servername=%~nxF
pause
echo %servername%
pause
I get this error - "~nxF was unexpcted at this time".
I got the sample code from https://stackoverflow.com/a/1100466/1105556
I'm just trying the get the file name in c:\packages & store the value in 'servername' variable. There is only one file (.msi) in the folder
I cant figure out whats wrong. Can someone please solve the riddle for me?
You need to double the %% in batch files:
for /R c:\packages %%F in (*.msi) do set /A servername=%%~nxF
At some point in my script, I'd like the bat script to delete itself. This requires that the script know its name and then to use that name to delete itself. Is this possible?
None of the existing answers provide a clean way for a batch file to delete itself and exit without any visible error message.
Simply including del "%~f0" does delete the script, but it also results in an ugly "The batch file cannot be found." error message.
If it is OK for the script to close the parent CMD process (the console window), then the following works fine without any error message:
del "%~f0"&exit
But it is a bit more complicated if you want the script to delete itself and exit without closing the parent CMD process, and without any error message.
Method 1: Start a second process that runs within the same console window that actually performs the delete. The EXIT /B is probably not necessary, but I put it in to maximize the probability that the batch script will close before the STARTed process attempts to delete the file.
start /b "" cmd /c del "%~f0"&exit /b
Method 2: Create and transfer control to another temp batch file that deletes itself as well as the caller, but don't use CALL, and redirect stderr to nul.
>"%~f0.bat" echo del "%~f0" "%~f0.bat"
2>nul "%~f0.bat"
If I'm not mistaken, %0 will be the path used to call the batch file.
Tested and working:
del %0
exit
%0 gives you the relative path the from the directory where the bat file was started. So if you call it
mybats\delyourself.bat tango roger
%0 will contain mybats\delyourself.bat
del %0 works if you haven't changed your current directory.
%~f0 exapnds to the full path to the file.
For me it was important, that I have no errorLevel 1 after I exited the batch file. I found an suitable and easy answer this way:
DeleteMyself.cmd:
START CMD /C DEL DeleteMyself.cmd
Test batch if DeleteMyself.cmd is returning errorLevel 0. TestDeleteMyself.cmd:
call DeleteMyself.cmd
echo Errorlevel: %errorLevel%