Computer Hope

Software => Computer software => Topic started by: swinters on September 29, 2009, 01:42:35 PM

Title: Defining current date in batch file
Post by: swinters on September 29, 2009, 01:42:35 PM
I have a file that gets exported daily.  It always ends with todays date, i.e. support_20090929.csv.  I've tried several variables to pull in the current date each day in my ftp batch file without any luck.  Below are some examples of what I've tried.  If anyone has any idea how to do this I'd appreciate the help.

put \\slnk1001a_support_&date&.csv /slnk1001a/slnk1001a_support&date&.csv

put \\slnk1001a_support_$YYYYMMDD$.csv /slnk1001a/slnk1001a_support_$YYYYMMDD$.csv

put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv
Title: Re: Defining current date in batch file
Post by: Geek-9pm on September 29, 2009, 02:02:23 PM
Not clear what "put" means
Command prompt says it is not a command.
Try to explain what you want in some more detail.
Title: Re: Defining current date in batch file
Post by: swinters on September 29, 2009, 02:37:11 PM
Its taking the file from my server and ftp'ing it to another vendor...what I do currently is change the date manually everytime.  Below is the .bat file code and the ftp.txt file it calls is below that.  I'm trying to find code that will replace the 20090929 each day with the current date so that I don't have to key it manully each time.

ftp.bat
ftp -n -i -d -g -s:S:\BatchFiles\FTP.txt ftp.site.com

ftp.txt
put S:\EXCEL\slnk1001a_support_20090929.csv /slnk1001a/slnk1001a_support_20090929.csv
Title: Re: Defining current date in batch file
Post by: oldun on September 29, 2009, 05:22:52 PM
Your third example looks as though it ought to work.
What output are you getting?
Title: Re: Defining current date in batch file
Post by: Geek-9pm on September 29, 2009, 06:16:33 PM
Part of a batch to use current date could be like this:
Code: [Select]
REM Below is an exercise to show...
REM date string manipulation.
DATE /T
echo %date:~4,2%
echo %date:~7,2%
echo %date:~10,4%
echo %date:~4,2%>MM.TMP
echo %date:~7,2%>DD.TMP
echo %date:~10,4%>YYYY.TMP
set /P MM= <MM.TMP
SET /P DD= <DD.TMP
SET /P YYYY= <YYYY.TMP
ECHO %MM%%DD%%YYYY%
Title: Re: Defining current date in batch file
Post by: swinters on September 30, 2009, 06:34:42 AM
Below is an excert from the third example.  I've also included what it looks like when i have the date hard coded in and the ftp is successful.

550 /slnk1001a/slnk1001a_disconnect_%date:~10,4%%date:~4,2%%date:~7,2%.csv: The
filename, directory name, or volume label syntax is incorrect.


---> STOR /slnk1001a/slnk1001a_disconnect_20090930.csv
150 Opening ASCII mode data connection for /slnk1001a/slnk1001a_disconnect_20090
930.csv.
226 Transfer complete.
ftp: 75671 bytes sent in 0.45Seconds 167.04Kbytes/sec.

Thanks!
Title: Re: Defining current date in batch file
Post by: oldun on September 30, 2009, 04:44:41 PM
OK. The problem appears to occur because FTP is unable to expand the variable. You may need to CALL the PUT command. Something like:
Code: [Select]
call:put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv


:put
put %*
Title: Re: Defining current date in batch file
Post by: swinters on October 02, 2009, 06:24:16 AM
Hi Oldun - Thank you once again for helping me.  I tried the new code you suggested and still get an error...little different though.

ftp> call:put S:\slnk1001a_support_%date:~10,4%date:~4,2%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~1
0,4%date:~4,2%date:~7,2%.csv
Invalid command.
Title: Re: Defining current date in batch file
Post by: Spoiler on October 02, 2009, 11:12:40 AM

Change your FTP to use the mput comand.

PROMPT

lcd put S:\EXCEL\

mput *.csv

this will send all the files named .csv in that folder.

If you only want to send todays file then setup a temp folder to move all the old stuff out of the working folder leaving todays file only. After the FTP move the file to the temp folder for storage and start a new blank file for the next day.



 

Title: Re: Defining current date in batch file
Post by: oldun on October 03, 2009, 06:41:56 PM
Sorry. Try rewriting your ftp.bat file like this:

NOTE: The Echo line should be on a single line.
Code: [Select]
@Echo Off
Echo put \\slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv /slnk1001a/slnk1001a_support_%date:~10,4%%date:~4,2%%date:~7,2%.csv >S:\BatchFiles\ftp.txt

ftp -n -i -d -g -s:S:\BatchFiles\FTP.txt ftp.site.com

Title: Re: Defining current date in batch file
Post by: Salmon Trout on October 04, 2009, 09:19:49 AM
I am presuming that the %date% format is the US style, Day dd/mm/yyyy.

why not shorten the long line thus and make things more readable

Code: [Select]
set YYYYMMDD=%date:~10,4%%date:~4,2%%date:~7,2%
set file=slnk1001a_support_%YYYYMMDD%.csv
set path1=\\%file%
set path2=/slnk1001a/%file%
set ftptxtfile=S:\BatchFiles\ftp.txt
Echo put %path1% %path2% >%ftptxtfile%
ftp -n -i -d -g -s:%ftptxtfile% ftp.site.com
Title: Re: Defining current date in batch file
Post by: swinters on October 15, 2009, 08:21:30 AM
Hi Oldun - Thanks so much for the reply.  I finally got some time to play with it this morning and it worked perfectly.  No more manual FTP'ing.  Thanks so much!!!!