我在下面的批处理作业执行方面有一个小问题,如代码片段所示。在批处理作业所在的目录(C:\ Temp \ TestFile)中触发时,批处理作业执行得很好。但是,我注意到当我计划通过任务计划程序执行作业时,什么也没有发生。任务计划程序使用带有“无论用户是否登录”运行选项的服务帐户。
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a
set Yr=%DateTime:~0,4%
set Mon=%DateTime:~4,2%
set Day=%DateTime:~6,2%
set Hr=%DateTime:~8,2%
set Min=%DateTime:~10,2%
for /f "delims=" %%I in ('dir /b *.xlsx ^| findstr /vlg:"valid_naming_conventions.txt"') do move "%%I" "C:\Temp\TestFile\Archive\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
我认为该服务帐户无法识别该目录。但是,将更改更改为以下内容后,仍然没有任何反应,并且不符合文本文件中指定的有效命名约定的文件仍保留在同一目录中,并且不会移动到存档文件夹中:
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a
set Yr=%DateTime:~0,4%
set Mon=%DateTime:~4,2%
set Day=%DateTime:~6,2%
set Hr=%DateTime:~8,2%
set Min=%DateTime:~10,2%
set MY_PATH="C:\Temp\TestFile"
for /f "delims=" %%I in ('%MY_PATH% /b *.xlsx ^| findstr /vlg:"valid_naming_conventions.txt"') do move "%%I" "C:\Temp\TestFile\Archive\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
您的帮助将不胜感激。期待您的建议!
运行任务计划程序后的输出:
C:\Windows\system32>rem @echo off
C:\Windows\system32>for /F "delims=" %a in ('wmic OS Get localdatetime | find "."') do set DateTime=%a
C:\Windows\system32>set DateTime=20190414170803.358000+120
C:\Windows\system32>set Yr=2019
C:\Windows\system32>set Mon=04
C:\Windows\system32>set Day=14
C:\Windows\system32>set Hr=17
C:\Windows\system32>set Min=08
C:\Windows\system32>set
MY_PATH="C:\Temp\TestFile"
C:\Windows\system32>for /F "delims=" %I in ('dir /B/A-D "C:\Temp\TestFile"\*.xlsx | findstr /vlg:"valid_naming_conventions.txt"') do move "%I" "C:\Temp\TestFile\Archive\%~nI__2019-04-14%~xI"
C:\Windows\system32>move "01-TestFile.xlsx" "C:\Temp\TestFile\Archive\01-TestFile__2019-04-14.xlsx"
C:\Windows\system32>move "02-TestFile.xlsx" "C:\Temp\TestFile\Archive\02-TestFile__2019-04-14.xlsx"
C:\Windows\system32>move "03-TestFile.xlsx" "C:\Temp\TestFile\Archive\03-TestFile__2019-04-14.xlsx"
C:\Windows\system32>move "04-TestFile.xlsx" "C:\Temp\TestFile\Archive\04-TestFile__2019-04-14.xlsx"
C:\Windows\system32>move "05-TestFile.xlsx" "C:\Temp\TestFile\Archive\05-TestFile__2019-04-14.xlsx"
C:\Windows\system32>move "06-TestFile.xlsx" "C:\Temp\TestFile\Archive\06-TestFile__2019-04-14.xlsx"
答案 0 :(得分:1)
以下是可以帮助您的示例:
@Echo Off
Set "My_Path=C:\Temp\TestFile"
If Not Exist "%My_Path%\*.xlsx" Exit /B
CD /D "%My_Path%"
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
)Do If Not Defined ds Set "ds=%%A-%%B-%%C"
For /F "Delims=" %%A In (
'Dir /B/A-D-L *.xlsx^|FindStr /VLG:"valid_naming_conventions.txt"'
)Do (If Not Exist "Archive\" MD "Archive"
Move /Y "%%A" "Archive\%%~nA__%ds%%%~xA")
您可能希望考虑是否打算将任务与批处理文件本身放在同一目录中运行。在这种情况下,您将没有硬编码的路径,因为您可以为批处理文件本身扩展特殊的元变量%0
:
@Echo Off
If Exist "%~dp0*.xlsx" (CD /D "%~dp0")Else Exit /B
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
)Do If Not Defined ds Set "ds=%%A-%%B-%%C"
For /F "Delims=" %%A In (
'Dir /B/A-D-L *.xlsx^|FindStr /VLG:"valid_naming_conventions.txt"'
)Do (If Not Exist "Archive\" MD "Archive"
Move /Y "%%A" "Archive\%%~nA__%ds%%%~xA")