Check if certain files exist in a directory, if not add name to a textfile

By v2.kumar ·
We recieve some logs on our windows box via FTP on a daily basis, in the same directory. I would like to check for missing logs files and add their name to a text file.

Something like...

Check if C:\logs\file1_currentdate exists (if not, add file1_currentdate to C:\missingfiles_currentdate.txt)

Check if C:\logs\file_currentdate exists (if not, add file1_currentdate to C:\missingfiles_currentdate.txt)

Check if C:\logs\file3_currentdate exists (if not, add file1_currentdate to C:\missingfiles_currentdate.txt)

Note: currentdate above is a variable for current date

It would be great if the above task can be accomplished through a batch file. However other alternatives as welcome as well.

Many Thanks,

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

This will work

by Bizzo In reply to Check if certain files ex ...

Couple of assumptions on my behalf:

It only checks the current directory.
Checks for files named file9_YYYYMMDD.txt, where 9 is a single digit 1 though 9, YYYY is 4 digit year, MM - month, DD - day.
checks for all files and appends missing filenames to output file files.txt.
If the assumptions are wrong, you should be able to easily change the batch file.

Here it is:

@echo off
Rem Get the current date
for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C

set CurrentDate=%DateYear%%DateMonth%%DateDay%

Rem Assumes file format is file9_YYYYMMDD
Rem where 9 is single digit

for %%i in (1 2 3 4 5 6 7 8 9) do (
if not exist file%%i_%CurrentDate%.txt echo file%%i_%CurrentDate%.txt >> files.txt

Collapse -


by v2.kumar In reply to This will work

Sorry about not stating some details, here they are...

1. I need to use ydate (currentdate-1), and the format is YYMMDD (e.g. 080207)

2. The filenames are not like file9, instead they is something like


3. Batchfile checks directory C:\logs for the above files, and appends missing filenames to \files_ydate.txt (files_ydate.txt doesnt exist at first, would >> create files_ydate.txt the first time and add missing filename to it).

Many thanks,

Collapse -


by Bizzo In reply to Assumptions...

Then you'll need to determine yesterday's date, batch files aren't that good at string processing or arithmetic, but it is possible.

Not really got time to do it, but here's some pointer so you can try it yourself, it'd be good practice.

To covert the 4 char year to 2 (ie remove the first two chars) use:
set Date2Year=%Date4Year:~2,2%

You'll need to subtract 1 from the day, if the day is now 0, subtract 1 from the month, if the month is no 0, subtract 1 from the year and set the month to 12.
If the day is 0, then (you'll need a bit of logic here), set it to the last day of the month you currently have. don't forget to take into account the leap year.

From my previous batch file, replace the for %%i in (1 2 ... with your file checking sequence
if not exist abc_system_%yDate% echo abc_system_%yDate% > files_%ydate%.txt
etc, for all required files.

By the way, yes ">>" will create the file if it doesn't exist.

You may be in the situation where all checked files exist, which will mean that the files_ydate.txt file will not exist.

It might be useful to put the following before the file checks:
echo ***** Starting file checking > files_%ydate%.txt
and then at the end
echo ***** Finishing file checking >> files_%ydate%.txt

This will produce a file for every day.

Alternatively, you may want to use VBscript or similar which will have better string and arithmetic functions.

Good Luck.

Related Discussions

Related Forums