Moving backed-up files to a folder named by date.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ghostcorps
Posts: 1
Joined: 18 Nov 2008 18:29

Moving backed-up files to a folder named by date.

#1 Post by ghostcorps » 18 Nov 2008 19:33

Hi Guys,


I have been trying to make a simple batch script to create database backups and move then into a folder named by date, and a sub folder by time.

This is what I have so far:

Code: Select all

@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.


thanks

=^_^=

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 23 Nov 2008 01:37

ghostcorps,

When using %time:~0,2% you will have a leading space in the hours before 10am. This space confuses the commands that follow because a space serves as argument separator. One way to avoid this problem is to wrap your directory name in quotes, i.e.:

Code: Select all

mkdir "c:\backup\%folderdate%\%foldertime%"
move "c:\backup\*.sql" "c:\backup\%folderdate%\%foldertime%\"

DosItHelp? :wink:

Jedininja
Posts: 25
Joined: 11 Jan 2022 22:41
Location: CanafukpilesDa
Contact:

Re: Moving backed-up files to a folder named by date.

#3 Post by Jedininja » 14 Jan 2022 05:52

will only work within the day

Code: Select all

md c:\temp\temper%date:~-4,4%%date:~-7,2%%date:~0,2%
xcopy /e /i /q /-y drive\path\ c:\temp\temper%date:~-4,4%%date:~-7,2%%date:~0,2%

i got this line

Code: Select all

%date:~-4,4%%date:~-7,2%%date:~0,2%
from microsoft!

from what i understand the numeric values determine the position and leanth of the variables in the date, however it is temperamental and i do not fully understand it. simply add it to a file or folder name.

i would like to point out that it is hard to target a folder dated with this string from program!

also, i have never seen someone actually make this string before.. it saved the world once! (y2k)

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Moving backed-up files to a folder named by date.

#4 Post by atfon » 14 Jan 2022 07:03

The %date% and %time% variables are going to be locale and language specific. For reliability across systems, it is better to use WMIC. You can find several posts on this forum. Here is one example from Steffan (aGerman):

viewtopic.php?t=9348#p60703

Post Reply