# If exist help

I need to verify that 12 files exist in a directory.  If they do, it needs to echo out 'Successfully renamed files' to the log file.  If all 12 don't exist, then it needs to echo out 'Failed to rename all the files' ........  Not sure how to link 12 IF Exists.......
Asked:
###### Who is Participating?

Commented:
No probs....

With all 12 filenames in a text file named FILES.TXT, the following code reads in the filenames and checks whether they're present.

@echo off

set folder=c:\temp

for /f "tokens=*" %%a in (FILES.TXT) do (
if not exist "%folder%\%%~a" (
echo Failed to rename all the files
exit /b
)
)

echo Successfully renamed files


Naturally, you'll want to set the variable folder to a foldername of your own choice...

BTW, simply place each filename on a seperate line in FILES.TXT and you're good to go.
0

Commented:
You would need something like in terms of code segment

Check for Existence
IF EXIST <FILENAME> ECHO Successfully renamed

Check for Non-Existence

IF NOT EXIST <FILENAME> ECHO Failed


I hope this helps
0

IT ConsultancyCommented:
do it the other way around.

If not exist file goto error
if not exist filw goto error
etc.
Echo is ok.
Exit /b
:error
echo isnt

if they have structure to the names, e .g file1 file2 etc we could check with loop using for /l or we could read list of files froma text file in for loop too.

Or you just got for the 'simpleK option of 12 if not exist lines.

steve
0

Commented:
@echo off

set AllExist = YES

if NOT exist MyFile1 set AllExist = NO
if NOT exist MyFile2 set AllExist = NO
if NOT exist MyFile3 set AllExist = NO
if NOT exist MyFile4 set AllExist = NO

if !%AllExist%==!YES (echo Yes) else (echo No)
0

IT ConsultancyCommented:
e.g. This will look for all files mentioned in the text file shown and if they dont exists records which are missing

@echo off
setlocal enabledelayedexpansion
set ERROR=NO
for /f "tokens=*" %%a in ('type filelist.txt') do (
if not exist "%%~a" set error=%%~a,!error!
)
if %error%==NO (
echo All is OK
) ELSE (
echo There were problems with %error%
)
0

Author Commented:
I really need for it to look at all 12 files as a group if possible.  As if one of them aren't present, then it's a fail......
0

IT ConsultancyCommented:
see above options then!
0

Author Commented:
so I have a separate file named 'filelist.txt' which contains the 12 filenames?
0

IT ConsultancyCommented:
you can do that or if they are short we could include them in the batch file with a for loop... Or just do 12 if NOT exist commands like in my first post or others since.
0

Author Commented:
It opens the text file up on the server though and script stops until I close that file.  Does it have to open up the text file with the list of the filenames?
0

Author Commented:
Filenames are:

%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_Revenue.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_Revenue.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_Revenue.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_Revenue.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_Revenue.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_MWh.csv
%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_Revenue.csv
0

IT ConsultancyCommented:
no reason it should open up the text file... Im typing this on mobile so may have something wrong but cant see any.

Please run without echo off from cmd prompt as posted above and see if you can see why.
Are you running this from a unc path rather than local or mapped drive?
Is there spaces in the name or path you are specifying for the text file.
If unsure could you post the script as you have it pls.
0

Commented:
Oops! Posted late again....
0

IT ConsultancyCommented:
in which case then pls remove %datadir% from the filename and add in the batch:

if not exist "%datadir%\%%~a" ..... As before.

Add an extra line

echo "%datadir%\%%~a" under that and before next ) to show the path that it is using.  what is the path in %datadir% for instance.... Hopefully not got " around it?
0

IT ConsultancyCommented:
in which case then pls remove %datadir% from the filename and add in the batch:

if not exist "%datadir%\%%~a" ..... As before.

Add an extra line

echo "%datadir%\%%~a" under that and before next ) to show the path that it is using.  what is the path in %datadir% for instance.... Hopefully not got " around it?
0

Author Commented:
Paul,

It's always saying it can't find my list.txt file:

The system cannot find the file OATI_Load_Data_File_List.txt.
Successfully renamed files
0

IT ConsultancyCommented:
unsure if you are trying mine or paul's now..... Please post batch file as you have it since what both of us have posted should work as they are?
0

Author Commented:
How do you guys feel about 250 pts each.. Kinda used a hybrid of both?
0

IT ConsultancyCommented:
ok here..... Dont want to get Paul in a mood :-)
Might want to post what you used for anyone looking back....
0

