1.

Solve : Edit a text file?

Answer»

Hi All,
I use a batch file to start a FTP session. The command I am using looks like this

ftp -n -s:d:\ftp\data.txt ftp_server_name.com

This uses a text file called data.txt to get the commands for the FTP session. It looks like this…

user Username Password
put D:\Some_file.txt
Bye


All of this has been working fine. That is until they changed the file names to include the date. So I have gone from:

put D:\Some_file.txt
to
put D:\some_file20070117.txt

My problem is that I would have to edit the data.txt file each day to change the date in the file name. Does anyone have an idea as to how to change the file name each day with the current date?


Thanks!
An EASY way to do this would be to create a variable called something like current_date using the set command and the integrated date variable... i.e.

set current_date=%date%

then use the new variable in the filename.. i.e. change it from

put D:\some_file.txt
to
put D:\some_file%current_date%.txt

Good luck,
-darrylHehe, you're also going to have to format the date, apparently... the standard DOS format includes spaces and "/"s, which are no-nos for filenames... whoops...

here's how to get your date to yyyymmdd as you have it in your post

set current_date=%date:~10,4%%date:~4,2%%date:~7,2%

use that line instead of "set current_date=%date%"

what it does is cuts up %date% which returns something like "wed 01/17/2007" and puts it back together in an orderly fashion.

e.g. %date:~10,4% calls for the 11th character and the 3 characters after it (a 4-character string)

that should work a little better... sorry
-darrylQuote


An easy way to do this would be to create a variable called something like current_date using the set command and the integrated date variable... i.e.

set current_date=%date%

then use the new variable in the filename.. i.e. change it from

put D:\some_file.txt
to
put D:\some_file%current_date%.txt

Good luck,
-darryl


But does ftp, when it reads data.txt for its commands convert %current_date%
to the env var , or does it read that text literally?



This happened to me recentl
I ended up using my batch to write the contents of the ftp script each time it was called, in which case, the batch expansion of the date variable will occur and the literal value will be written to the script file
Cheers
GrahamQuote
This happened to me recentl
I ended up using my batch to write the contents of the ftp script each time it was called, in which case, the batch expansion of the date variable will occur and the literal value will be written to the script file
Cheers
Graham


You mean that the original poster would need to use dworley's line of code to set the env var, then run a batch file something like this? :

Code: [Select]
@echo off
echo user Username Password > data.txt
echo put somefile%current_date%.txt >> data.txt
echo bye >> data.txt



I am not sure thats going to work. My goal was to inject a new name for a file that had the current date into a text file. That way the FTP session would use the new data.txt file for its session.

Maybe I don't understand what was suggested.

Hi,
I re-read the answer and I think I understand it now....ok I am a little slow.

I am going to give it a try. Thanks for your help!!

Quote
Hi,
I re-read the answer and I think I understand it now....ok I am a little slow.

And I thought I was the only one that had to go over things more than once.



Quote
I am going to give it a try.

That's the spirit!


Quote
Thanks for your help!!

We'll see.
If it works.
I hope it does help you.

Let us know what happens.




Quote

...and I think I understand it now....

Don't hesistate to quote anything you are unsure of, and ask.


Ok...I must be doing something wrong. I copied the sample code above and added the set date from above and it worked fine. It wrote me a data.txt file and it had everything in it that I would need to use for a FTP session.

Than I made the changes to ADD the real stuff I wanted in the data.txt file and its not working any more.

Here is my bat file:

set current_date=%date:~10,4%%date:~4,2%%date:~7,2%

@echo off
echo user sm2thy joneser > data.txt
echo cd /cool/directory/OK.Now >> data.txt
echo put C:\FTP\TEMP\BOB.LCT%current_date%.DAT >> data.txt
echo put C:\FTP\TEMP\BOB.TCL%current_date%.DAT >> data.txt
echo put C:\FTP\TEMP\FEDWL.TXT >> data.txt
echo bye >> data.txt

pause

And here is the out put in the data.txt file.

user sm2thy joneser
cd /cool/directory/OK.Now
put C:\FTP\TEMP\BOB.LCT20070118 .DAT
put C:\FTP\TEMP\BOB.TCL20070118 .DAT
put C:\FTP\TEMP\FEDWL.TXT
bye

pause

I don't know how to get rid of the extra space after the date in the files name.

Also can someone tell me about the > in the bat file? Why is there only one of them in the first line but all the others need 2?

Thanks for your help!!

Quote
Ok...I must be doing something wrong. I copied the sample code above and added the set date from above and it worked fine. It wrote me a data.txt file and it had everything in it that I would need to use for a FTP session.

Great! That sounds good, so far.


Quote
Than I made the changes to add the real stuff I wanted in the data.txt file and its not working any more.

Here is my bat file:

set current_date=%date:~10,4%%date:~4,2%%date:~7,2%

@echo off
echo user sm2thy joneser > data.txt
echo cd /cool/directory/OK.Now >> data.txt
echo put C:\FTP\TEMP\BOB.LCT%current_date%.DAT >> data.txt
echo put C:\FTP\TEMP\BOB.TCL%current_date%.DAT >> data.txt
echo put C:\FTP\TEMP\FEDWL.TXT >> data.txt
echo bye >> data.txt

pause

And here is the out put in the data.txt file.

user sm2thy joneser
cd /cool/directory/OK.Now
put C:\FTP\TEMP\BOB.LCT20070118 .DAT
put C:\FTP\TEMP\BOB.TCL20070118 .DAT
put C:\FTP\TEMP\FEDWL.TXT
bye

pause

I don't know how to get rid of the extra space after the date in the files name.

I don't have XP, so I can't try that command and experiment with the switches, etc.
Hopefully somebody else will be along soon that can work it out for you.


Quote
Also can someone tell me about the > in the bat file? Why is there only one of them in the first line but all the others need 2?

Those are redirection symbols.
Means 'take the output and instead of printing it to the screen, print it to the file which is named next'.
One ' > ' also means, ' and if that filename ALREADY exists, overwrite it'.
Two '>>' means, ' and if that filename already exists, don't overwrite it, but append to it' .

Check the file date/time stamp, after you've run your batch file more than once.
data.txt is always new, isn't it?
That line using only one > is creating a new data.txt each time.


Quote
Thanks for your help!!

You are welcome. I hope you get that blank figured out soon. You're almost there.


A space is a very real character and is included in the set statement WHENEVER the space bar is used. Ensure there is no trailing space after the LAST % symbol in your set statement:

set current_date=%date:~10,4%%date:~4,2%%date:~7,2%[highlight] [/highlight]

Good luck. 8-)Very cool!

I checked and I did have a space at the end. I would have never thought of that. Thanks!!

Now I am going to plug all of this into the full script.

Thanks for everyones help!


Discussion

No Comment Found