BATCH: generate file names using system date & time?

  • Thread starter Thread starter K. Shier
  • Start date Start date
K

K. Shier

i want to do something like XCOPY a folder structure (and its contents) to a
backup location and have it intelligently name the destination based on the
date and time. like '0310071159Backups'

can it be done?

thanks in advance! =)
 
i want to do something like XCOPY a folder structure (and its contents) to a
backup location and have it intelligently name the destination based on the
date and time. like '0310071159Backups'

can it be done?

The .Mount/\Command ".GetLogDate" will CONSISTENTLY display the date as
"yyyymmdd" under NT/2K/XP/K3 regardless of the local date format using no
external utilities. The value is also saved to variable "#LogDate".

".GetLogTime" will CONSISTENTLY display the time as hhmnss, regardless
of the local time format, and save the value to "#LogTime".

In your case, an example would be:

(%.GetLogDate%) & (%.GetLogTime%)
XCopy /s /e /v "Source\*.*" "%#LogDate%_%#LogTime%_Backups"

*******

For color-keyed examples, see
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTime.htm#GuardNotes)

For additional information, see
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogDate.htm)
(http://TheSystemGuard.com/MtCmds/GetValue/GetLogTimeP.htm)

*******
Notes:

1. .Mount/\Commands are constructed using ONLY builtin
commands common to all four platforms (NT/2K/XP/K3).
2. .M/\C's are NOT case sensitive. Mixed case is used
for Visual Clarity only.
3. The (FREE) Advanced NT/2K/XP Command Library (ntlib.cmd)
provides over 50 sample Mount/\Commands to assist with
writing and documenting cross-platform scripts.
You can obtain it at http://ntlib.com.

*******

All the commands internal to Cmd.exe are documented
in "Mounted Help" pages in TheGuardBook at
(http://TheSystemGuard.com/TheGuardBook/CCS-Int).

This includes a color keyed page highlighting the
differences among the internal commands in NT/2K/XP/K3.

The "Common Help" from each OS's help screen is also
available for comparison.

A "Mounted Help" page showing the syntax for Cmd.exe
itself can be found at
(http://TheSystemGuard.com/TheGuardBook/CCS-Ext/Cmd.htm)

*******

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!
 
guard said:
The .Mount/\Command ".GetLogDate" will CONSISTENTLY display the date as
"yyyymmdd" under NT/2K/XP/K3 regardless of the local date format using no
external utilities. The value is also saved to variable "#LogDate".

Hi John ("guard"),

I am in good faith going to reiterate what many of others have already told
you.

Please do not crosspost to newsgroups that do not pertain to the subject
matter and platform in question. In case you don't understand what this
means, alt.msdos.batch is not the place for NT-related shell scripting
using cmd.exe. That should be obvious, but evidently it's not.

You are also cross-posting to other newsgroups, but this is considered bad
etiquette. The question was asked in one newsgroup; please answer it in the
same newsgroup without spamming the others.

Also, in case you haven't noticed already, these newsgroups are not the
place to market your "products." All you are doing is annoying the regulars
and getting a bad name for yourself. Please stop insulting us with your
"rise above the rest" nonsense.

HTH,

Bill
 
This is the script i use to backup our DHCP Logs:

cd\
cd dhcp
set hyphen=-
for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (set year=%%k&
set month=%%i& set day=%%j)
set datevar=%year%%hyphen%%month%%hyphen%%day%
mkdir E:\DhcpLogBackup\%datevar%
copy c:\winnt\system32\dhcp\DhcpSrvLog.*
E:\DhcpLogBackup\%datevar%\DhcpSrvLog.*

It produces folders with the date of backup.

HTH
 
thanks, all, for your responses!

sorry for the FAQ. i knew it must be a popular topic, but 'the faq of all
faqs of all faqs of all faqs'???? i had no idea! ;-)

honestly, i tried to google some examples before i came here, but i didn't
find anything decent. (it's all in the terminology, dont'cha know?)

anyway, THANKS! =)
 
so close.... and yet so far! =)

your help has been invaluable! the 'for' statements that parse the output
of the system date/time commands are pure genius, and would have taken me
days to figure out how to do myself! (all those % signs ... argh! but then
again, it only looks like nonsense because i don't know the language that
well...)

using everyone's examples, i cobbled together the following, which works
*almost* perfectly... the only problem is it only copies the first folder
out of "...\visual studio projects\drs\" and ignores the others. i'm sure
it has something to do with my xcopy flags, but i don't know exactly what
.... (every one of them is set for what, to me, seems like a good reason!)
i've tried flipping them a few different ways, but really don't want to try
every permutation of flags just to find the one that works...

could i trouble any of you for one more ounce of insight? ;-)

