Solved

# If exist help

Posted on 2011-10-06
Medium Priority
481 Views
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.......
0
Question by:elwayisgod
• 12
• 9
• 8
• +4

LVL 9

Expert Comment

ID: 36925559
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

LVL 43

Expert Comment

ID: 36925562
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

LVL 3

Expert Comment

ID: 36925588
@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

LVL 43

Expert Comment

ID: 36925606
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 Comment

ID: 36925666
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

LVL 43

Expert Comment

ID: 36925751
see above options then!
0

Author Comment

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

LVL 43

Expert Comment

ID: 36925829
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 Comment

ID: 36925845
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 Comment

ID: 36925867
Filenames are:

0

LVL 11

Accepted Solution

paultomasi earned 668 total points
ID: 36925880
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

LVL 43

Expert Comment

ID: 36925881
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

LVL 11

Expert Comment

ID: 36925884
Oops! Posted late again....
0

LVL 43

Assisted Solution

Steve Knight earned 1332 total points
ID: 36925978
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?
0

LVL 43

Assisted Solution

Steve Knight earned 1332 total points
ID: 36925982
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?
0

Author Comment

ID: 36926035
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

LVL 43

Expert Comment

ID: 36926079
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 Comment

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

LVL 43

Expert Comment

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

Author Comment

ID: 36926235
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
0

Author Comment

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

LVL 43

Expert Comment

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

LVL 59

Expert Comment

ID: 36926523
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
0

LVL 43

Expert Comment

ID: 36926551
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

LVL 59

Expert Comment

ID: 36926623
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

LVL 11

Expert Comment

ID: 36926961
Wow elwayisgod!

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

LVL 59

Expert Comment

ID: 36928389
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
0

LVL 10

Expert Comment

ID: 36928562
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

LVL 11

Expert Comment

ID: 36928999
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

LVL 11

Expert Comment

ID: 36929010
bill

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

@echo off
echo YES
) else (
echo NO
)

0

LVL 43

Expert Comment

ID: 36929067
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
)
0

LVL 11

Expert Comment

ID: 36929708
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

LVL 59

Expert Comment

ID: 36930378
==> 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

LVL 10

Expert Comment

ID: 36930468
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

0

LVL 59

Expert Comment

ID: 36930493
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

LVL 10

Expert Comment

ID: 36930506
Very nice...
Thanks for exlaining Bill !!
0

LVL 59

Expert Comment

ID: 36930515
@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
0

LVL 10

Expert Comment

ID: 36930557
Nice!

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

Cheers
0

LVL 10

Expert Comment

ID: 36930600
@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

LVL 11

Expert Comment

ID: 36930608
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

LVL 10

Expert Comment

ID: 36930673
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 Comment

ID: 36931193
you guys are nuts :)
0

LVL 10

Expert Comment

ID: 36931293
And we love it :)
0

LVL 11

Expert Comment

ID: 36931446
Me love it too!... Muhahaha.....
0

LVL 59

Expert Comment

ID: 36932448
@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

LVL 10

Expert Comment

ID: 36932721
Thanks bp
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
###### Suggested Courses
Course of the Month15 days, 23 hours left to enroll