Bat file to check for file with current date

April 28, 2009 at 15:10:53
Specs: Windows XP
I need to write a bat file to check to see if a file is present with the current date. the file name will always be the same, except it will be followed by _042809 (where 042909 will always reflect the current date. I then need the script to unzip it. so, a rough idea would be :

Z:
cd Z:\test_folder

if !filedate! == %DATE% (
PKUNZIP -o image_042809.zip Z:\test_folder\

copy z:\test_folder\image_042809.zip z:\test_folder\image_042809_bak.txt

if errorlevel == 1 goto err2

exit 0

:err1
echo "Job failed - Unable to map the Z drive" >> C:\test.log
date /t >> C:\test1\test.log
exit 1

:err2
echo "Job failed - Unable to copy the file" >> C:\test1\test.log
date /t >> C:\test1\test.log
exit 2



See More: Bat file to check for file with current date

Report •


#1
April 29, 2009 at 04:31:48
The script below shows how to find today's image_*.zip if there is one. I'll leave it to you to hammer out the rest.

I can't think why you'd rename a zip to a txt, but it's your script.

====================================

:: find today's image_*.zip

@echo off > %temp%\# & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=" %%a in ('dir/s/b %temp%\#') do (
set datetime=%%~Ta
)

echo datetime is !datetime!

for /f "tokens=1 delims= " %%a in ("!datetime!") do (
set today=%%a
)

echo today is !today!

for /f "tokens=* delims=" %%a in ('dir/b image_*.zip') do (
for /f "tokens=1 delims=" %%i in ("%%a") do (
set zipdtime=%%~Ti
)
echo zipdtime=!zipdtime!

for /f "tokens=1 delims= " %%j in ("!zipdtime!") do (
set zipdate=%%j
)

echo zipdate=!zipdate!

if !zipdate! equ !today! (
echo %%a is today's zip
)
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
April 29, 2009 at 13:00:30
Hi, Thanks for that quick reply, and you are right, I do not want to rename a zip with a .txt extension. That was a cut and paste error :-). I wlll be trying this out soon here and will let you know.
Thanks again.

Report •

#3
April 29, 2009 at 13:34:41
Okay, i tried that and this is my output when the zip file is in that directory:

datetime is 04/29/2009 03:24 PM
today is 04/29/2009
Press any key to continue . . .


BUT...
this is the output I get when there is not zip file

datetime is 04/29/2009 03:27 PM
today is 04/29/2009
Press any key to continue . . .


Report •

Related Solutions

#4
April 29, 2009 at 13:58:04
I don't know why you get a pause. Better post your bat.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
April 29, 2009 at 14:48:06
Sorry, I put a pause in there because I wasn't getting output at all, screen just flashed. This is what I used.

@echo off > %temp%\# & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=" %%a in ('dir/s/b %temp%\#') do (
set datetime=%%~Ta
)

echo datetime is !datetime!

for /f "tokens=1 delims= " %%a in ("!datetime!") do (
set today=%%a
)

echo today is !today!
pause
for /f "tokens=* delims=" %%a in ('dir/b image_*.zip') do (
for /f "tokens=1 delims=" %%i in ("%%a") do (
set zipdtime=%%~Ti
)
echo zipdtime=!zipdtime!

for /f "tokens=1 delims= " %%j in ("!zipdtime!") do (
set zipdate=%%j
)

echo zipdate=!zipdate!

if !zipdate! equ !today! (
echo %%a is today's zip
)
)
)


Report •

#6
April 29, 2009 at 15:22:54
Without the pause it does just what I said:

datetime is 30-04-09 05:21
today is 30-04-09
zipdtime=30-04-09 05:01
zipdate=30-04-09
image_1.zip is today's zip


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
April 29, 2009 at 16:51:28
I throw this in as an alternative. PKUNZIP is not installed in my system so can't assist with that, have done a simple Copy operation only.

Code begins...
@echo off
setlocal
cls

:: Get todays date in the format mmddyy regardless of the system date format

set newfile=%temp%\date.vbs
echo otherdate = (Date())>%newfile%
echo   yy = datePart("yyyy", otherdate)>>%newfile%
echo   mm = datePart("m"   , otherdate)>>%newfile%
echo   dd = datePart("d"   , otherdate)>>%newfile%

echo wscript.echo yy^&" "^&mm^&" "^&dd>>%newfile%