THANKS!

(by the way if you're feeling really generous, anyone care to explain what
'echo off&setlocal' does? (i already know what plain old 'echo off'
does...))

-----------------
ps - Phase 2 is coming ... error handling for when the sql server data files
are in use! ;-)


\\\
echo off&setlocal
call :GetDate yy mm dd
call :GetTime hh nn ss tt
set datevar=%yy%%mm%%dd%
set timevar=%hh%%nn%%ss%

mkdir "c:\documents and settings\kris\my documents\DRS Complete
Backups\%datevar%_%timevar%\Database"
copy "\\drsserver\data\*.*" "c:\documents and settings\kris\my documents\DRS
Complete Backups\%datevar%_%timevar%\Database"
xcopy "c:\documents and settings\kris\my documents\visual studio
projects\drs\*.*" "c:\documents and settings\kris\my documents\DRS Complete
Backups\%datevar%_%timevar%\Source\" /S /E /V /H /K /Y
"c:\program files\winzip\wzzip.exe" -r -p -ybc -yp "c:\%datevar%_%timevar%
DRS Complete Backup.zip" "c:\documents and settings\kris\my documents\drs
complete backups\%datevar%_%timevar%\*.*"

goto :EOF

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDate yy mm dd
setlocal ENABLEEXTENSIONS
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetTime hh nn ss tt
setlocal ENABLEEXTENSIONS
for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
set hh=%%a&set nn=%%b&set ss=%%c&set cs=%%d)
if 1%hh% LSS 20 set hh=0%hh%
endlocal&set %1=%hh%&set %2=%nn%&set %3=%ss%&set %4=%cs%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
///
 
K. Shier said:
*almost* perfectly... the only problem is it only copies the first folder
out of "...\visual studio projects\drs\" and ignores the others. i'm sure
it has something to do with my xcopy flags, but i don't know exactly what

Are you absolutely sure about that? If you create a folder called "_test", then
is that the only folder copied (assuming it becomes the 'first')?
'echo off&setlocal' does? (i already know what plain old 'echo off'
does...))

From the GUI help:-

"The ampersand (&) separates multiple commands on one command line". In
ohterwords, all the commands joined by an & are treated as one block of
code. So:-

@echo off & setlocal

is just another way of writing:-

@echo off
setlocal

SETLOCAL basically creates a copy of the current environment and then that copy
becomes the current environment. When the script ends or an ENDLOCAL command
is encountered, the current environment is discarded and the previous restored.

See the GUI help for more info.
echo off&setlocal

You might want to make the above:-

@echo off & setlocal ENABLEEXTENSIONS
xcopy "c:\documents and settings\kris\my documents\visual studio
projects\drs\*.*" "c:\documents and settings\kris\my documents\DRS Complete
Backups\%datevar%_%timevar%\Source\" /S /E /V /H /K /Y

BTW, the /S switch is redundant because of the /E switch. Run 'xcopy/?' for
a list of switches and their descriptions.
 
thanks, Ritchie, for all your help! i appreciate it very much! =)

Ritchie said:
what

Are you absolutely sure about that? If you create a folder called "_test", then
is that the only folder copied (assuming it becomes the 'first')?

tried the "_test" test and it disproved my hypothesis so i just started
messing with a variety of stuff... during my tinkerings, i came across the
real problem.

i noticed that the xcopy operation was erroring out with 'Insufficient
Memory' at a certain point - it was caused by a file with a maximum-length
filename: "030926 UtilitiesForms copied out before attempting to move all
common functions into frmDataEntry - saved in case external versions of
these functions are needed agai.vb" (you can see that as i was typing it,
the final 'n' got cut off!) not sure if it's just the filename causing the
problem or the combined file/path name since the path is a bit lengthy
too... ("c:\documents and settings\kris\my documents\visual studio
projects\drs\drs client\safety\")

either way, i worked around the problem by simply renaming the file to
something shorter, but i would prefer a more 'robust' solution - i.e. a copy
command that doesn't care what my files are named! (i prefer _my_
preferences to dictate how _the OS_ works, not the other way around! (i
swear i can hear the mocking laughter of Mac evangelists even as i write
this...)) admittedly, it's pretty rare that i write a file with the
maximum-length name, but it _does_ happen...

any thoughts? is this a known bug? something fixed in a SP? (this machine
is only SP2 for reasons only my sys admin could explain...)

or maybe it's really out of memory?! it only has 256 megs, after all! ;-)

"The ampersand (&) separates multiple commands on one command line". In
ohterwords, all the commands joined by an & are treated as one block of
code...

