0

I have files that I need to move from an specific directory on schedule.

The files need to move in a folder withing that directory that has all the information separated by the metadata. I have a working Batch that I can run manually, but unable to make it work with scheduler.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%f IN (*.xml) DO (

 (SET destdir=)
 (SET checker=**mydata**)
 (SET fileName=%%f)

 FOR /f "tokens=3 delims=<>" %%i IN (
   'find "<**mydata**>" ^<"%%f"'
   ) DO SET destdir=%%i

 IF NOT DEFINED destdir (
     FOR /f "tokens=3 delims=<>" %%i IN (
       'find "<**mydata**>" ^<"%%f"'
       ) DO SET destdir=%%i
       IF DEFINED destdir (
            IF !destdir! == !checker! (
            
                FOR /f "tokens=4 delims=<>" %%i IN (
                   'find "</**mydata**>" ^<"%%f"'
                   ) DO SET destdir=%%i
                )
            )
        )
    CALL :moveme %%f
    )
)
GOTO :eof

:moveme
set directoryUp=%destdir: =%
set mydate=%date:~4%
set mytime=%time::=%
set mytimestamp=!mydate:/=!!mytime:.=!

MD "ArchivedByOOID\%directoryUp%" 2>NUL

IF EXIST "%~dp0ArchivedBy**MyData**\%directoryUp%\%fileName%" (
        MOVE "%fileName%" "%~dp0ArchivedBy**MyData**\%directoryUp%\%mytimestamp%_%fileName%"
) else (
        MOVE "%fileName%" "%~dp0ArchivedBy**MyData**\%directoryUp%" )
GOTO :eof

3
  • I have no way of testing this, but a very common mistake that beginners make is not realizing that the Task Scheduler starts the script in the C:\Windows\System32 directory and so they need to throw a cd /d "%~dp0" at the start of the script to begin in the correct location. I also see parentheses around your first three set statements that should absolutely be breaking things in a regular execution of the script. Commented Oct 16, 2023 at 19:30
  • Is there a way I can define the actual path of the folder: FOR %%f IN (DIRECTORY/FOLDERPATH) Commented Oct 16, 2023 at 20:15
  • NVM, I just answer my own question. Updated FOR %%f IN ("C:\MYDIRECTORY\*.xml") Commented Oct 16, 2023 at 20:25

1 Answer 1

0

Modified the following to have the directly specified. This allowed BATCH to run the directory needed.

FOR %%f IN (*.xml)

to

FOR %%f IN ("C:\MYDIRECTORY\*.xml")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.