Reputation: 345
I have a small problem with below batch job execution as shown in the code snippet. The batch job executes just fine when triggered in the directory where it resides (C:\Temp\TestFile). However, I noticed that nothing happens when I schedule the job to execute through task scheduler. The task scheduler uses a service account with 'run whether user is logged in or not' option.
@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"
I believe the service account is not recognizing the directory. However, after I made the change to below, nothing still happens and files that do not meet valid naming conventions as specified in the text file still remain in the same directory and are not moved to the archive folder:
@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"
Your help on this will be greatly appreciated. Looking forward to your suggestions!
Output after running task scheduler:
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"
Upvotes: 0
Views: 224
Reputation: 38719
Here's an example to aid you:
@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")
You may wish to consider whether you're intention is for the task to be run in the same directory as the batch file itself. In that case you would not have a hard coded path, because you could expand the special metavariable for the batch file itself, %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")
Upvotes: 1