tags:

views:

48

answers:

2

How would one go best about checking for existance of all files before building ?

Let me explain; I mostly build stuff from the command prompt. No problems there, just put the build command and all in the one .bat /.cmd file, and run it. It works fine.

But, for the normal running of my program, for example, I need several source files for the build, and then an additional few data files, measured data and such.

Is there a way to test via batch file whether a file exists, and if it exists just write OK ?

file1.for OK
file2.for OK
datafile.txt OK data.dat MISSING FROM DIRECTORY

Any ideas how this could be accomplished ?

+1  A: 

Something like this?

@ECHO OFF
IF EXIST "c:\myfile1.txt" (ECHO myfile1.txt OK) ELSE (ECHO myfile1.txt FILE MISSING FROM DIRECTORY)
IF EXIST "c:\myfile2.txt" (ECHO myfile2.txt OK) ELSE (ECHO myfile2.txt FILE MISSING FROM DIRECTORY)

For a list of available commands, see http://ss64.com/nt/

Bernhof
+1  A: 

As a slightly more advanced approach:

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set FileList=file1.for file2.for "File with spaces" ...
Set Build=1
For %%f In (%FileList%) Do Call :FileExists %%f

If Not Defined Build (
    Echo.
    Echo Build aborted. Files were missing.
    GoTo :EOF
)

...

GoTo :EOF

:FileExists
Set FileName=%~1
If Exist "!FileName!" (
    Echo !FileName! OK
) Else (
    Echo !FileName! MISSING FROM DIRECTORY
    Set Build=
)
GoTo :EOF

You can put all files into the FileList variable. The Build variable controls whether to continue with the build. A single missing file causes it to cancel.

Joey