1.

Solve : Help with script involving dates and days of week?

Answer» <html><body><p>Hi,<br/><br/>I use a batch program to download files from a server.  The files are named [name]_[date in yyymmdd format].<br/>I wrote the program to download files for the last 7 days starting with today.  I have two problems:<br/>1.  When the program counts backwards I loose the leading zero so 20100804 becomes 201084 which doesn't work.<br/>2.  When I back up against another month it fails also.<br/><br/>Can this be fixed.  Also can the day of the week that corresponds to the date be <a href="https://interviewquestions.tuteehub.com/tag/returned-1187971" style="font-weight:bold;" target="_blank" title="Click to know more about RETURNED">RETURNED</a> as a variable?<br/><br/>Here is the code:<br/><br/>SET dwMONTH=%DATE:~4,2%<br/>SET dwDAY=%DATE:~7,2%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>:gs01<br/>set localroot=[server]<br/>set localdrv=d:<br/>copy %localroot%\FL_%plantid%_*%datenow%.txt %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy %localroot%\D1_%plantid%_*%datenow%.txt %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/><br/>SET dwMONTH=%DATE:~4,2%<br/>SET /A dwDAY=%DATE:~7,2%-1<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>copy %localroot%\FL_%plantid%_*%datenow%.txt %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy %localroot%\D1_%plantid%_*%datenow%.txt %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/>An help is appreciatedHere is the solution. There was small defect.<br/><br/><br/> Code: <a>[Select]</a>SET dwMONTH=%DATE:~4,2%<br/>SET dwDAY=%DATE:~7,2%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>:gs01<br/>set localroot=[server]<br/>set localdrv=d:<br/>copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/><br/>SET dwMONTH=%DATE:~4,2%<br/>SET /A dwDAY=%DATE:~7,2%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/><br/><br/>Checked and <a href="https://interviewquestions.tuteehub.com/tag/verified-2320336" style="font-weight:bold;" target="_blank" title="Click to know more about VERIFIED">VERIFIED</a>. This will work<br/><br/>Please let me know if it all set.<br/><br/>Thanks will do it.<br/><br/><br/>Thanks and regards<br/>vishuHi Vishu,<br/><br/>I apologize for my lack of clarity the line: SET /A dwDAY=%DATE:~7,2%-1 is not really a defect.  It subtracts 1 day from the date.  the program counts <a href="https://interviewquestions.tuteehub.com/tag/backward-391901" style="font-weight:bold;" target="_blank" title="Click to know more about BACKWARD">BACKWARD</a> for 6 days to copy files dated as I stated in the original post.  The subsequent groups of code have: SET /A dwDAY=%DATE:~7,2%-2, SET /A dwDAY=%DATE:~7,2%-1-3, etc. to SET /A dwDAY=%DATE:~7,2%-6.<br/><br/>In the end I want 7 sets of files including the current day. Quote from: vishuvishal on August 15, 2010, 06:10:44 PM</p><blockquote>Here is the solution. There was small defect.<br/><br/><br/> Code: <a>[Select]</a>SET dwMONTH=%DATE:~4,2%<br/>SET dwDAY=%DATE:~7,2%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>:gs01<br/>set localroot=[server]<br/>set localdrv=d:<br/>copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/><br/>SET dwMONTH=%DATE:~4,2%<br/>SET /A dwDAY=%DATE:~7,2%-1<br/>IF /i %dwDAY% LSS 10 set dwday=0%dwDAY%<br/>SET dwday=%0%%dwday%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/>copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt<br/>copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt<br/><br/><br/><br/>Checked and verified. This will work<br/><br/>Please let me know if it all set.<br/><br/>Thanks will do it.<br/><br/><br/>Thanks and regards<br/>vishu<br/></blockquote> Batch scripting is all but useless in manipulating dates.<br/><br/>What happens if the day is 01?  The result of <strong>SET /A dwDAY=%DATE:~7,2%-1</strong> is that dwDAY will return zero , and if the day is 02 and you "count backwards" for six days, dwDay will return -4.<br/><br/>Also, if the day is 08 or 09 Set /a will treat the values as Octal and throw up the usual error message because 08 and 09 are invalid in octal.<br/><br/>If the script is run near the beginning of the month, month and day must be decremented.  If you run the script in early January the day, month and year must be decremented.<br/><br/>Best have a look at date manipulation using VB scripting.<br/><br/>Good luck.C:test&gt;type griff816.bat<br/>echo off<br/>set /a c=0<br/>:start<br/>SET dwMONTH=%DATE:~4,2%<br/>echo dwMONTH=%dwMONTH%<br/>SET /A dwDAY=%DATE:~7,2% -%c%<br/>echo dwDAY=%dwDAY%<br/>SET dwDAY=%DATE:~7,2%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/><br/><br/>set localroot=c:test<br/>set localdrv=c:tmp<br/>copy  %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt<br/>copy  %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt<br/><br/><br/>SET dwMONTH=%DATE:~4,2%<br/>SET /A dwDAY=%DATE:~7,2% -%c%<br/>echo dwDAY=%dwDAY%<br/>if %dwDAY% LEQ 0 (<br/>SET /a dwDAY=31<br/>SET /a dwMONTH=%DATE:~5,1% -1<br/>echo dwMONTH=%dwMONTH%<br/>if %dwMONTH% EQU 7 set /a dwDAY=31<br/>if %dwMONTH% EQU 8 set /a dwDAY=31<br/>if %dwMONTH% EQU 6 set /a dwDAY=30<br/>if %dwMONTH% EQU 2 set /a dwDAY=28<br/>echo dwDAY=%dwDAY%<br/>)<br/>if %dwMONTH% LEQ 9 set dwMONTH=0%dwMONTH%<br/>echo dwMONTH=%dwMONTH%<br/>SET dwYEAR=%DATE:~10,4%<br/>SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/>echo Datenow=%Datenow%<br/>if %dwDAY% LEQ 9 set dwDAY=0%dwDAY%<br/><br/>copy %localroot%\\FL_%plantid%_*%datenow%.txt %localdrv%\\rs_input\\FL_%plantid%_%datenow%.txt<br/>copy %localroot%\\\\D1_%plantid%_*%datenow%.txt %localdrv%\\\\rs_input\\\\D1_%plantid%_%datenow%.txt<br/>set /a c=%c% + 1<br/>if %c% LEQ 7 goto :start<br/><br/><br/>C:test&gt;Hi Roy,<br/><br/>I tried your code but it placed a zero in front all dates even if there were <a href="https://interviewquestions.tuteehub.com/tag/already-1974237" style="font-weight:bold;" target="_blank" title="Click to know more about ALREADY">ALREADY</a> 2 digits.  But I like your logic here.  I'm trying this but it isn't working quite right either. Any thoughts?<br/><br/>set /a addzero=0<br/>SET dwMONTH=%DATE:~4,2%<br/>SET /A dwDAY=%DATE:~7,2%-14<br/>SET dwYEAR=%DATE:~10,4%<br/>if (%dwday%) GEQ (10) (<br/>set /a datenow=%dwYEAR%%dwMONTH%%dwDAy%<br/>) else (<br/>SET /a Datenow=%dwyear%%dwmonth%%addzero%%dwday%<br/>)<br/><br/>echo %datenow%here comes the expert Roy. Don't worry mcgriff1969, Roy will solve your problem using batch. As for my recommendation, i would suggest you use some other tools able to <a href="https://interviewquestions.tuteehub.com/tag/manipulate-1086088" style="font-weight:bold;" target="_blank" title="Click to know more about MANIPULATE">MANIPULATE</a> dates better, eg vbscript. you can search the forum as there are many such vbscripts lying around. Quote from: ghostdog74 on August 23, 2010, 07:10:28 PM<blockquote>here comes the expert Roy. Don't worry mcgriff1969, Roy will solve your problem using batch. As for my recommendation, i would suggest you use some other tools able to manipulate dates better, eg vbscript. you can search the forum as there are many such vbscripts lying around.<br/></blockquote> <br/>Wow!  No love for Roy?  <br/><br/> I understand that VB Script is better for this but I have zero knowledge of it. Quote from: mcgriff1969 on August 25, 2010, 08:31:58 AM<blockquote>Wow!  No love for Roy?  <br/></blockquote> No patience for Roy, or any of his other accounts. Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM<blockquote><br/>I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  I\'m trying this but it isnt working quite right either. Any thoughts?<br/><br/>echo %datenow%<br/></blockquote> <br/>Many suggestions from the Ghost and Helpless about what does not work.<br/><br/>But no suggestions or examples from the Ghost and Helpless of what can be done.<br/><br/>The Cat Bell? Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM<blockquote><br/>I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  Im trying this but it isnt working quite right either. <br/><br/>Any thoughts?<br/><br/></blockquote> <br/>C:test&gt;Display  mc.bat<br/><br/>echo off<br/>SET dwMONTH=%DATE:~4,2%<br/>echo dwMONTH=%dwMONTH%<br/>SET /A dwDAY=%DATE:~7,2%-16<br/>echo dwDAY=%dwDAY%<br/>SET dwYEAR=%DATE:~10,4%<br/>echo dwYEAR=%dwYEAR%<br/>if %dwday% LEQ 9 set dwday=0%dwday%<br/>SET  Datenow=%dwyear%%dwmonth%%dwday%<br/><br/>echo Datenow=%Datenow%<br/><br/>Output:<br/><br/>C:test&gt; mc.bat<br/>dwMONTH=08<br/>dwDAY=9<br/>dwYEAR=2010<br/>Datenow=20100809<br/><br/>C:test&gt;<br/><br/>When dwDAY is not less than or equal to 9, no zero is added. ( We subtracted -14 not -16 )<br/><br/>Output:<br/><br/>C:test&gt; mc.bat<br/>dwMONTH=08<br/>dwDAY=11<br/>dwYEAR=2010<br/>Datenow=20100811<br/><br/>C:test&gt;<br/><br/> Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM<blockquote><br/><br/>I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  Im trying this but it isnt working quite right either. Any thoughts?<br/><br/></blockquote> <br/>set /? <br/>« Sent to: mcgriff1969 on: Today at 12:14:03 PM »      <br/><br/>--------------------------------------------------------------------------------<br/><br/> set dwday=0%dwday%<br/><br/> set /a dwday=0%dwday%<br/><br/>set /a  will not  work 09 is octal not an integer Quote from: Fields on August 25, 2010, 12:17:56 PM<blockquote>09 is octal not an integer<br/></blockquote> <br/>09 is not octal, and it is an integer.<br/> Quote from: Fields on August 25, 2010, 11:26:00 AM<blockquote>C:test&gt;Display  mc.bat<br/><br/>echo off<br/>SET dwMONTH=%DATE:~4,2%<br/>echo dwMONTH=%dwMONTH%<br/>SET /A dwDAY=%DATE:~7,2%-16<br/>echo dwDAY=%dwDAY%<br/>SET dwYEAR=%DATE:~10,4%<br/>echo dwYEAR=%dwYEAR%<br/>if %dwday% LEQ 9 set dwday=0%dwday%<br/>SET  Datenow=%dwyear%%dwmonth%%dwday%<br/><br/></blockquote> <br/><br/>I think you've got it!</body></html>


Discussion

No Comment Found