views:

2335

answers:

5

Hey Guys,

We have a batch job that runs every day and copies a file to a pickup folder. I want to also take a copy of that file and drop it into an archive folder with the filename

 yyyy-MM-dd.log

Whats the easiest way to do this in a Dos Batch Job...

I'm basically looking for an equivalent of the unix command

cp source.log `date +%F`.log
+1  A: 

Maybe this can help:

echo off
@prompt set date=$d$_ set time=$t$h$h$h
echo some log >> %date% %time%.log
exit

or

echo off
set v=%date%.log
echo some log >> %v%
Secko
Eoin Campbell
set is a "SET variable" statement. Where the date variable is $d$_ (day + the rest) and time variable is $t$h$h$h (time + ms). I tried to show how it can be done with a set of variables it can work fine without the prompt line.I basicly tried to do thisset date=%YYYY%%MM%%DD%but I'm running Linux here and compiling the script on SciTE so I dont know if it works. Sorry if it doesn't work.
Secko
Forgot to add, $_ is newline.
Secko
Also try,@prompt //$d$_ $t$h$h$h$h$h$h//
Secko
+3  A: 
CP source.log %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log

But it's likely locale dependent. I'm not sure if %DATE% is localized, or depends on the format specified for the short date in Windows.

opello
I'd recommend issuing a SETLOCAL command before this.
Philip Kelley
Needed to do a bit of fiddling with the indices but this seems to have done the trick. (though not fully sure I understand the minus-index logic)%DATE:~-4%-%DATE:~-7,-5%-%DATE:~-10,-8%.log
Eoin Campbell
If you wanted to use all positive indices, you could use: %DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%
opello
(I just took this from something else I'd worked on, it was a long time ago, and I have no idea why I did it the way I did originally, heh.)
opello
I't not likely locale-dependent, it's definitely locale-dependent. There is no portable way of getting a date in cmd, unfortunately.
Joey
A: 

Create a file with the current date as filename (ex. 2008-11-08.dat)

echo hello > %date%.dat

With the current date but without the "-" (ex. 20081108.dat)

echo hello > %date:-=%.dat
RealHowTo
A: 

Here is a locale independent solution (copy to a file named SetDateTimeComponents.cmd):

@echo off
REM This script taken from the following URL:
REM http://www.winnetmag.com/windowsscripting/article/articleid/9177/windowsscripting_9177.html

REM Create the date and time elements.
for /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $d $t"') do (
   for /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
      set dow=%%i
      set %%a=%%j
      set %%b=%%k
      set %%c=%%l
      set hh=%%m
      set min=%%n
      set ss=%%o
   )
)

REM Let's see the result.
echo %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss%

I put all my .cmd scripts into the same folder (%SCRIPTROOT%); any script that needs date/time values will call SetDateTimeComponents.cmd as in the following example:

setlocal

@echo Initializing...
set SCRIPTROOT=%~dp0
set ERRLOG=C:\Oopsies.err

:: Log start time
call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul
@echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : Start === >> %ERRLOG%

:: Perform some long running action and log errors to ERRLOG.

:: Log end time
call "%SCRIPTROOT%\SetDateTimeComponents.cmd" >nul
@echo === %dow% %yy%-%mm%-%dd% @ %hh%:%min%:%ss% : End === >> %ERRLOG%

As the example shows, you can call SetDateTimeComponents.cmd whenever you need to update the date/time values. Hiding the time parsing script in it's own SetDateTimeComponents.cmd file is a nice way to hide the ugly details, and, more importantly, avoid typos.

totorocat
A: 

1) you can download GNU coreutils which comes with GNU date

2) you can use vbscript, which makes date manipulation easier in windows.

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder = objFS.GetFolder(strFolder)
current=Now
mth = Month(current)
d = Day(current)
yr=Year(current)
If Len(mth) <2 Then
    mth="0"&mth
End If 
If Len(d) < 2 Then
    d = "0"&d
End If 
timestamp=yr & "-" & mth &"-"& d
For Each strFile In objFolder.Files
    strFileName = strFile.Name
    If InStr(strFileName,"file_name_here") > 0 Then
     BaseName = objFS.GetBaseName(strFileName)
     Extension = objFS.GetExtensionName(strFileName)
     NewName = BaseName & "-" & timestamp & "." & Extension
     strFile.Name = NewName 
    End If 

Next

run the script as c:\test> cscript /nologo myscript.vbs

ghostdog74