Jump to content

2008 server - batch file help - if any file exists in path


Recommended Posts

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


Worded the other way round, the condition (pseudocode) is something *like*:

IF "F:\EDIout\" IS EMPTY THEN GOTO COMMS ELSE GOTO ARCHIVE

See here ;):

http://ss64.com/nt/empty.html

Empty

CMD Batch script to show if a folder is empty:
@Echo off
Setlocal
if {%1}=={} set _empty=Syntax: empty.cmd "c:\Folder to check" &goto :message

::Does folder exist
if 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)

:message
Echo %_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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...