I need to make a Windows batch file that will:
1.- Check a directory name and "If" it exists,
a.- Run a specified *.exe from a different directory.
2.- Or "Else"
a.- Rename a directory and also
b.- Rename another directory - then
c.- Run a *.exe from the created directory.
Question is: I'm stuck on the correct syntax ( I suppose) on creating this batch file. This is what I have, (maybe a nested if/Else would be better?) someone please enlighten me... Thanks.
#echo off
IF EXIST "C:\Test\Dir1" (
START "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "C:\Test\Dir1"
ren "C:\Test\Dir2" "C:\Test\Dir"
START "C:\Test\Dir\Test.exe"
)
You can't ren with a target name including a path or drive.
try
ren "C:\Test\Dir2" "Dir"
start has odd syntax - 'though it seems to not be the problem in this case. The first "quoted argument" string becomes the window title and can be routinely disregarded by the executable. Try
routinely using
start "window title that can be empty if you like" "executablename" argument list
Even if this arguably doesn't comply exactly with the documented behaviour.
(and simplifying problems to a generality can obscure the real cause, too)
Thanks dbenham and Magoo. Your responses made a world of difference on something that seems quite simple. It is now working as all the changes you both mentioned help. The final "nail in the coffin" was understanding the syntax for the START parameters. Understanding that the first parameter has double quotes and it uses that as the optional TITLE for the new window, ssoooo giving it an empty double quote ( "" ) gives it an empty title before the name of the program to fake it out. And Voila...
Thank you guys sssooo much. Final Working batch file.
#echo off
IF EXIST "C:\Test\Dir1" (
START "" "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "Dir1"
ren "C:\Test\Dir2" "Dir"
START "" "C:\Test\Dir\Test.exe"
)
Related
I have ZERO bat knowledge so thought I would ask here, if I may.
I have an image C:\Users\Dane\Pictures\Doom.jpg. I wish for this image to be copied and for it to be renamed to the exact name of 379 non-image type files which are in another folder which is G:\Doom. So I will have the same image 379 times but named to match the 379 files.
Would anyone be kind enough to write a bat file to do that? Thank you in advance.
This site is not a free code writing service; Rather give the subject an attempt and we will be happy to assist you. However, because I'm nice, I have a response for you.
This problem is pretty common and can be solved very easily using a FOR statement. In this example we will be searching a directory for every item stored inside. Each item will be added to the integer %%A. For more information do FOR /? inside a command window.
for %%a in ("Directory") DO (Action)
For copying files, we will use the copy command. Please keep note that we will be using parameter extensions to expand the %%A to have no extension using %%~na. More info here: Parameter Extensions
This script will copy & rename Doom.jpg to G:\Doom for each item in the directory.
Batch File:
for %%a in ("G:\Doom\*") do (copy "C:\Users\Dane\Pictures\Doom.jpg" "G:\Doom\%%~na.png")
Command Prompt:
for %a in ("G:\Doom\*") do (copy "C:\Users\Dane\Pictures\Doom.jpg" "G:\Doom\%~na.png")
I have 2 projects for which I am trying to create a generic Post-Build event batch file.
Here is the command in Visual Studio:
Post-Build event
if $(ConfigurationName) == Release ("$(ProjectDir)PostBuildRelease.bat" "$(TargetDir)" #(VersionNumber) "$(TargetFileName)" "$(TargetName)")
So I am calling the file PostBuildRelease.bat with 4 parameters:
Bin\Release Directory
Project Version
File Name With Extension
File Name Without Extension
Project 1
This works perfectly with this batch script:
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET "productionpath=Z:\Unused\Apps\LyncVdiChecker\"
MOVE %productionpath%%3 %productionpath%"_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 %productionpath%
Where the assembly is copied to Z:\Unused\Apps\LyncVdiChecker\ and the existing version copied to _archive in the same folder. The archived version also has the date and version number replace the file extension.
Project 2
This batch script also works perfectly (it does the same thing but in a different folder and for a different project):
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET "productionpath=Z:\Unused\Apps\IT Support App\"
MOVE "Z:\Unused\Apps\IT Support App\"%3 "Z:\Unused\Apps\IT Support App\_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 "Z:\Unused\Apps\IT Support App"
However, if I try using the same script from Project1 (the more generic version) in Project2, I get errors, even though the 2 scripts are equivalent:
Errors
The command "if Release == Release ("C:\Users\Seb.Kotze\Source\Repos\Applications\ITSelfHelp\ITHelp\PostBuildRelease.bat" "C:\Users\Seb.Kotze\Source\Repos\Applications\ITSelfHelp\ITHelp\bin\Release\" 2.0.6100.20905 "IT Self Help.exe" "IT Self Help")" exited with code 4.
Output Window:
The syntax of the command is incorrect.
Invalid number of parameters
This error is rather unhelpful, so I tried commenting out the 2 lines MOVE and XCOPY and build again:
Removed MOVE
Same error as above.
Output window:
Invalid number of parameters
Remove XCOPY
No Visual Studio Error, but this appears in the output window:
The syntax of the command is incorrect.
Parameter Output
When I echo out the parameters being used in Project2, everything seems to be in order:
"Path\to\Bin\Release"
2.0.6100.21082
"IT Self Help.exe"
"IT Self Help"
Z:\Unused\Apps\IT Support App\
How can I debug this issue? How is it possible that my script runs fine without any issues, but when run against a different project none of the commands are recognised? Any help with this is much appreciated!
You should normalize all your arguments, so they don't contain outer quotes.
Then you can use them in a reliable way.
The syntax set "variable=%~1" avoids outer quotes in the variable itself.
set "TargetDir=%~1"
set "VersionNumber=%~2"
set "TargetFileName=%~3"
set "TargetName=%~4"
SET "productionpath=Z:\IT Support App\"
set "dateStamp=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"
MOVE "Z:\IT App\%TargetFileName%" "Z:\IT App\_archive\%TargetName%.%dateStamp%-%VersionNumber%"
XCOPY "%TargetFileName%" "Z:\IT App"
The problem is that the script is messing with the double quotes resulting in invalid paths and invalid number of arguments passed. When dealing with paths built dynamically, it's best to strip any existing " from the parts, and after the path is complete, surround it in ".
Dealing with batch arguments is explained on MSDN. Same thing for variables can be found on SS64.
I've played a bit with the file, and I was able to run it (from command line). The changes you should make in your (Project1) file:
SET productionpath="Z:\Unused\Apps\LyncVdiChecker\"
MOVE "%productionpath:"=%%~3" "%productionpath:"=%_archive\%~4.%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%-%~2"
XCOPY "%~3" "%productionpath:"=%"
I moved the " from the productionpath line to the beginning of its contents. That way will work with paths that contain SPACE s.
In the MOVE and XCOPY lines, I did what I explained above: even if the syntax is not that clear, it's more robust (the last "%productionpath:"=%" could be simple written as %productionpath%, but I left it in the the 1st form for consistency).
Note: You could remove the CMD command at the beginning of your batch, since it starts a new cmd instance(process) that doesn't end.
I found a solution to this, but I am still not sure what the cause was.
I suspect it has something to do with either one of:
Spaces in productionpath causing the command parameter declaration to escape
Quotes around one or more of the parameters creating a non-existent file path
After trying out a few changes to the script, I found that changing the productionpath declaration to SET productionpath="Z:\Unused\Apps\IT Support App\" solved the issue:
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET productionpath="Z:\Unused\Apps\IT Support App\"
MOVE "Z:\Unused\Apps\IT Support App\"%3 "Z:\Unused\Apps\IT Support App\_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 "Z:\Unused\Apps\IT Support App"
Making the same change to the Project1 script did not cause that to break either, so this seems safe.
Update
After reading some of the other answers, I amended the script once again to the following:
CMD
SET "TargetDir=%~1"
SET "VersionNumber=%~2"
SET "TargetFileName=%~3"
SET "TargetName=%~4"
SET "ProductionPath=Z:\Unused\Apps\IT Support App\"
SET "ArchivePath=%ProductionPath%_archive\"
SET "DateStamp=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"
MOVE "%ProductionPath%%TargetFileName%" "%ArchivePath%%TargetName%.%DateStamp%-%VersionNumber%"
XCOPY "%TargetFileName%" "%ProductionPath%"
Notice the "normalisation" of the paramaters - this removes all quotation marks from their values.
Also now using named parameters.
I am writing a batch script which will copy a file from a folder into the C:\ drive:
#ECHO ON
COPY C:\RANDOMFILES\Weekly Reprort_Hew*.xls C:\Weekly Reprort_Hew???????????.xls
The filename in the RANDOMFILES folder is: Weekly Reprort_Hew, 6-29-2014 10-30-00 PM-642.xls (The date and time and the number at the end will always change so I used the * in the filename being copied in the script)
When I run the batch script, I get the following message:
c:\RANDOMFILES>COPY C:\RANDOMFILES\Weekly Reprort_Hewlett*.xls C:\Weekly Reprort
_Hewlett???????????.xls
The system cannot find the file specified.
How can I fix the issue?
You need double quotes to handle spaces etc. Double check the spelling too.
#ECHO ON
COPY "C:\RANDOMFILES\Weekly Reprort_Hew*.xls" "C:\Weekly Reprort_Hew???????????.xls"
Don't know why it is not working - is it hidden? Maybe the spaces in the name?
The following will work though:
FOR %%I in (C:\RANDOMFILES\Weekly Reprort_Hew*.xls) DO COPY "%%I" C:\
The destination file name isn't necessary; if not otherwise specified, it will remain unchanged provided the destination is different. That may be why the plain COPY command isn't working.
Also:
"It is an not-so-well-known fact that the question mark wildcard will match exactly one character only when the wildcard does not appear at the end of a file name. " from http://www.thefriendlycoder.com/2011/11/24/batch-file-gotcha-question-mark-wildcard/
I am trying to run following code through cmd.
"C:\Program Files\Beyond Compare 2\BC2.exe" #"C:\New Folder\Myscript.txt" "C:\New Folder\A.txt" "C:\New Folder\B.txt"
This will actually open Beyond Compare and compare two text files.
The problem is ,when i run this code on cmd[Version 6.1.7601] it runs correctly but when i run it on version 5.1.2600 , it shows a fatal error :- Could not find C:/New .
I understand the error is due to space in the name(New Folder) , but why is it running fine on Win 7 .Does two versions of cmd have some difference in the way they accept arguments ?
Content of Myscript.txt :-
file-report layout:side-by-side &
options:display-all &
output-to:%3 output-options:html-color,wrap-word %1 %2
I can't explain why it is not working, but I have some potential solutions
1) Run with the current directory at the location of the files
Since the space is in the folder name, and all files are in the same location, you can avoid the folder name by simply changing directory to that folder and using a relative path.
pushd "c:\new folder"
"C:\Program Files\Beyond Compare 2\BC2.exe" #Myscript.txt A.txt B.txt
Of course this will not work if your files are in different locations, or if the file names have spaces (assuming spaces are really the problem)
2) Use the short 8.3 names
I hate the short 8.3 names because of the many bugs associated with them. But sometimes they can be useful.
You can get the short name of a file or folder by using DIR /X. Or you could use the following in a batch script to programmatically get the short paths.
for %%A in ("C:\New Folder\Myscript.txt") do (
for %%B in ("C:\New Folder\A.txt") do (
for %%C in ("C:\New Folder\B.txt") do (
"C:\Program Files\Beyond Compare 2\BC2.exe" #"%%~fsA" "%%~fsB" "%%~fsC"
)
)
)
Of course the above will not do any good if short 8.3 names are disabled on your volume.
If i understood correctly Raymond's comment ,the parsing is done by Beyond Compare not cmd.
I tried to use
file-report layout:side-by-side &
options:display-all &
output-to:"%3" output-options:html-color,wrap-word "%1" "%2"
and it worked fine on XP but shows error on windows 7 .It seems the beyond compare behaves differently for different OS.
I am coding a batch file. Here is a sample:
if exist rootsys.txt del rootsys.txt
if %lang%==1 (
if %bit%==32 echo C:\Program Files\path\to\the dir>rootsys.txt
if %bit%==64 echo C:\Program Files(x86)\path\to\the dir>rootsys.txt
goto :waset
)
This goes on for six times (so if %lang%==2 etc... to if %lang%==6.)
So, what it does, the user puts something in, like '6', so it will do the thing for the sixth.
But, whenever i try this, it ends up with \the was unexpected at this time.
So, i tried debugging with #echo on. Seemed that it gave me the error at if exist rootsys.txt del rootsys.txt. How can that happen?!
I am 110% sure i do not mention the(actually i use this as a sample, it has another name) nowhere else in my batch file. Can anyone help me? I am really stuck here.
enclose the C:\Program Files\path\to\the dir string with "