Author Commented:
I had it working now I get this:

failed. was unexpected at this time.  Here's code:

for /f "tokens=*" %%a in (%BATCHDIR%\OATI_Load_Data_File_List.txt) do (
if not exist "%DATADIR%\%%~a" (
echo ERROR - Failed to rename all the data files. Check for ERRORS below when script tries and loads via MaxL will show which one(s) failed.
exit /b
)
)

echo SUCCESS - Successfully renamed all the data files
0

Author Commented:
Something wrong in my echo lines...... I fixed it by removing . at the end
0

IT ConsultancyCommented:
Be careful using ( ) & ^ % ! < > | and other chars in any batch file including your echo statement for example.  Glad we got there in the end!
0

Commented:
Just for what it's worth, could certainly be done in a "brute force" approach like this:

@echo off
set Flag=N
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_Revenue.csv" set Flag=Y
if %Flag% == Y (
echo YES
) else (
echo NO
)

~bp
0

IT ConsultancyCommented:
True.  Will it handle all those potentially long paths effectively as one line I wonder?!
Think I'd stick with a loop or 12 if not exist commands, either goto or a set if an error found!
0

Commented:
Command length a possible consideration Steve, although it seems to be a lot longer these days.

I do have a clever technique for dealing with that though, will have to dig up what I am thinking of and post back...

~bp
0

Commented:
Wow elwayisgod!

Can't believe I bagged pointson this one. Thanks a ton!
0

Commented:
Okay, took a little more plumbing than I thought, but here's the idea.  Basically we're using "reflection" against the running batch script to grab the names of the files to be checked from it.  Saves having to create and maintain a second TXT file with the file names.  Feedback welcome...

@echo off
setlocal EnableDelayedExpansion

set DATADIR=c:\ee

set AllExist=Y
for /f "tokens=1* delims==" %%A in ('findstr /B /C:"$DATA$" ^<"%~f0"') do if not exist "%%~B" set AllExist=N

if %AllExist% == Y (
echo SUCCESS - Successfully renamed all the data files
) else (
echo ERROR - Failed to rename all the data files. Check for ERRORS below when script tries and loads via MaxL will show which ones failed.
)

goto :EOF

$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_MPT_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_MPT_Revenue.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_MST_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_MST_Revenue.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_PPT_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Purchases_PPT_Revenue.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_MPT_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_MPT_Revenue.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_MST_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_MST_Revenue.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_PPT_MWh.csv
$DATA$=!DATADIR!\Export_for_N_S_Hyperion_Cube_Sales_PPT_Revenue.csv

~bp
0

Commented:
Hey Bill, I just learned something new from you...again... Thanks a bunch!

In 36926523, would you mind telling me why is there an escape code at the end of each lines?

Cheers
0

Commented:
Huh! I rote the same program last night (but dependant on /e ".csv") but never posted it... oh, and mine just accepted "%DATADIR%\..." as supplied by the author.
0

Commented:
bill

Why set a flag? Why not go straight into your IF body like this?

@echo off
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_MST_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases_PPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MPT_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_MST_Revenue.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_MWh.csv" ^
if exist "%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales_PPT_Revenue.csv" (
echo YES
) else (
echo NO
)

0

IT ConsultancyCommented:
Surprised no-one shortened them paths more then:

set P=%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases
set s=%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales

@echo off
if exist "%p%_MPT_MWh.csv" ^
if exist "%p%_MPT_Revenue.csv" ^
if exist "%p%_MST_MWh.csv" ^
if exist "%p%_MST_Revenue.csv" ^
if exist "%p%_PPT_MWh.csv" ^
if exist "%p%_PPT_Revenue.csv" ^
if exist "%s%_MPT_MWh.csv" ^
if exist "%s%_MPT_Revenue.csv" ^
if exist "%s%_MST_MWh.csv" ^
if exist "%s%_MST_Revenue.csv" ^
if exist "%s%_PPT_MWh.csv" ^
if exist "%s%_PPT_Revenue.csv" (
echo YES
) else (
echo NO
)
0

Commented:
Didn't shorten them because it's easier to just drag and drop the whole thing into the batch file and leave it at that. But point taken!
0

Commented:
==> Why set a flag? Why not go straight into your IF body like this?

Personal preference.  When I have a longish and complicated set of conditions that are checked I sometimes find it useful to just set a flag rather than embed the dependent logic into the complex conditional.

I also often find the use of a flag variable a bit self documenting, as in this case:

