datetime - Loop through file timestamps, and if it equals to today's date, copy file -
good day all
i have bunch of sql files in folder. loop through each file , timestamp of file. if timestamp mathes current date, want copy file new location.
here directory structure of files:
c:\mfa\mfa_timestamp\mfa_timestamp.sql
and example of file be:
c:\mfa\mfa_20131008\mfa_20131008.sql
so here code have far, not right...
set currentdate=%date% /r c:\mfa\ %%g in (*.sql) set %%g=%file% %%f in (%file%) set filedatetime=%%~tf if %filedatetime:~0, 10% == %currentdate% goto same goto notsame :same copy %file% c:\newlocation goto next :notsame goto end :next
any suggestions on doing wrong here?
thanks
@echo off setlocal enableextensions enabledelayedexpansion set "currentdate=%date:~0,10%" %%g in ("c:\mfa\*.sql") ( set "filedate=%%~tg" set "filedate=!filedate:~0,10!" if "!filedate!"=="%currentdate%" ( copy "%%~fg" "c:\newlocation" ) )
for each .sql file in indicated folder, if file date (10 first characters, i'm assuming date in mm/dd/yyyy variation) equal current date (same amount of characters), copy file new directory
edited - seems 2008 server installation gives problems delayedexpansion. under same asumption (from data in op original question), date in mm/dd/yyyy variation, , since file date in filedatetime returned (as far know) in same format in %date%
variable, "should" work (i hope, previous code "should" work) without delayedexpansion
@echo off setlocal enableextensions disabledelayedexpansion /f %%d in ("%date%") %%a in ("c:\mfa\*.sql") /f %%b in ("%%~ta") ( if "%%b"=="%%d" copy "%%~fa" "c:\newlocation" ) endlocal
it takes first token of data returned '%date%' , each file in indicated set, take first token in file datetime field , check agains splitted current date. if 2 values equal, file copied target.
edited 2 - well, seems not work in 2008 server. next try. without delayed expansion. far %date%
variable returns information in same format file datetime (not sure if can different), , in file datetime date part appears before time part, "maybe" works.
@echo off setlocal enableextensions disabledelayedexpansion set "source=d:\descargas\_work\x" set "target=c:\newlocation" /f "tokens=2 delims=|" %%a in ( 'cmd /q /d /c "for %%f in ("%source%\*.*") echo(%%~tf^|%%~ff"^|findstr /l /i /b /c:"%date%"' ) ( echo copy "%%a" "%target%" )
Comments
Post a Comment