.bat file to copy files using current date as part of name?

  • Thread starter Thread starter +Bob+
  • Start date Start date
B

+Bob+

Guest
I need to set up a bat file that will use xcopy to copy a directory
from one drive to another. The resulting directory needs to
incorporate the date that it is copied into the name. Something like
this:

xcopy c:\bigdir\*.*/s/e d:\bigdir-10-oct-2008\*.*

I need to do this on a regular basis, so how can I pull the date
within the bat file into a variable?

Also, assuming I can get the date into a variable named something like
"DateIn" will a command like this work:

xcopy c:\bigdir\*.*/s/e d:\bigdir-%DateIn%\*.*

Thanks,
 
Re: .bat file to copy files using current date as part of name?

The date and time was 10/20/2008 9:20 AM, and on a whim, +Bob+ pounded
out on the keyboard:

> I need to set up a bat file that will use xcopy to copy a directory
> from one drive to another. The resulting directory needs to
> incorporate the date that it is copied into the name. Something like
> this:
>
> xcopy c:\bigdir\*.*/s/e d:\bigdir-10-oct-2008\*.*
>
> I need to do this on a regular basis, so how can I pull the date
> within the bat file into a variable?
>
> Also, assuming I can get the date into a variable named something like
> "DateIn" will a command like this work:
>
> xcopy c:\bigdir\*.*/s/e d:\bigdir-%DateIn%\*.*
>
> Thanks,


Hi Bob,

I use this script in a batch file that adds a "today" variable, then I
use it to create the folder name.

This is the script:
:: begin script
@echo off
echo D = Now : WScript.Echo Right(100+Month(D),2) ^& "-" ^& _
>%tmp%\today.vbs

echo Right(100+Day(D),2) ^& "-" ^& Year(D) >>%tmp%\today.vbs

for /f "tokens=1" %%a in (
'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

del %tmp%\today.vbs
echo Todays date: %today%
:: end script

Then I add a line like this to use the variable:
xcopy "d:\data\Mozilla Firefox\Profiles\*.*" "d:\data\Mozilla profile
backup\%today%\Firefox\" /e /y /d

I'm sure you can modify it for your needs,

--
Terry R.

***Reply Note***
Anti-spam measures are included in my email address.
Delete NOSPAM from the email address after clicking Reply.
 
Re: .bat file to copy files using current date as part of name?

I did a quick google on "dos date" and the first link below has a batch
file to name a folder with the date and time.

http://talk.bmc.com/blogs/blog-gentle/anne-gentle/dos-timestamp-tips/view?searchterm=batch

I then searched on name file with date in batch and got the following
link which seems a bit complicated -

http://www.ericphelps.com/batch/samples/namedate.txt

Then I found this link which has a much simpler method -

http://www.zorbathegeek.com/153/batch-file-to-append-date-to-file-name.html

Another link offers the following code which is similar to the last link
there -

for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
(set Dy=%%i)
(set Mth=%%j)
(set Yr=%%k)
)
I've modified the last line of his code below to show how to name your
directory.

mkdir c:\%Dy%-%Mth%-%Yr%

This assumes you want it on the C drive, change the drive and pathname
to what you need.

Later, Ray Parrish

+Bob+ wrote:
> I need to set up a bat file that will use xcopy to copy a directory
> from one drive to another. The resulting directory needs to
> incorporate the date that it is copied into the name. Something like
> this:
>
> xcopy c:\bigdir\*.*/s/e d:\bigdir-10-oct-2008\*.*
>
> I need to do this on a regular basis, so how can I pull the date
> within the bat file into a variable?
>
> Also, assuming I can get the date into a variable named something like
> "DateIn" will a command like this work:
>
> xcopy c:\bigdir\*.*/s/e d:\bigdir-%DateIn%\*.*
>
> Thanks,


--
http://www.rayslinks.com/ Web index of human reviewed links.
<http://www.rayslinks.com/Troubleshooting%20and%20fixing%20Windows.html>
Trouble shooting and Fixing Windows
http://www.writingsoftheschizophrenic.com My poetry in web pages
 
Re: .bat file to copy files using current date as part of name?

On Mon, 20 Oct 2008 11:28:59 -0700, Ray Parrish <crp@cmc.net> wrote:

>Another link offers the following code which is similar to the last link
>there -
>
>for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
> (set Dy=%%i)
> (set Mth=%%j)
> (set Yr=%%k)
>)
>I've modified the last line of his code below to show how to name your
>directory.
>
>mkdir c:\%Dy%-%Mth%-%Yr%
>
>This assumes you want it on the C drive, change the drive and pathname
>to what you need.


Thanks Boys, this got me where I needed to go.


>
>Later, Ray Parrish
 
Re: .bat file to copy files using current date as part of name?

On Mon, 20 Oct 2008 09:53:51 -0700, "Terry R." <F1Com@NOSPAMpobox.com>
wrote:

>for /f "tokens=1" %%a in (
> 'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a


Terry:

It seems to throw an error on this line. I don't know enough about
windows scripting to diagnose it.
 
Re: .bat file to copy files using current date as part of name?

The date and time was 10/28/2008 6:00 PM, and on a whim, +Bob+ pounded
out on the keyboard:

> On Mon, 20 Oct 2008 09:53:51 -0700, "Terry R." <F1Com@NOSPAMpobox.com>
> wrote:
>
>> for /f "tokens=1" %%a in (
>> 'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

>
> Terry:
>
> It seems to throw an error on this line. I don't know enough about
> windows scripting to diagnose it.
>
>
>


Did you just copy and paste all of it into your batch file? It runs
perfectly here and wherever I install it...

What is the error? You sure it's not about deleting the today.vbs in
the temp folder?

--
Terry R.

***Reply Note***
Anti-spam measures are included in my email address.
Delete NOSPAM from the email address after clicking Reply.
 
Back
Top