elwayisgod
asked on
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.......
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
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
@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)
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)
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%
)
@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%
)
ASKER
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......
see above options then!
ASKER
so I have a separate file named 'filelist.txt' which contains the 12 filenames?
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.
ASKER
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?
ASKER
Filenames are:
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_MPT_MWh .csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_MPT_Rev enue.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_MST_MWh .csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_MST_Rev enue.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_PPT_MWh .csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Purchas es_PPT_Rev enue.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_M PT_MWh.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_M PT_Revenue .csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_M ST_MWh.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_M ST_Revenue .csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_P PT_MWh.csv
%DATADIR%\Export_for_N_S_H yperion_Cu be_Sales_P PT_Revenue .csv
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
%DATADIR%\Export_for_N_S_H
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Oops! Posted late again....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Paul,
It's always saying it can't find my list.txt file:
The system cannot find the file OATI_Load_Data_File_List.t xt.
Successfully renamed files
It's always saying it can't find my list.txt file:
The system cannot find the file OATI_Load_Data_File_List.t
Successfully renamed files
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?
ASKER
How do you guys feel about 250 pts each.. Kinda used a hybrid of both?
ok here..... Dont want to get Paul in a mood :-)
Might want to post what you used for anyone looking back....
Might want to post what you used for anyone looking back....
ASKER
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
failed. was unexpected at this time. Here's code:
for /f "tokens=*" %%a in (%BATCHDIR%\OATI_Load_Data
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
ASKER
Something wrong in my echo lines...... I fixed it by removing . at the end
Be careful using ( ) & ^ % ! < > | and other chars in any batch file including your echo statement for example. Glad we got there in the end!
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
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!
Think I'd stick with a loop or 12 if not exist commands, either goto or a set if an error found!
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
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
Wow elwayisgod!
Can't believe I bagged pointson this one. Thanks a ton!
Can't believe I bagged pointson this one. Thanks a ton!
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
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
In 36926523, would you mind telling me why is there an escape code at the end of each lines?
Cheers
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.
bill
Why set a flag? Why not go straight into your IF body like this?
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
)
Surprised no-one shortened them paths more then:
set P=%DATADIR%\Export_for_N_S _Hyperion_ Cube_Purch ases
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
)
set P=%DATADIR%\Export_for_N_S
set s=%DATADIR%\Export_for_N_S
@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
)
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!
==> 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
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
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
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
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
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
Very nice...
By the way, I posted before reading your previous post 36930378.
Thanks for exlaining Bill !!
By the way, I posted before reading your previous post 36930378.
Thanks for exlaining Bill !!
@Rene,
I like that reduction, how about takeing that a little further to:
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
Nice!
Line 8 has a finger glitch: FOR %%C IN (%FileNames%)
Cheers
Line 8 has a finger glitch: FOR %%C IN (%FileNames%)
Cheers
@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
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
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....
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....
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
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
ASKER
you guys are nuts :)
And we love it :)
Me love it too!... Muhahaha.....
@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
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
Thanks bp
Check for Existence
Open in new window
Check for Non-ExistenceOpen in new window
I hope this helps