Computing.Net > Forums > Programming > Bat file to check for file with current date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Bat file to check for file with current date

Reply to Message Icon

Name: Candace Bryant
Date: April 28, 2009 at 15:10:53 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 29, 2009 at 04:31:48 Pacific
Reply:

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


0

Response Number 2
Name: Candace Bryant
Date: April 29, 2009 at 13:00:30 Pacific
Reply:

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.


0

Response Number 3
Name: Candace Bryant
Date: April 29, 2009 at 13:34:41 Pacific
Reply:

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 . . .


0

Response Number 4
Name: Mechanix2Go
Date: April 29, 2009 at 13:58:04 Pacific
Reply:

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


0

Response Number 5
Name: Candace Bryant
Date: April 29, 2009 at 14:48:06 Pacific
Reply:

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
)
)
)


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: April 29, 2009 at 15:22:54 Pacific
Reply:

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


0

Response Number 7
Name: Valerie (by Garibaldi)
Date: April 29, 2009 at 16:51:28 Pacific
Reply:

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...


0

Response Number 8
Name: Candace Bryant
Date: April 29, 2009 at 19:15:55 Pacific
Reply:

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


0

Response Number 9
Name: Mechanix2Go
Date: April 29, 2009 at 19:53:05 Pacific
Reply:

@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


0

Response Number 10
Name: Candace Bryant
Date: April 30, 2009 at 12:57:24 Pacific
Reply:

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.


0

Response Number 11
Name: Mechanix2Go
Date: April 30, 2009 at 15:17:10 Pacific
Reply:

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


0

Response Number 12
Name: Candace Bryant
Date: May 5, 2009 at 21:10:38 Pacific
Reply:

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.


0

Response Number 13
Name: Mechanix2Go
Date: May 6, 2009 at 00:46:15 Pacific
Reply:

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


0

Response Number 14
Name: Candace Bryant
Date: May 6, 2009 at 20:20:33 Pacific
Reply:

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.


0

Response Number 15
Name: Candace Bryant
Date: May 6, 2009 at 20:38:28 Pacific
Reply:

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.


0

Response Number 16
Name: Mechanix2Go
Date: May 7, 2009 at 00:22:34 Pacific
Reply:

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


0

Response Number 17
Name: Candace Bryant
Date: May 9, 2009 at 20:49:54 Pacific
Reply:

Hi,

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

thanks again for your help!!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Batch File If contains VBA Cell reference



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Bat file to check for file with current date

Create dir with current date then xcopy to it www.computing.net/answers/programming/create-dir-with-current-date-then-xcopy-to-it/20029.html

list file's current date and time. www.computing.net/answers/programming/list-files-current-date-and-time/11922.html

Bat file to check for multiple files www.computing.net/answers/programming/bat-file-to-check-for-multiple-files/19129.html