Space-Time Continuum Problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Space-Time Continuum Problem

#1 Post by mjfoxtrot » 31 Jul 2014 01:42

Okay, so this is not really a true time-space continuum problem, but the issue I am having does involve a batch file with time references and spaces that foul them up.

Here's the issue: I run a simple batch file to make some critical backups of mainly text and zip files. The script works well and makes the backups to a folder that is named for the day-month-year-time; so the created folder (for example) will be named something like this: 07-31-2014-15.16. This gives me a unique folder for every backup that I create.

But I keep running into the darn blank line problem when I do the backup before 10 a.m., as Windows sees the leading blank space as an invalid character. For instance, if I run the backup at 9:37 a.m., the batch file wants to create the folder like this: 07-31-2014- 9.37. And the script fails to create the backed up files and folders because of the space before the 9.

I think there is a way to use the "WMIC OS Get LocalDateTime" method to solve this problem, but I just can't get it to work. If it helps, here is the complete script that I run:

Code: Select all

for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%C
set DateMonth=%%B
set DateYear=%%D
)

for /F "tokens=1-4 delims=/ " %%F in ('time/t') do (
set DateTime=%%F
)

set CurrentDate=%DateMonth%-%DateDay%-%DateYear%-%time:~0,2%.%time:~3,2%

xcopy c:\1DOS\factory c:\zBackup\%CurrentDate%\Boxes_and_Pfiles\ /s >> c:\log.txt

xcopy x:\Emulators\DosBox\dosroot\apba\apba20\BB\GAME\2014.ddr c:\zBackup\%CurrentDate%\Statmaster_Org\ /s /t

xcopy x:\Emulators\DosBox\dosroot\apba\apba20\BB\GAME\2014.ddr\SMO00003.T13\*.* c:\zBackup\%CurrentDate%\Statmaster_Org\SMO00003.T13\ >> c:\log.txt

Xcopy c:\1DOS\special\score.txt c:\zBackup\%CurrentDate%\ >> c:\log.txt

mkdir c:\zBackup\%CurrentDate%\Game_Summaries

xcopy c:\apbaz\2014\games\al_sum.txt c:\zBackup\%CurrentDate%\Game_Summaries\ >> c:\log.txt

xcopy c:\apbaz\2014\games\nl_sum.txt c:\zBackup\%CurrentDate%\Game_Summaries\ >> c:\log.txt

ren c:\zBackup\%CurrentDate%\score.txt %CurrentDate%_Results.txt

move c:\log.txt c:\zBackup\%CurrentDate%\

ren c:\zBackup\%CurrentDate%\log.txt %CurrentDate%_Log.txt



Any help would be sincerely appreciated. Again, the script works well, just not between the hours of 1 and 10 a.m.

MOD EDIT: added code tags

einstein1969
Expert
Posts: 776
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Space-Time Continuum Problem

#2 Post by einstein1969 » 31 Jul 2014 02:51

try this:

Code: Select all

set T=%time%
set T=%T: =0%
echo %T%


einstein1969

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Space-Time Continuum Problem

#3 Post by foxidrive » 31 Jul 2014 03:50

This is a far more robust way to get the date and time into a variable.

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

Squashman
Expert
Posts: 4178
Joined: 23 Dec 2011 13:59

Re: Space-Time Continuum Problem

#4 Post by Squashman » 31 Jul 2014 06:26

mjfoxtrot wrote:Any help would be sincerely appreciated. Again, the script works well, just not between the hours of 1 and 10 a.m.

Because you are not putting any of your file paths in quotes. You need quotes when there are spaces in your file names or paths. You should get used to using quotes ALL the time regardless of you thinking you will never need them for your file names or file paths.

Not sure why you are not using them. In your two previous threads the code you were using had them.

mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Re: Space-Time Continuum Problem

#5 Post by mjfoxtrot » 31 Jul 2014 10:16

All three solutions posted here solved my problem. That's what I call service ;) . . . thanks very much to all three responders.

Foxidrive, I definitely think the 'wmic OS Get localdatetime' method is the most elegant way to deal with the problem, and it worked like a charm. That's a keeper and I'll be using that for a long time.

Squashman, you're right about the quotes. It was just laziness on my part not to use them, because I figured, "why put the quotes around the directories if there are no spaces in the directory names?" Well, obviously my problem illustrated why - the code I made put spaces into the directories, and fouled things up because I hadn't bothered to use the quotes. So, I will always use the quotes from now on.

This is a great forum.

Compo
Posts: 497
Joined: 21 Mar 2014 08:50

Re: Space-Time Continuum Problem

#6 Post by Compo » 31 Jul 2014 14:56

Here's a batch file utilising powershell:

Code: Select all

@Echo Off & SetLocal
For /F "Tokens=*" %%A In ('PowerShell -C "&{Get-Date -f "MM-dd-yyyy-HH.mm"}"'
   ) Do Set "d8ime=%%A"
Echo(%d8ime%
>Nul Timeout 5

Post Reply