开发者

What wrong with this batch command in Windows?

开发者 https://www.devze.com 2023-03-14 09:39 出处:网络
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 curr开发者_如何转开发ent year/m
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 curr开发者_如何转开发ent year/month/day


%%~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.


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


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消