-4

I have the folowing line in my batch script and try to use dot (".") instead of hypen ("-") between dates. However, while the following is working (with "-"):

Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%

when I change to dot ("."), it does not working:

Set TODAY=%date:~4,2%.%date:~7,2%.%date:~10,4%

I tried several escape character before dots e.g. \., but does not worked. Any idea?

Update: Here is the line that uses TODAY variable:

rar a -r -ep1 %backup%\"%name%_%today%" %folder%

UPDATE: Here is the code that I use:

@echo off

setlocal

REM Specify the folder to compress below:

REM --------------------- Folder to compress -----------------------------------
set dir="D:\project\my-project\"
set name=my-project
set backup="D:\backup\project\"
set folder="D:\dev\project\my-project"
REM If you don't want to include the folder in backup file, add "\" at the end of the directory in folder param

REM ----------------------------------------------------------------------------
REM Path to WinRAR executable in Program Files. Change if location is different
REM ---------------------- WinRAR Directory ------------------------------------
set path="C:\Progra~2\WinRAR\WinRAR.Exe";%path%
REM ----------------------------------------------------------------------------

REM change working dir to dir specified above
cd %dir%

REM Replace space in hour with zero if it's less than 10
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

REM This sets the date like this: mm-dd-yr-hhmmssss (includes 1/100 secs)
Set TODAY=%date:~10,4%-%date:~7,2%-%date:~4,2%_%hr%%time:~3,2%^%time:~6,2%%time:~9,2%

echo Folder to compress in *.RAR format:
echo %folder%
echo.
echo.

echo Compress all files in dir and subdirs into a single archive
echo.
echo.

echo Today's date and time will be added to the base filename

rar a -r -ep1 %backup%\"%name%_%today%" %folder%

pause
goto eof
endlocal

echo.
echo "Task Completed"
echo.

REM --------------------------- exit -----------------------------------------
:end
EXIT /B 0

3 Answers3

1

Attempting to format the %date% variable is not robust at all. You will end up with mangled results once you migrate this to another pc with a different locale, or if by any chance your date format is changed.

a better solution is to simply use powershell to format the result for you.

@echo off
for /f %%i in ('powershell get-date -format "yyyy.mm.dd"') do set "today=%%i"
echo %today%
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

If your currently reported date string when entering echo %date% at the Command Prompt is dd.MM.yyyy and you want it to be yyyy.MM.dd, then the following should do that:

@Set "TODAY=%DATE%"
@Set "TODAY=%TODAY:~-4%%TODAY:~-8,4%%TODAY:~-10,2%"
@Echo %TODAY%

However, as I've already stated in my comments, all now incorrectly deleted, despite nothing in them being unhelpful or rude, that will only be guaranteed to work on any computer which reports the date in a format which ends with dd<separator>MM<separator>yyyy. To get the date in a format which will work on any modern computer, you need to get the assistance of a built-in program or utility. Gerhard has provided a method which uses powershell.exe, so here's one which uses WMIC.exe:

@For /F "Delims==" %%G In ('"(Set strDate) 2>NUL"') Do @Set "%%G="
@For /F "EOL=L" %%G In (
    '%SystemRoot%\System32\wbem\WMIC.exe OS Get LocalDateTime 2^>NUL'
) Do @For %%H In (%%~nG) Do @Set "strDateTime=%%H"
@If Not Defined strDateTime GoTo :EOF
@Set "strDate=%strDateTime:~,4%.%strDateTime:~4,2%.%strDateTime:~6,2%"
@Echo %strDate%

[EDIT /]

If your code changed your date format and defined the new string as %TODAY%, as in your first example command line:

Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%

And you wanted to later in your script change the hyphens in that to periods, you'd then use:

Echo %TODAY:-=.%

or to redifine that variable value string:

Set "TODAY=%TODAY:-=.%"

[EDIT2 /]

Adapting your latest edit, (with all of the bloat removed), and my initial code above to incorporate your required TIME string and using a format like this, yyyy.MM.dd_hh.mm.ss.##, where ## are hundredths of a second, (rounded down):

@Echo Off
SetLocal EnableExtensions

Set "dir=D:\project\my-project"
Set "name=my-project"
Set "backup=D:\backup\project"
Set "folder=D:\dev\project\my-project"
Set "RarPath=C:\Program Files\WinRAR"

CD /D "%dir%" 2>NUL || Exit /B 1

Set "}="
For /F "EOL=L" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Get LocalDateTime 2^>NUL') Do For /F %%H In ("%%G") Do Set "}=%%H"
If Not Defined } Exit /B 1

%RarPath%\rar.exe a -ep1 -r "%backup%\%name%_%}:~,4%.%}:~4,2%.%}:~6,2%_%}:~8,2%.%}:~10,2%.%}:~12,5%" "%folder%"
Pause
Exit /B 0
Compo
  • 36,585
  • 5
  • 27
  • 39
1

I have decided to add this an a new answer, because based upon your more recent edit, none of your attempts at recreating the date format as a variable are necessary. All you need to do is to read and understand the options available for the rar.exe command line utility you are using. Those are available by using its -? option. e.g. rar.exe -?.

Based upon that information, all you need is to use its -ag option:

rar.exe a -agYYYY.MM.DD -ep1 "%backup%\%name%_" "%folder%"

The example above assumes that you have not added a backward slash or filemask to the end of the string you have assigned to %folder%.

If you have assigned a backward slash or filemask to the end of the string value of %folder% then you'd use:

rar.exe a -agYYYY.MM.DD -ep1 -r "%backup%\%name%_" "%folder%"

[EDIT /]

To incorporate the time, as in your latest submission, (but not the pointless and unnecessary hundredths of a second), just expand the -ag option string:

rar.exe a -agMM.DD.YYYY_HHMMSS -ep1 -r "%backup%\%name%_" "%folder%"
Compo
  • 36,585
  • 5
  • 27
  • 39