if\else not working as expected with network location - batch-file

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)

Related

What :PROMPT means in dos batch file? [duplicate]

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause

Move avi files to a different drive after being created using batch file

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

How do you use batch IF EXIST to test directories?

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

Syntx Error with IF ELSE in batch-file

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.

How can a create a directory that follows a numbered sequence?

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%

Resources