I am interested in messing with batch files and I am making a batch file to copy the contents of a drive. When I run it, the program stops when the IF statement happens. How can I fix this?
#echo on
TITLE Full Drive Back-up
ECHO 1- "E:"
ECHO 2- "F:"
ECHO 3- "G:"
SET /P choice=Which Drive would you like to backup: || Set choice=404
ECHO:
IF "%choice%" == "1" (
ECHO Copying "E:"
COPY "E:\" "D:\Desktop - data drive\Coding\Windows\Backup Storage\E"
)
IF %choice%==2 (
ECHO Copying "F:"
COPY "F:\" "D:\Desktop - data drive\Coding\Windows\Backup Storage\F"
)
IF %choice%==3 (
ECHO Copying "G:"
COPY "G:\" "D:\Desktop - data drive\Coding\Windows\Backup Storage\G"
)
PAUSE
Thank you for reading this far, have a good day.
Related
Due to little C:/ drive space, I have spent hours looking and trying code that will manage the following with my X-Plane 11 avi files process and move.
Xplane11 is creating *.avi files to my C:/ drive. Each *.avi file is approximately 2 GB in size.
Xplane11 will continue creating *.avi files as long as it continues to record flight.
I need a batch file which will do the following:
Check for *.avi files on C:/ and their status.
Wait to complete creation before moving them from C:/ drive to D:/. Then. move them.
Continue this process until simulator recording has finished and no more *.avi files are being created.
I have tried numerous variations of code similar to this:
#echo off
:loop
if not exist "C:\X-Plane 11\Output\*.avi" goto :end
if exist ("C:\X-Plane 11\Output\*.avi") do (
If %Errorlevel% gtr 0 (
echo Avi file still being created please wait!
) else (
echo Avi created and is now being moved!
goto :copy
)
)
:copy
start /w "" move "C:\X-Plane 11\Output\*.avi" "D:\X-Plane 11\Output\New Captures
loop
:end
echo Avi does not exist!
exit
The problem I find is that it won't wait until the *.avi finishes before moving and preferred without any console key presses as I am not always sitting at simulator when files being created.
I have managed a partial batch solution to my question having looked through previous stackoverflow solutions and help from its coders (thx Mofi). It will check for an existing .avi and move it to another directory when it has completed being created and repeat the process until no more .avi files exist at source location. I am now looking to change its name when moved as any new .avi files overwrite the previous.
Here is my new code so far....
#echo off
:loop
if not exist "C:\X-Plane 11\Output\*.avi" (
echo No more Avi files to move!
echo Will now Exit!
pause
goto :exit
)
) else (
if "!errorlevel!" GTR "0" (
echo
) else (
echo Please Wait.....
for %%F in (C:\X-Plane 11\Output\*.avi) do (
move "C:\X-Plane 11\Output\*.avi" "D:\X-Plane 11\Output\"
)
PING localhost -n 10 >NUL
goto :loop
)
)
)
)
:exit
exit
The batch here inserts file correctly but provides odd output for the IF EXIST. I have verified the issue as being with the statement by the echos before and after it, but the IF EXIST is pinging as true if the copy is going off. The error I get is the console text of "The system can not find the drive specified."
Code is below.
ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
::If image exists
ECHO 1
::1--
IF EXIST %directoryName% (
::If directory exists
::insert all below images
::2--
ECHO 2
COPY /-Y guest.bmp %directoryName% ) ELSE (
::Else echo directory doesnt exist
::2--
ECHO The folder %directoryName% does not exist.
goto ENDER ) ) ELSE (
::Else echo image doesn't exist
::1--
ECHO Images do not exist in current batch file directory.
goto ENDER )
::Exit insertion
:ENDER
PAUSE
I would highly advise you use a syntax of coding that is readable.
Proper indentation helps with readability of parentheses code blocks.
Using a double colon as a comment inside a parentheses code block can cause undesirable code output.
You can use a backslash to make sure you are testing for the existence of a directory.
Use quotes around your file names and file paths to protect spaces and special characters.
This may fix your problems.
#ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET "directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder"
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
REM If image exists
ECHO 1
REM 1--
IF EXIST "%directoryName%\" (
REM If directory exists
REM insert all below images
REM 2--
ECHO 2
COPY /-Y guest.bmp "%directoryName%\"
) ELSE (
REM Else echo directory doesnt exist
REM 2--
ECHO The folder %directoryName% does not exist.
goto ENDER
)
) ELSE (
REM Else echo image doesn't exist
REM 1--
ECHO Images do not exist in current batch file directory.
goto ENDER
)
::Exit insertion
:ENDER
PAUSE
For the life of me, I can't figure out why the below set prompt won't work when it is in the if statement:
#echo off
REM :askdeletecsvs
if exist *.csv (
echo Warning! All files in the scripts folder that have the "CSV" extension will be deleted!
echo Answering "n" will continue the script without deleting the CSVs.
set /p ASKDELETE=Delete CSVs? (y/n):
REM
REM if ( /i %ASKDELETE% equ "y" goto :deletecsvs )
REM if ( /i %ASKDELETE% equ "n" goto :runscripts )
REM goto :askdeletecsvs
)
When I run the batch file as it is above the cmd window opens and then shuts quickly. If I move the set line outside of the if statement then the prompt shows as expected. (There are csvs in the folder the bat file is running from)
What am I missing?
To start with you had used a closing parenthesis which was prematurely ending your opening If parenthesis.
I'd suggest reversing the thinking:
If Not Exist *.csv GoTo runscripts
Echo Warning!
Echo All files in the scripts folder that have the "CSV" extension will be deleted!
Echo Answering "N" will continue the script without deleting the CSVs.
Choice /M "Delete CSVs"
If ErrorLevel 2 GoTo runscripts
:deletecsvs
Del /F /Q /A "PathTo\scripts\*.csv"
GoTo :EOF
:runscripts
You can change GoTo :EOF to a relevant valid label as necessary or remove it if you want to continue on to :runscripts. You can also replace PathTo\scripts\ with %~dp0 if the batch file is running from the scripts directory, or remove PathTo\scripts\ if the current directory holds those files. (note that the current directory and batch file path may not necessarily be the same)
This question already has answers here:
variable not getting update inside if condition in batch script
(3 answers)
Closed 6 years ago.
Please find below a program to copy-paste the folder from one location to another.
While trying to execute it I am getting the error as : ( was unexpected at this time.
#echo off
set /p SrcPath= Source file is
echo %SrcPath%
set /p DestPath= Destination file is
echo %DestPath%
echo Checking if the package with the same name exists in the Destination Path
if exist %DestPath% (
echo Folder exists
echo Do you want to rename the existing folder Y/N
set /p Answer=
echo %Answer%
if %Answer% == y ( echo please suggest what suffix you would like to append e.g. _old, _bkp etc
set /p Suffix=
move %DestPath% %DestPath%%Suffix%
goto :CopyPackage )
if %Answer% == n echo "please decide what to do"
) else ( echo "folder doesn't exist"
goto :CopyPackage)
:CopyPackage
ROBOCOPY /s /e %SrcPath% %DestPath%
Output on cmd prompt:
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is C:\New
C:\New
Destination file is C:\New1
C:\New1
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is C:\New
C:\New
Destination file is C:\New1
C:\New1
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is "C:\New"
"C:\New"
Destination file is "C:\New1"
"C:\New1"
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.
Please suggest what modifications are required!!!
Command Line is often messing around with special characters, such as brackets, quotes etc. And it is maybe a reason, if it is not set everywhere, where cmd line expects them...
I can not find an issue in reading this, so try to clarify brackets for cmd-line.
So try to
if %Answer% == n ( echo "please decide what to do" )
if it does not work try:
if %Answer% == y (
echo please suggest what suffix you would like to append e.g. _old, _bkp etc
set /p Suffix=
move %DestPath% %DestPath%%Suffix%
goto :CopyPackage )
else ( echo "please decide what to do" )
) else (
echo "folder doesn't exist" goto :CopyPackage
)
Despite having a matching number of parentheses, I think that you've missed two parentheses, one opening and one closing.
Start by changing your if block from:
if exist %DestPath% (
echo Folder exists
echo Do you want to rename the existing folder Y/N
set /p Answer=
echo %Answer%
if %Answer% == y ( echo please suggest what suffix you would like to append e.g. _old, _bkp etc
set /p Suffix=
move %DestPath% %DestPath%%Suffix%
goto :CopyPackage )
if %Answer% == n echo "please decide what to do"
) else ( echo "folder doesn't exist"
goto :CopyPackage)
to:
if exist "%DestPath%\" (
echo Folder exists
echo Do you want to rename the existing folder Y/N
set /p Answer=
echo %Answer%
if %Answer%==y (
echo please suggest what suffix you would like to append e.g. _old, _bkp etc
set /p Suffix=
move "%DestPath%" "%DestPath%%Suffix%"
goto :CopyPackage
)
if %Answer%==n (
echo "please decide what to do"
) else (
echo "folder doesn't exist"
goto :CopyPackage
)
)
and they should then be properly balanced.
I'd like to write a batch file that logs data. Each time it runs, it should log data in a new, sequentially numbered directory.
If I were doing this in BASH I would simply do:
~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-1
~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-2
~/$ for i in {1..25}; do if [[ ! -d log-$i ]]; then mkdir log-$i; break; fi; done; echo "log-$i"
log-3
What would be the equivalent of this in Windows (XP or more recent) batch programming?
[EDIT]
This is what I implemented, and it doesn't do what I'd hoped:
set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST "C:\Thermo-Cal\NUL" "md C:\Thermo-Cal"
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\NUL" "md C:\Thermo-Cal\%UNIT_ID%"
FOR /L %%F IN (1,1,99) DO (
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL" (
"md C:\Thermo-Cal\%UNIT_ID%\log-%%F"
set "LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F"
goto dir_set
)
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo "Stuff will get saved in: %LOG_DIR%"
Running on Windows 7 (cmd) gives:
c:\batch\log-dir.bat
Check Thermo-Cal
The filename, directory name, or volume label syntax is incorrect.
Check Thermo-Cal\00534
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
"Stuff will get saved in: C:\Thermo-Cal\00534\log-1"
The first time the batch file runs, the log-1 is created.
Running the command a second time produces the exact same results, I would hope it create log-2.
Turning off the #echo off shows that the loop never breaks out early and runs (in this case) 99 times.
FOR /L %%F IN (1,1,25) DO (
IF "condition" "md C:\some\folder\log-%%F"
ECHO log-%%F
PAUSE
)
Inserted pause so you can see each output before it moves onto the next sequential number. Remove PAUSE when you finalize your script.
EDIT: Adding an IF NOT EXIST condition
FOR /L %%F IN (1,1,25) DO (
IF NOT EXIST "C:\some\folder\log-%%F\NUL" "md C:\some\folder\log-%%F"
ECHO log-%%F
PAUSE
)
When using IF [NOT] EXIST statements on directories, you must specify .\NUL as a file, as Windows normally only passes the condition on files and not folders. And in Windows, the NUL file ALWAYS exists in an existing directory.
EDIT2: Making log-%%F accessible outside of the loop
FOR /L %%F IN (1,1,25) DO (
IF NOT EXIST "C:\some\folder\log-%%F\NUL" ("md C:\some\folder\log-%%F" && SET dir%%F=C:\some\folder\log-%%F)
)
ECHO %dir1%
ECHO %dir2%
ECHO %dir3%
Plug that into a batch file and try it.
This worked on Windows 7:
set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST C:\Thermo-Cal\NUL md C:\Thermo-Cal
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST C:\Thermo-Cal\%UNIT_ID%\NUL md C:\Thermo-Cal\%UNIT_ID%
FOR /L %%F IN (1,1,99) DO (
IF NOT EXIST C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL (
md C:\Thermo-Cal\%UNIT_ID%\log-%%F
set "LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F"
goto dir_set
)
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo "Stuff will get saved in: %LOG_DIR%"
You have to be careful where you use quotes, as in batch scripting, quoted content can be seen as literal strings instead of code.
set "UNIT_ID=00534"
echo Check Thermo-Cal
IF NOT EXIST "C:\Thermo-Cal\NUL" (md C:\Thermo-Cal)
echo Check Thermo-cal\%UNIT_ID%
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\NUL" (md C:\Thermo-Cal\%UNIT_ID%)
FOR /L %%F IN (1,1,99) DO (
IF NOT EXIST "C:\Thermo-Cal\%UNIT_ID%\log-%%F\NUL" (
md C:\Thermo-Cal\%UNIT_ID%\log-%%F
set LOG_DIR=C:\Thermo-Cal\%UNIT_ID%\log-%%F
goto dir_set
)
)
echo "Couldn't create a directory to save stuff."
goto :EOF
:dir_set
echo Stuff will get saved in: %LOG_DIR%