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
Related
i have been trying to get this to work but it either does not follow the if/else or it just goes through the the copy and echo and never goes to else if the file does not exists
IF EXIST "\\netowrklocation\folder\*.*" (
copy "\\netowrklocation\folder\*.*" "\\remotepc\folder" >nul
echo \\netowrklocation\folder\*.*
echo copied \\netowrklocation\folder\*.* >>%log%
) ELSE (
Echo The file was not found.
echo try again later
goto repeat
)
echo Files Copied
echo Files Copied >>%log%
i'm not sure what i'm doing wrong, but what i want it to do is check if their is a file in \netowrklocation\folder\, if their is copy it and do the echo/log
if not echo file not found, and go to the repeat area (not shown, not relevant)
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
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)
#ECHO OFF
set /p TerminalName= Enter the PC you wish to relocate ECFs on:
ECHO Do you wish to relocate the ECFs on %TerminalName% ?
PAUSE
IF NOT EXIST "\\%TerminalName%\c$\Program Files\Google" (
ECHO You don't have Google installed
) ELSE (
ECHO You have Google installed!
ECHO Relocating the ECF Files! Here we go...
IF EXIST "\\%TerminalName%\c$\Program Files\Google" (
ECHO The ECF Folder already exists..
ECHO Moving ECFs now
cd \\%TerminalName%\c$\Program Files\Google
FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google"
) ELSE (
ECHO No ECF Folder exists...Let's sort that out!
MKDIR "\\%TerminalName%\c$\Program Files\Google"
cd \\%TerminalName%\c$\Program Files\Google
ECHO Moving ECFs now
FOR %%f IN (*.ecf) DO move /y "%%f" "\\%TerminalName%\c$\Program Files\Google"
)
)
ECHO Finished!
PAUSE
Whenever running the above Batch file i get this error:
CMD does not support UNC paths as current directories.
Ps - Sorry for the formatting...it won't all go into the grey for code :(
Here is a batch code which should work for your task:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET /P "TerminalName=Enter the PC you wish to relocate ECFs on: "
ECHO Do you wish to relocate the ECFs on !TerminalName! ?
PAUSE
SET "SourcePath=\\RemoteComputer\\c$\Program Files\Google\ECF_Folder"
SET "TargetPath=\\!TerminalName!\c$\Program Files\Google"
IF NOT EXIST "!TargetPath!" (
ECHO You don't have Google installed.
GOTO EndMoveECF
)
ECHO You have Google installed!
ECHO Relocating the ECF files! Here we go...
IF EXIST "%TargetPath%\ECF_Folder" (
ECHO The ECF folder already exists..
) ELSE (
ECHO No ECF Folder exists... Let's sort that out!
MKDIR "%TargetPath%\ECF_Folder"
IF ERRORLEVEL 1 (
ECHO Failed to create ECF folder "%TargetPath%\ECF_Folder".
GOTO EndMoveECF
)
)
ECHO Moving ECF files now ...
MOVE /Y "%SourcePath%\*" "%TargetPath%\ECF_Folder\"
ECHO Finished!
:EndMoveECF
ENDLOCAL
PAUSE
You need to set SourcePath accordingly. And you need to replace all occurrences of ECF_Folder by whatever is right in your environment.
Moving the files is done with command MOVE without switching current working directory as this is not needed. And command MOVE supports also wildcards and therefore no need for a FOR loop.
Delayed environment variable expansion is used partly in case of user of batch file enters an invalid terminal name containing for example a double quote, angle brackets or other characters with a special meaning in batch files. Open a command prompt window and execute in this window set /? for details about delayed expansion.
There is once !TerminalName! and once !TargetPath! used instead of %TerminalName% and %TargetPath%. After existence of Google directory on remote computer is positive verified, it should be safe to reference TargetPath without delayed expansion.
Instead of CD, the PUSHD command can be used. Remember, that internally PUSHD will do a NET USE and consume a "drive letter" from the OS. If this is done too many times, the system will run out of "drive letters."
Remember to POPD at an appropriate time.
UPDATE: Looking at this again, I doubt if you ever need to change directories. Where do the files exist that are to be MOVE'd to the new directory? More analysis needs to be done.
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%