Append timestamp to copy or move operation?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Append timestamp to copy or move operation?

#1 Post by SIMMS7400 » 17 Oct 2016 13:53

HI Folks -

I have a need to append a timestamp to block of code that does a move and a block of code that does a copy.

I've been able to acheive my goal do if know hte file names, such as this:

Code: Select all


FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

::-- Setting PEMFIN Error Path --::
FOR /D %%f IN (%MAINPATH%%ERRORPATH%PEMFIN_Errors\) DO (
               
    ::-- Setting PEMFIN Error File Names --::
    FOR %%b IN ( PEMFIN_dataload_adminfct.txt PEMFIN_dataload_adminfx.txt  PEMFIN_dataload_empfct.txt  PEMFIN_dataload_empfx.txt  ) DO (
                               
      ::-- Making Error File Archive Directory --::
         IF NOT EXIST "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%" MKDIR "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
                               
        ::-- Copy all *.err files to archive directory and append timestamp --::
        ECHO F | XCOPY /F "%%f%%b" "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%%~nb_%timestamp%%%~xb"

    )
)


But, having trouble adapting these (2) portions of code to use the method above. These are pieces of coee I have in a lot of my scripts:

Code: Select all

FOR %%f IN (%MAINPATH%%LOGPATH%*) DO (
IF NOT EXIST "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" MKDIR "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
MOVE %%f "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)


I also use:

Code: Select all

FOR %%f IN (%MAINPATH%%LOGPATH%*) DO (
IF NOT EXIST "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" MKDIR "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
Copy %%f "%MAINPATH%%LOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)


I believe the wildcards are giving me trouble tying to append a time stamp to them. Thank you!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Append timestamp to copy or move operation?

#2 Post by SIMMS7400 » 17 Oct 2016 16:22

Please ignore I've solved it.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Append timestamp to copy or move operation?

#3 Post by ShadowThief » 17 Oct 2016 18:05

SIMMS7400 wrote:Please ignore I've solved it.

Don't tell us how or anything; other people couldn't possibly also be having this problem.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Append timestamp to copy or move operation?

#4 Post by SIMMS7400 » 18 Oct 2016 10:33

Sorry!

So, I modified the move command with the following syntax:

Code: Select all

FOR %%f IN (%MAINPATH%%ERRORPATH%%GJAERRORPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )   
MOVE /Y %%f "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%%~nf_%timestamp%%%~xf">nul
)


\%%~nf_%timestamp%%%~xf is the trick.

%timestamp% is derived using the following code:

Code: Select all

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%")   do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)


And whether or not your using two loops to generate the path and file names, the function is similar:

Code: Select all

::-- Setting PEMFIN Error Path --::
FOR /D %%f IN (%MAINPATH%%ERRORPATH%PEMFIN_Errors\) DO (
               
    ::-- Setting PEMFIN Error File Names --::
    FOR %%b IN ( PEMFIN_dataload_adminfct.txt PEMFIN_dataload_adminfx.txt  PEMFIN_dataload_empfct.txt  PEMFIN_dataload_empfx.txt  ) DO (
                               
        ::-- Making Error File Archive Directory --::
        IF NOT EXIST "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%" MKDIR "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
                               
        ::-- Copy all *.err files to archive directory and append timestamp --::
        ECHO F | XCOPY /F "%%f%%b" "%MAINPATH%%ERRORPATH%PEMFIN_Errors\%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%%~nb_%timestamp%%%~xb"
      
        ::-- Overwrite current *.err files with blanks --::
        ECHO Y | ECHO >%%f%%b
    )
)


Thanks!

Post Reply