Check for a File Periodically in a MS DOS Batch File - dos

I'm writing an MS DOS batch file that looks for a specific file until it is found.
My code gives me the following error after the SET commands
The syntax of the command is incorrect
C:\> If not exist d:\fdev\data\filename.csv
Here's the code:
SET driveltr=d:\
SET envdir=fdev\
SET datadir=data\
SET archivedir=archive\
SET inputdir=c:\Epic\v8.2\Analytics Tools\Epic BI\Input
SET filename=filename.csv
:while1
if not exist %driveltr%%envdir%%datadir%%filename%
(
echo "Waiting to check for file"
ping -n 11 127.0.0.1 > nul
goto :while1
)

You need "" If your path contains spaces. wait till file exists is working this way:
:while
IF EXIST proceed.txt goto :break
echo "Waiting to check for file"
ping -n 11 127.0.0.1 > nul
goto :while
:break

Related

Window batch commands to detect if downloaded file exists?

I use a windows batch file that scans download folder and if it detects label.pdf it sends to local printer. This works fine most of the time but often it detects file before the file completely downloads which causes PDFtoPrinter error. Is there a way to make sure the download process is complete before attempting to detect?
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
echo "Found!"
ping 127.0.0.1 -n 1 -w 3000> nul
"C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A"
ping 127.0.0.1 -n 1 -w 10000> nul
del "C:\Users\Downloads\label.pdf"
) ELSE (
echo "it isn't here!"
)
goto start
You can use the mechanism to check if a file is locked for exclusive access by another application.
#echo off
cls
:start
IF EXIST "C:\Users\Downloads\label.pdf" (
REM check if file is locked
2>nul (>>"C:\Users\Downloads\label.pdf" (call )) && ("C:\Users\Downloads\PDFtoPrinter" label.pdf "4BARCODE 4B-2054A") || (echo file is locked)
) ELSE (
echo "it isn't here!"
)
Timeout /T 10
goto start

I always get the README file in cmd rather than commands if I type commands

I am trying to make a program in Windows, and whenever you type "commands" or anything else, it always shows you the README file. Nothing else.
I have tried adding quotes to the commands in the cmd file, but that still didn't work. I'm using Windows 8.1 x64.
Prototype.cmd:
#echo off
set version=1.0.0 ALPHA
title Prototype version %version%
echo Welcome to Prototype!
echo You have to change the User Directory in the command file to your username.
timeout 1 > NUL
IF NOT EXIST C:\Users\OldBo-5\Desktop\Prototype\System GOTO SYSTEMNOTFOUND
timeout 5 > NUL
cls
:PrototypeMain
echo Type "commands" to get a list of commands.
echo Type "README" to get the readme text file.
timeout 1 > NUL
set /p cmd=">"
cd C:\Users\OldBo-5\Desktop\Prototype\System\Commands
if %cmd% == "readme" goto :ReadmeCmd
:ReadmeCmd
notepad C:\Users\OldBo-5\Desktop\Prototype\READ ME.text
cls
goto PrototypeMain
if %cmd% == "commands" goto :HelpCmd
:HelpCmd
cls
echo "COMMANDS" - Shows this.
echo "README" - Shows readme.text
timeout 10 > NUL
goto PrototypeMain
:SYSTEMNOTFOUND
cls
title Error starting Prototype.
echo Error starting Prototype. Error Code: 4 - System directory not found. Output error log to ROOT\ErrorLog.text
echo Did you forget to install?
echo To open, right click and click Edit. Select Notepad.
cd C:\Users\OldBo-5\Desktop\Prototype\
type NUL > ErrorLog.text
echo Prototype failed to boot. Error Code: 4 > ErrorLog.text
echo Reason: Error Code 4 means that the system directory could not be found. >> ErrorLog.text
timeout 5 > NUL
exit
Does this help you?
#Echo Off
Set "version=1.0.0 ALPHA"
Title Prototype version %version%
CD /D "%UserProfile%\Desktop\Prototype" 2>NUL||Exit /B
Echo Welcome to Prototype!
Timeout 1 >NUL
If Not Exist "System\" GoTo SYSTEMNOTFOUND
:PrototypeMain
ClS
Set "cmnd="
Echo To get a list of commands type COMMANDS
Echo To get the ReadMe text file type README
Set /P "cmnd=>"
If /I "%cmnd%" == "readme" (
Notepad "READ ME.text"
) Else If /I "%cmnd%" == "commands" (
ClS
Echo COMMANDS - Shows this.
Echo README - Shows readme.text
Timeout 10 >NUL
)
GoTo PrototypeMain
:SYSTEMNOTFOUND
ClS
Title Error starting Prototype.
Echo Error starting Prototype. Error Code: 4 - System directory not found. Output error log to ErrorLog.text
Echo Did you forget to install?
( Echo Prototype failed to boot. Error Code: 4
Echo Reason: Error Code 4 means that the system directory could not be found.
)>"ErrorLog.text"
Timeout 5 >NUL
Exit /B

I dont want my batch program to terminate after it calls "spl stop"

