使用 WMIC 的批处理文件无法与 TaskScheduler 一起正常工作(但在命令行下工作正常!)
Batch file using WMIC not working correctly with TaskScheduler (but working fine from command line!)
我决定编写一个小的批处理文件来制作文件夹及其内容的每日副本。我使用 WMIC 正确格式化日期以便自动命名文件夹。虽然它在命令行上完美运行,但它似乎不喜欢由 TaskScheduler 执行 - 它确实制作了文件夹的副本,但不检索任何日期,并将文件保存到名为 -~-2-~-2
的文件夹中.它似乎完全跳过了代码的第一部分,直接跳转到 Pad digits
部分,从那里获得 ~-2
位。欢迎提出任何建议!
这是我的批处理文件:
@Echo off
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
:: Display the date/time in ISO 8601 format:
Set _date=%_yyyy%-%_mm%-%_dd%
Echo %_date%
:: Xcopy the files!
xcopy /s /i "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\"%_date%\
GOTO:EOF
:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo:
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.
我建议您使用比 wmic 更快的方法来确定您的日期。
以下示例使用 robocopy:
@Set "_date="
@For /F "Tokens=1-3Delims=/ " %%A In ('^""%__AppDir__%RoboCopy.exe" /NJH /L "\|" Null^"')Do @If Not Defined _date Set "_date=%%A-%%B-%%C"
@"%__AppDir__%XCopy.exe" "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\%_date%\" /S
如果你想从 WMIC
得到 ISODate
,你应该尝试像这样的代码:
@echo off
Title GET ISODATE FROM WMIC
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo Today : %today%
set "year=%MyDate:~2,2%"
set "month=%MyDate:~4,2%"
set "day=%MyDate:~6,2%"
echo %day%%month%%year%
pause
我决定编写一个小的批处理文件来制作文件夹及其内容的每日副本。我使用 WMIC 正确格式化日期以便自动命名文件夹。虽然它在命令行上完美运行,但它似乎不喜欢由 TaskScheduler 执行 - 它确实制作了文件夹的副本,但不检索任何日期,并将文件保存到名为 -~-2-~-2
的文件夹中.它似乎完全跳过了代码的第一部分,直接跳转到 Pad digits
部分,从那里获得 ~-2
位。欢迎提出任何建议!
这是我的批处理文件:
@Echo off
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _yyyy=%%L
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
:: Display the date/time in ISO 8601 format:
Set _date=%_yyyy%-%_mm%-%_dd%
Echo %_date%
:: Xcopy the files!
xcopy /s /i "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\"%_date%\
GOTO:EOF
:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo:
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.
我建议您使用比 wmic 更快的方法来确定您的日期。
以下示例使用 robocopy:
@Set "_date="
@For /F "Tokens=1-3Delims=/ " %%A In ('^""%__AppDir__%RoboCopy.exe" /NJH /L "\|" Null^"')Do @If Not Defined _date Set "_date=%%A-%%B-%%C"
@"%__AppDir__%XCopy.exe" "C:\Users\Alcides\Documents\CQC_APS_TESE_DOUTORAMENTO_LaTeX" "I:\Tese-Backup\%_date%\" /S
如果你想从 WMIC
得到 ISODate
,你应该尝试像这样的代码:
@echo off
Title GET ISODATE FROM WMIC
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo Today : %today%
set "year=%MyDate:~2,2%"
set "month=%MyDate:~4,2%"
set "day=%MyDate:~6,2%"
echo %day%%month%%year%
pause