SnakePliskin Posted February 12, 2014 Share Posted February 12, 2014 Hi I'm doing a batch file that will check if ANY file exists in particular a directory. If so it creates an archive directory and archives said files before running the communications portion of the script to send and receive files. If no file exists it will use GOTO to jump directly to the communications. I need it to skip archiving if no files exist so that it does not create empty directories every time she task scheduler runs it.I know this will work if there is a file mask set, because I have tried it. My problem is that there is no real way to set any kind of file mask to look for (i.e. C:\filesout\partoffilename*.txt). The files that are being generated have no common naming convention. There is no way to change this in the app that creates the files. So I have tried (C:\filesout\*.*) This doesn't work as it runs the archive portion of the script even if no files exist. Any assistance would be greatly appreciated. Script below with comments. The part that's giving me issues is the (if exist "F:\EDIout\*.*")rem #####################rem #CD to EDI directory#rem #####################F:cd "F:\EDIout"rem ################################################################rem #First Check if files exist to be sent so we don't end up #rem #creating a bunch of empty directories. #rem #If not, bypass the archive step and go right to SND/RCV comms.#rem ################################################################if exist "F:\EDIout\*.*" (goto archive) else (goto comms):archiverem ########################rem #Set date and time Vars#rem ########################For /F "Tokens=1-7 Delims=/:. " %%d In ("%Date%%Time%") Do Set destDir=%%f-%%e-%%d_%%g.%%h.%%i.%%jrem #######################################rem #Check if dir exists. If not create it#rem #######################################if not exist "F:\EDIout\Archive\%destDir%" mkdir "F:\EDIout\Archive\%destDir%" rem ############################rem #copy files to tarchive dir#rem ############################copy "F:\EDIout\*.*" "F:\EDIout\Archive\%destDir%":commsrem #############################rem ## Comms Stuff goes next ##rem ############################# Link to comment Share on other sites More sharing options...
jaclaz Posted February 12, 2014 Share Posted February 12, 2014 Worded the other way round, the condition (pseudocode) is something *like*:IF "F:\EDIout\" IS EMPTY THEN GOTO COMMS ELSE GOTO ARCHIVESee here :http://ss64.com/nt/empty.htmlEmptyCMD Batch script to show if a folder is empty:@Echo offSetlocalif {%1}=={} set _empty=Syntax: empty.cmd "c:\Folder to check" &goto :message::Does folder existif not exist %1 set _empty=No_Such_Folder&goto :message:: Is folder empty:: Search the filenames for a random string, this will complete without error unless the directory is empty.Dir %1 /b | find /v "RandomString64" >nul && (set _empty=NotEmpty) || (set _empty=Empty):messageEcho %_empty%Endlocal&set _empty=%_empty%Of course the "RandomString64" must be choisen wisely to avoid that by pure chance a file named "RandomString64" is created in the directory.jaclaz Link to comment Share on other sites More sharing options...
SnakePliskin Posted February 12, 2014 Author Share Posted February 12, 2014 Brilliant! Thanks a bunch. Don't know why I didn't think of that!Worded the other way round, the condition (pseudocode) is something *like*:IF "F:\EDIout\" IS EMPTY THEN GOTO COMMS ELSE GOTO ARCHIVESee here :http://ss64.com/nt/empty.htmljaclaz Link to comment Share on other sites More sharing options...
jaclaz Posted February 12, 2014 Share Posted February 12, 2014 Brilliant! Thanks a bunch. Don't know why I didn't think of that!Well, the devil is in the details and ....jaclaz Link to comment Share on other sites More sharing options...
Yzöwl Posted February 12, 2014 Share Posted February 12, 2014 Here's an idea.REM ###########################REM #Navigate to EDI directory#REM #If it doesn't exist or if#REM #it has no contents-bypass#REM ###########################PUSHD F:\EDIout 2>NUL && (DIR/B/S/A|FIND "\">NUL ||GOTO comms)||(GOTO comms)REM #############################################REM #Set directory name based upon date and time#REM #############################################FOR /F "TOKENS=1-7 DELIMS=/:. " %%A IN ("%DATE%%TIME%") DO ( SET destDir=%%C-%%B-%%A_%%D.%%E.%%F.%%G)REM ###########################REM #Copy files to new archive#REM ###########################XCOPY *.* "Archive\%destDir%" /S /C /I /Q /H /K:commsREM ###########################REM ## Comms stuff goes next ##REM ########################### Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now