# 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.......
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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)
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%
)
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......
IT ConsultancyCommented:
see above options then!
Author Commented:
so I have a separate file named 'filelist.txt' which contains the 12 filenames?
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.
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?
Author Commented:
Filenames are:

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.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

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.
Commented:
Oops! Posted late again....
IT ConsultancyCommented:
in which case then pls remove %datadir% from the filename and add in the batch:

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

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?
IT ConsultancyCommented:
in which case then pls remove %datadir% from the filename and add in the batch:

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

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?
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
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?
Author Commented:
How do you guys feel about 250 pts each.. Kinda used a hybrid of both?
IT ConsultancyCommented:
ok here..... Dont want to get Paul in a mood :-)
Might want to post what you used for anyone looking back....
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 (
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
Author Commented:
Something wrong in my echo lines...... I fixed it by removing . at the end
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!
IT / Software Engineering ConsultantCommented:
Just for what it's worth, could certainly be done in a "brute force" approach like this:

@echo off
set Flag=N
if %Flag% == Y (
echo YES
) else (
echo NO
)
~bp
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!
IT / Software Engineering ConsultantCommented:
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
Commented:
Wow elwayisgod!

Can't believe I bagged pointson this one. Thanks a ton!
IT / Software Engineering ConsultantCommented:
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 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
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
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.
Commented:
bill

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

@echo off
echo YES
) else (
echo NO
)
IT ConsultancyCommented:
Surprised no-one shortened them paths more then:

@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
)
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!
IT / Software Engineering ConsultantCommented:
==> 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
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 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
IT / Software Engineering ConsultantCommented:
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
Commented:
Very nice...
Thanks for exlaining Bill !!
IT / Software Engineering ConsultantCommented:
@Rene,

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

@ECHO OFF
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
Commented:
Nice!

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

Cheers
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
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....
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
Author Commented:
you guys are nuts :)
Commented:
And we love it :)
Commented:
Me love it too!... Muhahaha.....
IT / Software Engineering ConsultantCommented:
@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
Commented:
Thanks bp
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.