使用WMIC的批处理文件不能与TaskScheduler一起正常使用(但是在命令行中可以正常使用!) [英] Batch file using WMIC not working correctly with TaskScheduler (but working fine from command line!)

查看:83
本文介绍了使用WMIC的批处理文件不能与TaskScheduler一起正常使用(但是在命令行中可以正常使用!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定写一个小的批处理文件来每天制作一个文件夹及其内容的副本.我使用WMIC正确格式化日期,以便自动命名文件夹.尽管它在命令行上可以完美运行,但似乎不喜欢由TaskScheduler执行-它确实制作了该文件夹的副本,但没有获取任何日期,并将文件保存到名为-〜的文件夹中-2-〜-2 .似乎完全跳过了代码的第一部分,而直接跳转到数字位部分,从那里它获得了〜-2 位.任何建议都将受到欢迎!

I decided to write a small batchfile to make daily copies of a folder and its contents. I used WMIC to properly format the date in order to name the folders automatically. While it works perfectly on the command line, it seems that it dislikes being executed by the TaskScheduler - it does make a copy of the folder, but doesn't retrieving any date, and saves the files into a folder named -~-2-~-2. It seems to skip the first part of the code altogether, and jump directly to the Pad digits part, from where it gets the ~-2 bit. Any suggestions would be most welcome!

这是我的批处理文件:

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

推荐答案

我建议您使用一种比.

以下示例使用:

The following example uses 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的批处理文件不能与TaskScheduler一起正常使用(但是在命令行中可以正常使用!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