Using a variable in file name when copying

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dcjones
Posts: 1
Joined: 07 Oct 2010 08:43

Using a variable in file name when copying

#1 Post by dcjones » 07 Oct 2010 08:55

Hi All,

I have a simple batch file which uses CURL to FTP a file. The file name has a dynamic date like:

LGWcapture-"yyyymmdd"-"hhnnss".txt

The "yyyymmdd"-"hhnnss" changes each time the file is updated.

My batch file is:

curl -T c:\ComCapV4\logs\LGWcapture-"yyyymmdd"-"hhnnss".txt ftp://mydomain.com/httpdocs/xml/ --user name:password

How would I include the date part of the file in my script.

Many thanks in advance.

Dereck

Hung_war3
Posts: 1
Joined: 08 Oct 2010 01:08

Re: Using a variable in file name when copying

#2 Post by Hung_war3 » 08 Oct 2010 01:14

You can use this code to get date time (your computer time)

Code: Select all

set hh=%time:~0,2%
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set day=%Date:~-10,2%
if "%Date:~-10,2%"=="%Date:~-10,2%" set day=-%Date:~-10,2%
set month=%Date:~-7,2%
if "%Date:~-7,2%"=="%Date:~-7,2%" set month=-%Date:~-7,1%
set year=%Date:~-4,4%
if "%Date:~-4,4%"=="%Date:~-4,4%" set year=-%Date:~-2,4%
ipconfig>"%CD%\log\%COMPUTERNAME%__[%day%_%month%_%year%_%hh%_%time:~3,2%_%time:~6,2%].txt"

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Using a variable in file name when copying

#3 Post by miskox » 11 Oct 2010 02:02

This is not a good approach. In theory time/date can change between two SET commands so you could get:

Example: currently the date is 31-dec-2009, time: 23:59:59.99 (or very close)

day=31
here date changes from 31-dec-2009 to 1-jan-2010
month=1 (instead of 12)
year=2010 (instead of 2009)

You have to use something like this:

currdatetime=%time% %date%

and then you have to manipulate with a currdatetime variable.

(better solution is to use FOR command)

Saso

Post Reply