SETLOCAL basically creates a copy of the current environment and then that copy
becomes the current environment. When the script ends or an ENDLOCAL command
is encountered, the current environment is discarded and the previous
restored.

i c - thanks! i don't really see the advantage of joining the first two
commands on one line in this case, but now at least i know what it does.
and as for 'environments' - sadly, i don't even know exactly what that is...
(my best guess is that it's a 'set' of env. variables that you can address
by name.) i don't use either of these things on a daily basis (or even
command line in general) but it's helpful to know what these things are when
i do run across them!

BTW, the /S switch is redundant because of the /E switch. Run 'xcopy/?' for
a list of switches and their descriptions.

yeah i ran it with '/?' - that's where i came up with all these crazy flags!
=)

i wasn't real clear on /S vs. /E, but with your input + re-reading the help
text several times i think i've got it now. (i know it seems easy, but the
part that confused me was where they say in the help "Same as /S /E." - i
figured "if the example does it this way, it must be sensible to do it that
way."... wrong!)

thanks again! =)
 
K. Shier said:
tried the "_test" test and it disproved my hypothesis so i just started
messing with a variety of stuff... during my tinkerings, i came across the
real problem.

eh he, I thought it might.
either way, i worked around the problem by simply renaming the file to
something shorter, but i would prefer a more 'robust' solution - i.e. a copy
command that doesn't care what my files are named! (i prefer _my_
preferences to dictate how _the OS_ works, not the other way around! (i
swear i can hear the mocking laughter of Mac evangelists even as i write

On the basis that every MAC user that I come into contact with I ask "What
does a MAC do that a PC doesn't?" and the best answer so far is "I've a
button on the keyboard to eject CDs", who gives a flying ****.
any thoughts? is this a known bug? something fixed in a SP? (this machine
is only SP2 for reasons only my sys admin could explain...)

A bug sounds most likely. IIRC there have been a number of bugs discovered
in XCOPY. IMHO It's best avoided for all but the simplest of tasks. There's
plenty alternatives, ROBOCOPY for starters - and its free.

http://groups.google.com/groups?q=batch.nt OR cmdprompt.admin+robocopy+download

BTW, assuming you have access can't you zip the files first?
 
Ritchie said:
On the basis that every MAC user that I come into contact with I ask "What
does a MAC do that a PC doesn't?" and the best answer so far is "I've a
button on the keyboard to eject CDs", who gives a flying ****.

ha! too true... =)

Honestly, the ability to use The Forbidden Characters in filenames is the #1
thing i'm envious of. I keep praying Windows will one day catch up on that
one...

(being a slave to ergonomics, i used to think it was kinda neat how they
could daisy chain the mouse through the KB... but now we have USB so....
chain on!)

A bug sounds most likely. IIRC there have been a number of bugs discovered
in XCOPY. IMHO It's best avoided for all but the simplest of tasks. There's
plenty alternatives, ROBOCOPY for starters - and its free.

i will check out ROBO. it's not that often that i need to use this stuff,
but i _am_ making archival backups of important files. i don't want any
bug-induced 'surprises' if i ever need to revert to one of these archived
versions...

BTW, assuming you have access can't you zip the files first?

i suppose i could, once i register WZ so i don't get the nag prompts! my
evaluation period is almost over anyway... ;-) (right now i only get one
nag and it's at the 'end' of the batch, so not that big a deal...)

do you see a performance difference one way or the other?

are there other potential advantages besides just being able to avoid XCOPY?


thanks again!! =)
 
K. Shier said:
i suppose i could, once i register WZ so i don't get the nag prompts! my
evaluation period is almost over anyway... ;-) (right now i only get one
nag and it's at the 'end' of the batch, so not that big a deal...)

May be consider a free file archiver, something like http://www.info-zip.org
(I've not used it myself, I'm a WinRAR convert)
do you see a performance difference one way or the other?

Depends on the file sizes I guess and whether you could run the command to
archive the files locally. For instance, use PSExec or the Task Scheduler.
are there other potential advantages besides just being able to avoid XCOPY?

Can't think of any off the top of my head, I only mentioned it so you could
avoid xcopy.
 
i want to do something like XCOPY a folder structure (and its contents) to
a backup location and have it intelligently name the destination based on
the date and time. like '0310071159Backups'

*** I haven't tested this, but try something like:

@ECHO OFF
XSET CUR-DATE DATE
XSET CUR-TIME TIME

MD C:\BACKUPS\%CUR-DATE%%CUR-TIME%

XCOPY C:\FOLDER\*.* /S C:\BACKUPS\%CUR-DATE%%CUR-TIME%

:END

An XSET link is at my site.

Richard Bonner
http://www.chebucto.ns.ca/~ak621/DOS/
 
Back
Top