Search This Blog

Sunday, January 10, 2016

Couldn't Grasp The Problem Until

I was spinning my wheels last night. When I run my DOS batch files, I use %TODAY% as a parameter to create a folder with the current date. What I ultimately end up doing, though, is manually creating a sub-folder with the time stamp of the first file that is created and then manually moving the files that were created into it. If I don't do that, I overwrite / replace the first set of TXT files the second time I run the DOS batch file.

So, armed with the knowledge that DOS is all-powerful, I wanted to automatically create folders based upon the date and time so that it would be done automatically. I love automation. Don't make me do something manually when I know there is a way to do it through a DOS command or a macro or something. I want to save time any chance I get.

I started poking around and came across the sites listed below and, armed with that knowledge, I began writing my own commands. What FINALLY got me to a mental breakthrough was when I observed the DOS window as it ran. The first command created a folder with the date and time, the second ran a DIR command that generates a TXT file in that folder. Then it goes to the second DIR command but because "current time" has changed between the first DIR and the second DIR, DOS puked and said, "I can't find a folder that has the "current time of the second DIR command" in it. Then the third DIR command ran and it was looking for a folder with the "current time of the third DIR command" in it, but the only folder that exists is the folder with the "current time of the first DIR command" in it.

What finally got me over the hump was analyzing the DOS commands more closely. These commands are from the second link:

@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%\


Notice how this command - mkdir c:\backup\%folderdate%\%foldertime% - is not done at the beginning of the commands - it's done at the end! A-ha! I had my order reversed. I need to run all the DIR commands I'm going to run and then create a folder with the date and time in it.

This is a work-in-progress, but here are the links:

No comments: