1.

Solve : datestamp help?

Answer» <html><body><p>hi all - I need a batch file to search in a directory. If all the files have the current date stamp then execute command1 if files are not current then execute command2.<br/>thank you.This can be done via Perl or a number of other languages which have abilities to compare <a href="https://interviewquestions.tuteehub.com/tag/strings-13382" style="font-weight:bold;" target="_blank" title="Click to know more about STRINGS">STRINGS</a>, but I cant see doing this in raw batch. Quote from: DaveLembke on September 21, 2010, 10:10:00 PM</p><blockquote>This can be done via Perl or a number of other languages which have abilities to compare strings, but I cant see doing this in raw batch.<br/></blockquote> <br/>It is a commonplace everyday task for batch. It can easily be done... using FOR and the ~t variable modifier you can get the "date" (created or last modified?) of each file, and it is trivially easy to get today's date. <br/><br/>However we do not know the OP's local date format or the format returned on his system by ~t<br/><br/>The output of this script will <a href="https://interviewquestions.tuteehub.com/tag/tell-1240910" style="font-weight:bold;" target="_blank" title="Click to know more about TELL">TELL</a> us those things.<br/><br/> Code: <a>[Select]</a>echo off<br/>echo local date format [%date%]<br/>echo.&gt;temp$$$<br/>for /f %%A in ("temp$$$") do echo file date  format [%%~tA]<br/>del temp$$$<br/>pause<br/><br/><br/>For example, I get this<br/><br/> Code: <a>[Select]</a>local date format [22/09/2010]<br/>file date  format [22/09/2010 07:46 AM]<br/>But I am worried about this in the question<br/><br/> Quote<blockquote>I need a batch file to search in a directory. If<em><strong> all the files have the current date stamp</strong></em> then execute command1 if files are not current then execute command2.<br/>thank you.</blockquote> <br/>All the files? What if 99 do and 1 does not? Likewise for the converse situation.<br/><br/>these files will <a href="https://interviewquestions.tuteehub.com/tag/always-373607" style="font-weight:bold;" target="_blank" title="Click to know more about ALWAYS">ALWAYS</a> have the same date. I guess we can look in one particular file to check for the date.<br/>thanks Quote from: x3g on September 22, 2010, 07:58:01 AM<blockquote>these files will always have the same date. I guess we can look in one particular file to check for the date.<br/>thanks<br/></blockquote> <br/>So did you run my script?<br/>your script works good but it is missing the logic I need.<br/> Quote from: x3g on September 22, 2010, 09:44:19 AM<blockquote>your script works good but it is missing the logic I need.<br/><br/></blockquote> <br/>I asked you to run the script so I could see your local date format. Then I can make a script for you with the logic that you need.<br/>ah here it is<br/>local date format [Wed 09/22/2010]<br/>file date  format [09/22/2010 11:14 AM]<br/><br/>thank you. Code: <a>[Select]</a>echo off<br/>setlocal enabledelayedexpansion<br/><br/>REM Of course you <a href="https://interviewquestions.tuteehub.com/tag/understand-720010" style="font-weight:bold;" target="_blank" title="Click to know more about UNDERSTAND">UNDERSTAND</a> you need to edit this<br/>set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"<br/><br/>set pdate=%date%<br/>set today=%pdate:~4,10%<br/><br/>REM get file date of latest file in folder<br/>for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (<br/> set fdate=%%~tA<br/> set fdate=!fdate:~0,10!<br/> )<br/><br/>if "%fdate%"=="%today%" (<br/> goto yes<br/>) else (<br/> goto no<br/> )<br/><br/>:yes<br/>echo YES!!!<br/>rem Code to execute if file date is today<br/>goto next<br/><br/>:no<br/>rem Code to execute if file date is not today<br/>echo NO!!!<br/><br/>:next<br/>REM whatever happens next...<br/><br/>pause<br/><br/><br/><br/> <br/> <br/> this looks very promising. I'll let you know my feedback once I've tested.<br/>Thank you.<br/>the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic. Quote from: x3g on September 22, 2010, 01:13:29 PM<blockquote>the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic.<br/></blockquote> <br/>are you quite sure about this? Because the test would be<br/><br/>if "09/21/2010"=="09/22/2010" <br/><br/>which should fail and therefore the else <a href="https://interviewquestions.tuteehub.com/tag/clause-246677" style="font-weight:bold;" target="_blank" title="Click to know more about CLAUSE">CLAUSE</a> gets triggered.<br/><br/>Could you insert these diagnostic lines shown in red?<br/><br/><br/>echo off<br/>setlocal enabledelayedexpansion<br/><br/>REM Of course you understand you need to edit this<br/>set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"<br/><br/>set pdate=%date%<br/>set today=%pdate:~4,10%<br/><br/>REM get file date of latest file in folder<br/>for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (<br/>        set fdate=%%~tA<br/>        set fdate=!fdate:~0,10!<br/>        )<br/><br/><br/><br/><strong><br/>echo today is %today%<br/>echo fdate is %fdate%<br/>echo comparing [%fdate%] with [%today%]<br/></strong><br/><br/>if "%fdate%"=="%today%" (<br/>        goto yes<br/>) else (<br/>        goto no<br/>        )<br/><br/>:yes<br/>echo YES!!!<br/>rem Code to execute if file date is today<br/>goto next<br/><br/>:no<br/>rem Code to execute if file date is not today<br/>echo NO!!!<br/><br/>:next<br/>REM whatever happens next...<br/><br/>pause<br/><br/><br/>even though the files are dated 9/21/2010 fdate still shows 9/22<br/><br/>today is 09/22/2010<br/>fdate is 09/22/2010<br/>comparing [09/22/2010] with [09/22/2010]<br/>YES<br/>Press any key to continue . . .<br/><br/>are you quite sure there is no file dated 09/22/2010 in that folder, including hidden files?<br/>I replaced the 9/21 files with some 9/14 &amp; 9/20 files and get this <br/><br/>today is 09/22/2010<br/>fdate is ~0,10<br/>comparing [~0,10] with [09/22/2010]<br/>NO<br/>Press any key to continue . . .</body></html>


Discussion

No Comment Found