| 1. |
Solve : Help with script involving dates and days of week? |
|
Answer» Hi, Here is the solution. There was small defect.Batch scripting is all but useless in manipulating dates. What happens if the day is 01? The result of SET /A dwDAY=%DATE:~7,2%-1 is that dwDAY will return zero , and if the day is 02 and you "count backwards" for six days, dwDay will return -4. 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. 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. Best have a look at date manipulation using VB scripting. Good luck.C:test>type griff816.bat echo off set /a c=0 :start SET dwMONTH=%DATE:~4,2% echo dwMONTH=%dwMONTH% SET /A dwDAY=%DATE:~7,2% -%c% echo dwDAY=%dwDAY% SET dwDAY=%DATE:~7,2% SET dwYEAR=%DATE:~10,4% SET Datenow=%dwYEAR%%dwMONTH%%dwDAy% set localroot=c:test set localdrv=c:tmp copy %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt copy %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt SET dwMONTH=%DATE:~4,2% SET /A dwDAY=%DATE:~7,2% -%c% echo dwDAY=%dwDAY% if %dwDAY% LEQ 0 ( SET /a dwDAY=31 SET /a dwMONTH=%DATE:~5,1% -1 echo dwMONTH=%dwMONTH% if %dwMONTH% EQU 7 set /a dwDAY=31 if %dwMONTH% EQU 8 set /a dwDAY=31 if %dwMONTH% EQU 6 set /a dwDAY=30 if %dwMONTH% EQU 2 set /a dwDAY=28 echo dwDAY=%dwDAY% ) if %dwMONTH% LEQ 9 set dwMONTH=0%dwMONTH% echo dwMONTH=%dwMONTH% SET dwYEAR=%DATE:~10,4% SET Datenow=%dwYEAR%%dwMONTH%%dwDAy% echo Datenow=%Datenow% if %dwDAY% LEQ 9 set dwDAY=0%dwDAY% copy %localroot%\\FL_%plantid%_*%datenow%.txt %localdrv%\\rs_input\\FL_%plantid%_%datenow%.txt copy %localroot%\\\\D1_%plantid%_*%datenow%.txt %localdrv%\\\\rs_input\\\\D1_%plantid%_%datenow%.txt set /a c=%c% + 1 if %c% LEQ 7 goto :start C:test>Hi Roy, 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 isn't working quite right either. Any thoughts? set /a addzero=0 SET dwMONTH=%DATE:~4,2% SET /A dwDAY=%DATE:~7,2%-14 SET dwYEAR=%DATE:~10,4% if (%dwday%) GEQ (10) ( set /a datenow=%dwYEAR%%dwMONTH%%dwDAy% ) else ( SET /a Datenow=%dwyear%%dwmonth%%addzero%%dwday% ) 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 MANIPULATE 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 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. Wow! No love for Roy? 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 Wow! No love for Roy?No patience for Roy, or any of his other accounts. Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM
Many suggestions from the Ghost and Helpless about what does not work. But no suggestions or examples from the Ghost and Helpless of what can be done. The Cat Bell? Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM
C:test>Display mc.bat echo off SET dwMONTH=%DATE:~4,2% echo dwMONTH=%dwMONTH% SET /A dwDAY=%DATE:~7,2%-16 echo dwDAY=%dwDAY% SET dwYEAR=%DATE:~10,4% echo dwYEAR=%dwYEAR% if %dwday% LEQ 9 set dwday=0%dwday% SET Datenow=%dwyear%%dwmonth%%dwday% echo Datenow=%Datenow% Output: C:test> mc.bat dwMONTH=08 dwDAY=9 dwYEAR=2010 Datenow=20100809 C:test> When dwDAY is not less than or equal to 9, no zero is added. ( We subtracted -14 not -16 ) Output: C:test> mc.bat dwMONTH=08 dwDAY=11 dwYEAR=2010 Datenow=20100811 C:test> Quote from: mcgriff1969 on August 23, 2010, 01:51:44 PM
set /? « Sent to: mcgriff1969 on: Today at 12:14:03 PM » -------------------------------------------------------------------------------- set dwday=0%dwday% set /a dwday=0%dwday% set /a will not work 09 is octal not an integer Quote from: Fields on August 25, 2010, 12:17:56 PM 09 is octal not an integer 09 is not octal, and it is an integer. Quote from: Fields on August 25, 2010, 11:26:00 AM C:test>Display mc.bat I think you've got it! |
|