I have my batch script below:-
#echo off
For /F "tokens=1* delims==" %%A IN (test.properties) DO (
IF "%%A"=="PORT" set PORT=%%B
IF "%%A"=="SPLEBASE" set SPLEBASE=%%B
IF "%%A"=="SPLOUTPUT" set SPLOUTPUT=%%B
IF "%%A"=="SPLENVIRON" set SPLENVIRON=%%B
IF "%%A"=="JAVA_HOME" set JAVA_HOME=%%B
IF "%%A"=="jarsource" set jarsource=%%B
IF "%%A"=="jartarget" set jartarget=%%B
)
echo.
echo The environment properties are as follows :-
echo.
echo port number is %PORT%
echo environment path is %SPLEBASE%
echo environment output location is %SPLOUTPUT%
echo environment name is %SPLENVIRON%
echo java home is %JAVA_HOME%
echo cm jar source location is %jarsource%
echo cm jar target location is %jartarget%
echo.
echo on
cd %SPLEBASE%/bin
spl stop
echo off
:loop
netstat -n -o -a | findstr ":6500"
if %ERRORLEVEL% equ 0 (#echo "listening") ELSE goto exitloop
goto loop
:exitloop
echo server has been stopped
The problem is that after It executes spl stop, it exits from my current batch program and does not go into the coming loop at all. spl.cmd is a file that calls a vb script which executes and exits hence terminating my current batch program. I don't want my batch program to terminate. Please suggest something so that my batch proceeds to the loop after running spl stop
Try using the CALL command:
call spl stop

Need help modifying script to use individual files, instead of all files at once

I was wondering if anyone could help me with a very simple rework of this script I inherited. I have no previous experience with batch files and needed to modify this already working script in a very simple manner.
Currently, this script does the following:
- Monitors a folder and looks to see if there are files in it
- If there are, it executes a FTP transfer via command line
- Then, it moves the files into a archive folder created and named with the current timestamp and writes something to a text log file
- If there are no files, the script exits and does nothing.
As of now, the script is looking for, processing, and moving all the files at once. I was looking to see if I could be pointed int the right direction with help on how to modify the script so that it does each file one by one, instead of using . everywhere. Ultimately, I think all I need to do is figure out how to do the loop correctly and read/store the filenames to use as variables instead of using . Any help would be appreciated.
#ECHO OFF
:: Set count of files = 0
SET X=0
:: Set timestamp for processed folders
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%
:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%i IN (c:\Encoded_HL7_Vanderbilt\*.*) DO IF EXIST %%i (SET X=1)
IF [%X%] gtr [0] (cd\..\..
cd\"Program Files (x86)\Ipswitch\WS_FTP 12"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error switching to ftp program directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)
wsftppro -s local:C:\Encoded_HL7_Vanderbilt\*.* -d Vandy!Vanderbilt:/incoming/
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error transmitting file to ftp server>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)
md "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error creating archive directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)
move "C:\Encoded_HL7_Vanderbilt\*.*" "E:\Processed_HL7_Vanderbilt%1\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error moving files to archive directory>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt
exit)) ELSE (exit)
echo %TIMESTAMP% File transfer completed successfully>E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.success.txt
exit
Give this a walk around the park: if any file generates an error then it will stop processing further files.
#ECHO OFF
set "folder=%~1"
:: Set timestamp for processed folders
set TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%
:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%a IN (c:\Encoded_HL7_Vanderbilt\*.*) DO (
cd\"Program Files (x86)\Ipswitch\WS_FTP 12"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error switching to ftp program directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)
wsftppro -s local:%%a -d Vandy!Vanderbilt:/incoming/
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error transmitting "%%a" file to ftp server>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)
md "E:\Processed_HL7_Vanderbilt\%folder%\%TIMESTAMP%" 2>nul
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error creating archive directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)
move "%%a" "E:\Processed_HL7_Vanderbilt\%folder%\%TIMESTAMP%"
IF ERRORLEVEL 1 (echo %TIMESTAMP% Error moving "%%a" file to archive directory>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.error.txt
goto :done)
)
echo %TIMESTAMP% File transfers completed successfully or no files were found>E:\HL7_Vanderbilt_log\%folder%\%TIMESTAMP%.success.txt
exit
:done
goto :EOF
try this, it might work for you:
#ECHO OFF &SETLOCAL
:: Set count of files = 0
SET /a count=0
:: Set timestamp for processed folders
set "TIMESTAMP=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.%TIME:~9,2%"
cd "%ProgramFiles(x86)%\Ipswitch\WS_FTP 12" || (
echo %TIMESTAMP% Error switching to ftp program directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
exit /b 1
)
md "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" || (
echo %TIMESTAMP% Error creating archive directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
exit /b 1
)
:: If more than 0 files exist begin ftp and file archival otherwise exit
FOR %%i IN (c:\Encoded_HL7_Vanderbilt\*) DO (
set /a count+=1
REM I don't know the wsftppro command line parameters, pls. check the man page
wsftppro -s local:"%%~i" -d Vandy!Vanderbilt:/incoming/ || (
echo %TIMESTAMP% Error transmitting file to ftp server>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
exit /b 1
)
move "%%~i" "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" || (
echo %TIMESTAMP% Error moving files to archive directory>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.error.txt"
exit /b 1
)
)
if %count% equ 0 (
rd "E:\Processed_HL7_Vanderbilt\%1\%TIMESTAMP%" 2>nul
exit /b 1
)
echo %TIMESTAMP% File transfer completed successfully>"E:\HL7_Vanderbilt_log\%1\%TIMESTAMP%.success.txt"

Bat file and labels

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

Resources