Upstart
Upstart

Reputation: 196

How to check a folder for files, or else do nothing?

I inherited the following script that essentially just looks in a folder, processes the files individually, then moves them and writes to a log.

The current issue I face is that if the folder is empty, the script still creates a processing folder to move the potential files to, as well as writes to the log. Can anyone help me modify the script such that nothing will happen if there are no files in the folder? I know I need to do the check at the top of the script, then exit out if there are no files, but I have no experience in batch files and I am not sure how to accomplish this.

@ECHO OFF

set "folder=%~1"


::HERE I NEED ADD A CHECK TO SEE IF FILES EXIST.  IF THEY DO, GO THROUGH SCRIPT.  
::IF NOT, EXIT EVERYTHING

:: 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

Upvotes: 0

Views: 1046

Answers (1)

cure
cure

Reputation: 2688

dir "%~1" /a:-d /b >nul 2>&1
if %errorlevel%==1 exit

at the very beginning.

Upvotes: 1

Related Questions