FOR /F "tokens=1-3" %%A in ('cscript //nologo %newfile%') do (
        set year=%%A
        set month=%%B
        set day=%%C

)
del %temp%\date.vbs

        set year=%year:~2%
        if %month% lss 10 set month=0%month%
        if %day%   lss 10 set day=0%day%

set today=%month%%day%%year%

:: Echo today's date for confirmation only

echo.&echo.&echo.
echo Today's date is %today%
echo.&echo.&echo.

pushd z:\trial_folder\

set filename=image_%today%

if not exist %filename%.zip (
   echo File %filename%.zip does not exist & goto finish
   ) else (
   copy %filename%.zip %filename%.bak > nul
   if %errorlevel% gtr 0 echo %filename%.zip not copied
)

:finish
popd

:: Code ends...


Report •

#8
April 29, 2009 at 19:15:55
Hi, Thanks again. Your original code does work, but I can only see the results if I put a pause at the end. The reason I was only seeing the first few lines was due to where I had placed the pause. I added the rename part, so now I just need to figure out how to add the unzip part. Any ideas on how I can go about getting help with that? Do I need to create an additional post? I am new to this so not sure how it works yet. Thank you so much for your help!!! This is what I have now so far that works:

@echo off > %temp%\# & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=" %%a in ('dir/s/b %temp%\#') do (
set datetime=%%~Ta
)

echo datetime is !datetime!

for /f "tokens=1 delims= " %%a in ("!datetime!") do (
set today=%%a
)

echo today is !today!

for /f "tokens=* delims=" %%a in ('dir/b image_*.zip') do (
for /f "tokens=1 delims=" %%i in ("%%a") do (
set zipdtime=%%~Ti
)
echo zipdtime=!zipdtime!

for /f "tokens=1 delims= " %%j in ("!zipdtime!") do (
set zipdate=%%j
)

echo zipdate=!zipdate!

if !zipdate! equ !today! (
echo %%a is today's zip
)
)
)

copy image_042909.zip image_042909_bak.zip
pause


Report •

#9
April 29, 2009 at 19:53:05
@echo off > %temp%\# & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=" %%a in ('dir/s/b %temp%\#') do (
set datetime=%%~Ta
)

echo datetime is !datetime!

for /f "tokens=1 delims= " %%a in ("!datetime!") do (
set today=%%a
)

echo today is !today!

for /f "tokens=* delims=" %%a in ('dir/b/od image_*.zip') do (
for /f "tokens=1 delims=" %%i in ("%%a") do (
set zipdtime=%%~Ti
)
echo zipdtime=!zipdtime!

for /f "tokens=1 delims= " %%j in ("!zipdtime!") do (
set zipdate=%%j
)

echo zipdate=!zipdate!

if !zipdate! equ !today! (
echo %%a is today's zip
copy %%a %%~Na_bak.zip
)
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
April 30, 2009 at 12:57:24
Thanks, now, how woud we reverse this so I can send an alert if the file for the current date is not there instead. Do you know what the syntax is for not equal? I had no idea in the beginning that this was going to be so difficult. I will try posting a seperate request for unzipping a file I guess.

Report •

#11
April 30, 2009 at 15:17:10
Good idea to post the unzip separately. For the alert, in the last chunk in the script, replace this:
=================================
if !zipdate! equ !today! (
echo %%a is today's zip
copy %%a %%~Na_bak.zip
===========================
with [line breaks and () are critical]
============================
if !zipdate! neq !today! (
echo %%a is NOT today's zip
pause


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#12
May 5, 2009 at 21:10:38
Hi, thanks so much for all your help, but...have another question, this works great with equ or neq, but i need to combine so that if it is there it will rename, but if it is not there it will echo 'not there'. actually what i have it doing is utilizing blat to send an email if it is not there. Using an else statement is not working for me.

Report •

#13
May 6, 2009 at 00:46:15
I'm not going back through a few 100 lines. But, in gereral:

============== code scrap
if a equ a (
echo yes
) else (
echo no
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#14
May 6, 2009 at 20:20:33
Don't know how to thank you!! that should be all I need! I didn't think you could use else in a bat file.

Report •

#15
May 6, 2009 at 20:38:28
Hi, I did try that and I do get an error that else is not recognized as an internal or external command and then it executes both commands.

Report •

#16
May 7, 2009 at 00:22:34
That error is probably caused by mismatched () or line breaks.

I suggest you start a new thread, since this is pretty messy. Post the EXACT code and make clear what is code and what is comment.

best regards


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#17
May 9, 2009 at 20:49:54
Hi,

It was as you said, line break and mismatched ()

thanks again for your help!!


Report •


Ask Question