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

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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

Check for Existence
IF EXIST <FILENAME> ECHO Successfully renamed

Open in new window

Check for Non-Existence

IF NOT EXIST <FILENAME> ECHO Failed

Open in new window


I hope this helps
Steve KnightIT 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
OrenRozenCommented:
@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)
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Steve KnightIT 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%
)
elwayisgodAuthor 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......
Steve KnightIT ConsultancyCommented:
see above options then!
elwayisgodAuthor Commented:
so I have a separate file named 'filelist.txt' which contains the 12 filenames?
Steve KnightIT 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.
elwayisgodAuthor 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?
elwayisgodAuthor 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
Paul TomasiCommented:
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

Open in new window


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

Your issues matter to us.

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

Start your 7-day free trial
Steve KnightIT 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.
Paul TomasiCommented:
Oops! Posted late again....
Steve KnightIT 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?
Steve KnightIT 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?
elwayisgodAuthor 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
Steve KnightIT 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?
elwayisgodAuthor Commented:
How do you guys feel about 250 pts each.. Kinda used a hybrid of both?
Steve KnightIT ConsultancyCommented:
ok here..... Dont want to get Paul in a mood :-)  
Might want to post what you used for anyone looking back....
elwayisgodAuthor 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
elwayisgodAuthor Commented:
Something wrong in my echo lines...... I fixed it by removing . at the end
Steve KnightIT ConsultancyCommented:
Be careful using ( ) & ^ % ! < > | and other chars in any batch file including your echo statement for example.  Glad we got there in the end!
Bill PrewIT / 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 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
)

Open in new window

~bp
Steve KnightIT 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!
Bill PrewIT / 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
Paul TomasiCommented:
Wow elwayisgod!

Can't believe I bagged pointson this one. Thanks a ton!
Bill PrewIT / 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 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

Open in new window

~bp
ReneGeCommented:
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
Paul TomasiCommented:
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.
Paul TomasiCommented:
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
)

Open in new window

Steve KnightIT 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
)
Paul TomasiCommented:
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!
Bill PrewIT / 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
ReneGeCommented:
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

Open in new window

Bill PrewIT / 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
ReneGeCommented:
Very nice...
By the way, I posted before reading your previous post 36930378.
Thanks for exlaining Bill !!
Bill PrewIT / Software Engineering ConsultantCommented:
@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

Open in new window

~bp
ReneGeCommented:
Nice!

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

Cheers
ReneGeCommented:
@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

Open in new window

Paul TomasiCommented:
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....
ReneGeCommented:
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
elwayisgodAuthor Commented:
you guys are nuts :)
ReneGeCommented:
And we love it :)
Paul TomasiCommented:
Me love it too!... Muhahaha.....
Bill PrewIT / 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
ReneGeCommented:
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.