Saved Bookmarks
| 1. |
Solve : Batch file to check when it was lat run?? |
|
Answer» May I but in ? OK, I will anyway. MS-DOS System Date Cannot Have a Year Past 2099It apples to Microsoft MS-DOS 6.22 Standard Edition, so if you really use DOS and not the XP command prompt, you can have a problem. Maybe. I don't know. Quote from: devcom on April 27, 2009, 10:45:57 AM try this: if your date looks like yyyy/mm/dd then change every delims=- to delims=/you could do all of this in vbscript Code: [Select]'lastrun.vbs - check for vbs lastrun and rename a folder if less than 7d f=".\testfolder" with createobject("scripting.filesystemobject") if not .folderexists(f) then wsh.echo f, "not exist":wsh.quit 1 lastrun=now if .fileexists(".\dummy.txt") then lastrun=.getfile(".\dummy.txt").datelastmodified wsh.echo "Last run:",lastrun if datediff("d",now,lastrun)<7 then d=.getbasename(f) & "_" & right(year(date)*10000+month(date)*100+day(date),6) wsh.echo "Less than 7 days, rename to", d .getfolder(f).name = d end if set dummy=.opentextfile(".\dummy.txt",2,true) dummy.writeline now dummy.close end withto run, type at command prompt "lastrun.vbs" use cscript engine: cscript//nologo lastrun.vbs use wscript engine: wscript//nologo lastrun.vbs or if you insist on batch code, i've the pure batch code which is twice the size of vbs code. or you could have the best of both world with the above code by devcomThanks everyone for all your help. I'm gonna go away ant try these out. no doubt, i'll be back if i get confused Hi - back again I am almost there, however there is ONE small problem. The script successfully renames the test folder, however when i run it again it should do the comparison to see when it was last run. It was run less than 7 days ago the 'rename will not be performed' message should display. The script for some reason is not doing this. When i run it for the second time I get the 'duplicate file name exists or location cannot be found' error instead. I think it is because of the "if not" statement that compares the 2 dates to see whether 7 days have elapsed. The 2 variables are in different formats and therefore the statement fall over. i.e. Date = 28/04/09 Date7 - 05052009 Am I correct in this? and how to I get the date.txt in the same format as the date7 variable? thanks Code: [Select] @echo off for /f "tokens=1-3 delims=/ " %%A in ('cscript //nologo evaluate.vbs date') do set yymmdd=%%C%%B%%A set org=test pause if not exist date.txt cscript //nologo evaluate.vbs date >date.txt set /p date=<date.txt for /f "tokens=1-3 delims=/ " %%A in ('cscript //nologo evaluate.vbs date+7') do set date7=%%A%%B%%C if not %date:+=% GEQ %date7:+=% ( echo Rename will not be performed. pause >nul exit ) pause ren %org% %org%_%yymmdd% cscript //nologo evaluate.vbs date >date.txt echo. DONE! pause forgot to told this to you you also NEED to change this: Code: [Select]if not %date:+=% GEQ %date7:+=% ( to Code: [Select]if not %date:/=% GEQ %date7:/=% ( so it can remove it and i don't know why Date & Date7 are in different format... they should be the same as it uses the same command Hi, I'm almost there - so the batch file runs and successfully renames the test folder to test_yyyymmdd If I create another test folder and run the batch file again - it should check if the batch file has been run in the last 7 days and then decide wether to run or not. So in the case of me creating the second test folder, the batch file should not run as i have previosuly run it on the same day. I have tried this and it is not working I get the following error - "A duplicate file name exists, or the file cannot be found" What am I doing WRONG? Code: [Select]@echo off for /f "tokens=1-3 delims=/" %%A in ('cscript //nologo evaluate.vbs date') do set yymmdd=%%C%%B%%A set org=test for /f "tokens=1-3 delims=/" %%A in ('cscript //nologo evaluate.vbs date') do set current=%%A%%B%%C if not exist date.txt echo.%current% >date.txt set /p date=<date.txt for /f "tokens=1-3 delims=/" %%A in ('cscript //nologo evaluate.vbs date+7') do set date7=%%A%%B%%C if not %date:/=% GEQ %date7:/=% ( echo Rename will not be performed. pause >nul exit ) ren %org% %org%_%yymmdd% echo.%current% >date.txt echo. DONE! pause final code i think edit: or no... edit: if i understand you good add this in front of file: Code: [Select]for /f "tokens=*" %%a in ('dir /b ^|findstr "%org%"') do ( if not "%%a" equ "%org%"( echo Error: %%a pause >nul exit ) ) |
|