if %AllExist% == Y (
echo SUCCESS - Successfully renamed all the data files
) else (
echo ERROR - Failed to rename all the data files. Check for ERRORS below when script tries and loads via MaxL will show which ones failed.
)

For someone reading the script I feel this more quickly lets them know what is going on in that segment of the code.

Definitely a personal preference thing, and I often error on the side of a few extra lines of code for some clarity compared to some people that try to condense code to the fewest lines of code.  Both are valid and useful, just different styles.

Thanks for sharing a different perspective, hope you can better understand my approach.

~bp
0

Commented:
Just for the fun of it, here another versions

By the way, I'm dying to know why there is an escape code at the end of each lines (if exist "%p%_MPT_MWh.csv" ^).

Cheers,
Rene

@ECHO OFF
SET P=%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases
SET s=%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales
SET Flag=Yes
FOR %%A IN ("%P%","%S%") DO FOR %%B IN (MPT,MST,PPT) DO FOR %%C IN (MWh,Revenue) DO IF NOT EXIST "%%~A_%%B_%%C.csv" SET Flag=No
IF %Flag% EQU Yes (ECHO Yes) ELSE (ECHO NO)
PAUSE
EXIT

0

Commented:
That's a continuation character Rene, so that the interpretter treats that all as one long statement, which is how "AND" is implemented in a complex conditional in a BAT file.  So rather than writing:

IF A == B if C == D if E == F echo Hello

you can write it as:

IF A == B ^
if C == D ^
if E == F echo Hello

~bp
0

Commented:
Very nice...
By the way, I posted before reading your previous post 36930378.
Thanks for exlaining Bill !!
0

Commented:
@Rene,

I like that reduction, how about takeing that a little further to:

@ECHO OFF
SET BaseFolders="%DATADIR%\Export_for_N_S_Hyperion_Cube_Purchases","%DATADIR%\Export_for_N_S_Hyperion_Cube_Sales"
SET FileTypes=MPT,MST,PPT
SET FileNames=MWh,Revenue
SET Flag=Yes
FOR %%A IN (%BaseFolders%) DO (
FOR %%B IN (%FileTypes%) DO (
FOR %%C IN (%Fileames%) DO (
IF NOT EXIST "%%~A_%%B_%%C.csv" SET Flag=No
)
)
)
IF %Flag% EQU Yes (
ECHO Yes
) ELSE (
ECHO NO
)
PAUSE

~bp
0

Commented:
Nice!

Line 8 has a finger glitch: FOR %%C IN (%FileNames%)

Cheers
0

Commented:
@Bill

To conclude with the escape code, I did the following to test it.

If i omit the escape code, an error is generated.

Thanks for making me learn something new.

Cheers,
Rene

@echo off
if 1 == 1 ^
if 2 == 2 (echo YES) else (echo NO)

if 1 == 1 ^
if 2 == 1 (echo YES) else (echo NO)
PAUSE

0

Commented:
Funnily enough, I programmed one of those too (again, last night) but for fun only.

What I realised from the various methods is that it's easy to reach a point where the logic becomes too obscure, or when the code becomes difficult to maintain.

Sometimes, for the sake of a few extra lines, it is better to have clarity in a program.

Going back to the beginning, simply listing all 12 lines filenames (with their paths) may be preferable to the complexity and obscurity of a 3-tier nested FOR construction.

Just thought I'd share my thoughts on this one....
0

Commented:
Nice comment Paul,

I'm way a less experienced programmer than you guys. When I started to do serious batch files, and with the help of Bill, the Qs, Steve, Paul,oBdA,Knight, leew, etc... I'v learned a lot, and more!!

Therefore, in the process, I have a bunch of batch files with hundrend of lines. A lot of them, are what I call "fit" or "optimised". My challange today, if for any reasons, I need to make a change; Oh boy... the "obscure" factor is quite challanging.

Now, I don't mind adding few extra lines, to make sure that for later on, I'll be able to read the code like a book!

Cheers,
Rene
0

Author Commented:
you guys are nuts :)
0

Commented:
And we love it :)
0

Commented:
Me love it too!... Muhahaha.....
0

Commented:
@ReneGe

Just keep in mind you can't break a single DOS command line anyplace you want and continue to a new line with the ^ approach.  It has to be at a "logical" breaking point in the line command, or more often between commands.  I haven't seen the rules documented, it's somewhat trial and error, and occasionally I've had to add some parens where I wouldn't normally need them.

~bp
0

Commented:
Thanks bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.