0
Thanks

A few words of thanks would be greatly appreciated.

Batch Script - Move files to \%date%\%time%\


I have tried to make a simple batch script to create database backups and move them into a folder named by date, and a subfolder by time. This is what I have so far:

---
@echo off 

@echo Backing Up Server1
mysqldump -A -Q -R -c -e --lock-tables=FALSE -uXXXX -pXXXX -hX.X.X.1 > c:\backup\01.sql

@echo Backing Up Server2
mysqldump -A -Q -R -c -e --lock-tables=FALSE -uXXXX -pXXXX -hX.X.X.2 > c:\backup\02.sql

@echo Backing Up Server3
mysqldump -A -Q -R -c -e --lock-tables=FALSE-uXXXX -pXXXX -hX.X.X.3 > c:\backup\03.sql

set folderdate=%date:~7,2%-%date:~4,2%-%date:~10,4%
mkdir c:\backup\%folderdate%
set foldertime=%time:~0,2%-%time:~3,2%
mkdir c:\backup\%folderdate%\%foldertime%
move c:\backup\*.sql c:\backup\%folderdate%\%foldertime%\

---

I have this scheduled to run every 12hours from 8PM. However, while the batch that runs at 8PM is successful, the 8AM files remain unmoved. In fact in the AM, the 'foldertime' folder is created outside the 'folderdate' folder and sites next to the files that have been created but not moved.

And yet, at 8Pm everything works like a charm. Presumably this has to do with using 24hour time, as the folders are names in 24hr, while XP uses AM/PM in the 'modified' column. I do not see any logic that would cause this to happen the way it does, but I hope someone can point me in the right direction.

Solution

Insertthe following lines in your batch file:

set DD=%DATE:~0,2% 
set MM=%DATE:~3,2%
set YY=%DATE:~8,2%
set YYYY=%DATE:~6,4%
set HH=%TIME:~0,2%
set MN=%TIME:~3,2%


You can now string them together in any format you want to create a dated folder name, eg,

set FOLDER=%YYYY%-%MM%-%DD%

echo %FOLDER% will now give you 2009-03-10

I recommend you use this reverse date format as it sorts better.

Remove the space at the end of the line SET YYYY=%DATE:~10,4%

Note

Thanks to Rob for this tip on the forum.
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Published by . Latest update on by Jean-François Pillou.

This document, titled "Batch Script - Move files to \%date%\%time%\," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments