2
FOR %%F IN (E:\backups\7DbBackup_local\*.bak) DO IF %%~tF == (time /t) echo %%F

This supposed to print file names if their date modified year/month/day is equal to current year/month/day

eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • well, it just prints everything after DO, point is i don't even know how to get current date for IF condition so i use basic cmd command which obviously doesn't work this way – eugeneK Jun 22 '11 at 07:51
  • If it prints everything after `do` then either prefix commands with `@` or use `echo off`. – Joey Jun 22 '11 at 08:48

3 Answers3

3

%%~t gives you the date and the time of the file, along the lines of:

23/03/2011 04:27 PM

but even this may be locale specific.

On the other hand, time /t gives you 03:57 PM and date /t gives you Wed 22/06/2011, neither of which is useful as a match.

If you wanted to be able to match them, you'd have to do some string trickery to get the date only from %%~t (strip off the time) and date /t (strip off the day name).

Or you could save yourself the hassle, forget about that strange activity I like to call cmd-gymnastics, and use a decent find utility, either by downloading CygWin or using the more lightweight GnuWin32 tool (search for FindUtils).


If you must use cmd, you can start with the following:

@setlocal enableextensions enabledelayedexpansion
@echo off

rem Get todays date

for /f "tokens=1,2,*" %%a in ('date /t') do set today=%%b
echo %today%

rem Process each file in turn.

for %%a IN (.\*) do call :procFile "%%a" %%~ta

endlocal
goto :eof

:procFile
    if %2==%today% echo %2 %1
    goto :eof

But again, keep in mind that this may break if your locale information changes. I wrote a script several years ago which bypassed these programs to get more accurate information using WMI:

for /f "skip=2 tokens=2-7 delims=," %%A in ('wmic path win32_localtime get day^,hour^,minute^,month^,second^,year^ /format:csv') do (
    set /a mydate = 10000 * %%F + 100 * %%D + %%A
    set /a mytime = 10000 * %%B + 100 * %%C + %%E
)
set mydate=00000000%mydate%
set mydate=%mydate:~-8%
set mytime=000000%mytime%
set mytime=%mytime:~-6%

This gives you a date and time of the format YYYYMMDD and HHMMSS but you could adapt it to give other values.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • l33t.... never seen something like this before. Thanks for your time helping me out with this unknown to me subject//// – eugeneK Jun 22 '11 at 08:32
  • `ECHO %DATE%` displays the same as `DATE /T`, which means the date could be extracted from `%DATE%`. So instead of the FOR loop it could be something like `SET today=%DATE:~4%`. – Andriy M Jun 22 '11 at 09:31
  • Um, I meant, of course, the first `FOR` loop in the first script, the one that initialises `today` (just wanted to be clear on that). – Andriy M Jun 22 '11 at 12:37
3

As paxdiablo pointed out, using time in bat is locale specific. But this is not your problem. Your problem is that in windows you can't compare directly with the results of a command.

In case you are wondering, you can't neither compare nor assign nor do anything else, by the way. You need to capture the result with a FOR command, assign it to a environment variable and then use it.

So you need to compare against %date% environment variable. Which is also locale specific. Both ~t and %date% use the same locale, so you need to change the locale prior to executing the commands to a common format that accomodates well to your needs, and bring it back at the end.

Try this...

setlocal enabledelayedexpansion
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
FOR %%F IN (E:\backups\7DbBackup_local\*.bak) DO (
  set dt=%%~tF
  set dt=!dt:~0,10!
  if !dt!==%date% echo %%F %%~tF !dt!
)
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
PA.
  • 28,486
  • 9
  • 71
  • 95
  • I doesn't work, could be because %date% returns Day Name abbr ie. Wed? – eugeneK Jun 22 '11 at 08:28
  • that depends on your locale, if it does not fit your needs, then change it before running the bat file, as indicated in the last lines. I am editing my answer to a better example. – PA. Jun 22 '11 at 08:37
  • Thanks for your solution but @paxdiablo's one works without modifying registry. – eugeneK Jun 22 '11 at 09:12
2

If you're not to concerned about the locale, you can get xcopy to do this for you:

xcopy E:\backups\7DbBackup_local\*.bak %tmp% /d:%date:~4,10% /l
  • %tmp% is your Temp directory; xcopy needs a destination, even when not copying.
  • %date% gives the same output as date /t, but without having to do the for song and dance.
  • %date:~4,10% gets the date mm/dd/yyyy format using batch substrings.
  • /d tells xcopy to get files changed on or after the given date.
  • /l tells xcopy to just list the files that would be copied.
Community
  • 1
  • 1
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • It should probably be `%date:~4,10%` instead of `%date:~5,10%` (it's an offset so it's 0-based). – Andriy M Jun 22 '11 at 11:18
  • I need to compress files based on for loop. Using your approach will force me to create another batch. – eugeneK Jun 22 '11 at 12:10
  • @eugeneK; or, just use this command in your for loop to drive the list of files to compress. – Patrick Cuff Jun 22 '11 at 12:21
  • this version also depends on locale, because %date% does. On my system, %date% returns dd/mm/yyyy; on anothers it might be yymmdd – PA. Jun 22 '11 at 12:35
  • @eugeneK; instead of `FOR %%F IN (E:\backups\7DbBackup_local\*.bak) DO IF...` you can do `FOR /f %%F IN ('xcopy E:\backups\7DbBackup_local\*.bak %tmp% /d:%date:~4,10% /l') DO`. Gets you out of needing the `IF` to check the dates. – Patrick Cuff Jun 22 '11 at 12:39
  • @PA; hence my "If you're not concerned about the locale" statement ;) – Patrick Cuff Jun 22 '11 at 12:40