0

In below code I want to create a daily wise folder with hostname for backup where the date format shall be MM.dd.yyyy.

I'm using set mydate=%date:/=% and Echo %mydate% to get thedate. But I'm getting dd-MM-yyyy format.

As the system format is dd-MM-yyyy %date% not providing desired format.

My script so far:

@echo off

REM Getting Host Name FOR /F "usebackq" %%i IN (hostname) DO SET host=%%i ECHO %host%

REM Getting System Date & Time set mydate=%date:/=% Echo %mydate% set mytime=%time:~-11,2%-%time:~-8,2%-%time:~-5,2% set mytimestamp=%mydate: =%%mytime:.=% echo %mytimestamp% REM This timestamp is used to create folder System Name with date set mynewtimestamp=%host%%mydate: =% echo %mynewtimestamp% Rem Change the WINRAR Installation path & MSSQL OSQL.exe path in below statement, seperated with semicolumn. set path=C:\Program Files\WinRAR;C:\Program Files\Microsoft SQL Server\150\Tools\Binn;C:\WINDOWS\System32;

Rem Change patch in following statement as per Required "BackupDest1" (Centrlise Location) Installation. set BackupDest=D:\Daily_Backup\Backup\

REM CREATING BACKUP WITH RAR FORMAT

rar a -ep1 "%BackupDest%%mytimestamp%_TEST.rar" "C:\Program Files\TEST"

REM MSSQL DB BACKUP

"osql.exe" -E -Q "BACKUP DATABASE [TEST] TO DISK='%BackupDest%%mytimestamp%_MSSQL_TEST.bak' WITH FORMAT" rar a -epl "%BackupDest%%mytimestamp%_SQL_Backup.rar" "%BackupDest%*.bak"

pushd "%BackupDest%" del .bak del .reg md %BackupDest%%mynewtimestamp% 2>NUL ROBOCOPY "D:\Daily_Backup\Backup" "%BackupDest%%mynewtimestamp%" /z /MOV /MT32

help-info.de
  • 2,159

2 Answers2

0

Use a one liner from powershell for this and save yourself the hassle.

Yes, I know you are using batch and not powershell. I shell out to powershell for the finite stuff.. FROM MY BATCH FILE like this..

for /f %%a in ('powershell -Command "Get-Date -format MM.dd.yyyy"') do set datetime=%%a

This is how I would solve this and I am very adept at batch. Sure I could parse the commands from batch but why? In this case, the easiest route is probably also the best route.

0
@echo off && setlocal enabledelayedexpansion

for /f usebackq^tokens^=4delims^=^<^>. %%G in =;(`
    "%__AppDir__%wbem\wmic.exe" OS Get localdatetime /format:xml ^| find "VALUE"
    `);= do set "_dt=%%~G"

set "_BackupDest=D:\Daily_Backup\Backup\"
set "_Rar="c:\program files\winrar\rar.exe""
set "_osql="c:\program files\microsoft sql server\150\tools\binn\osql.exe""
set "_var=%ComputerName%_%_dt:~4,2%/%_dt:~6,2%/%_dt:~0,4%-%_dt:~8,2%-%_dt:~10,2%-%_dt:~12,2%"

%_rar% a -ep1 "%_BackupDest%\%_var%_TEST.rar" "C:\Program Files\TEST"
%_osql% -E -Q "Backup Database [Test] To Disk='%_BackupDest%\%_var%_MSSQL_Test.bak' With Format"
%_rar% a -epl "%_BckupDest%\%_var%_SQL_Backup.rar" "%_BackupDest%*.bak"

pushd "%_BackupDest%" && 2>nul =;( del *.bak *.reg & mkdir "%_BackupDest%\%_var%" 
    Robocopy "D:\Daily_Backup\Backup" "%_BackupDest%\%_var%" /z /mov /mt32
   );= & popd & endlocal  

1. Change your command for obtaining date and time to one that does not suffer changes due to user settings, customizations, language, etc... and compose substrings relevant to the desired layout

2. ComputerName returns the hostname without using a loop, which ultimately only complicates your code for the same result

3. Do not use the path name for the variable name, you can use your command customizations without messing with the system variable (only recommended)

4. Documenting the code is helpful, but yours has a lot of comments for the same actions, summarize your command and your comments.

Io-oI
  • 9,237