Batch programming help - variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sheldor
Posts: 3
Joined: 23 Apr 2013 13:12

Batch programming help - variables

#1 Post by Sheldor » 23 Apr 2013 13:16

All,

Background:
I have a batch script that has an sftp command in it that's supposed

to log in to a server and pull a file. The set of commands to be

executed on the server are consolidated in a command file and this

file is used in the sftp command as the input parameter.

Issue:
The file to be pulled has a dynamic timestamp on it that keeps changing with every new file. I calculate that timestamp in the main
script and store it in a variable. But in order to pull the correct file, I need to pass this variable to the command file so that the
actual filename on the server could be determined on run time. But the compiler fails to recognize this variable(%ts%) in the command file.
Below is the putty sftp command in the main batch script:

psftp user@server -pw "password" -batch -be -b C:\Scripts\Inbound.txt

The contents of C:\Scripts\Inbound.txt are as below:

cd test
lcd C:\data\in\
get goods_receipt.-%ts%.txt
close
quit

The above usage of %ts% needs to be fixed. Any ideas or comments would be helpful.

Thanks in advance.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch programming help - variables

#2 Post by abc0502 » 23 Apr 2013 15:14

you have to make the batch create the Inbound file, so it can pass the value of "%ts%" to the file when it reads it.

So your main batch do these steps:
1) calculate the time stamp.
2) Create the Inbound file, and pass the dynamic stamp while creating it.
3) Run the SFTP command.

The 2nd Step this is how it's should be done: ( add this to your batch before running the sftp command )

Code: Select all

(
ECHO cd test
ECHO lcd C:\data\in\
ECHO get goods_receipt.-%ts%.txt
ECHO close
ECHO quit
)>"C:\Scripts\Inbound.txt"
This code will overwrite any existing file, and make sure the path exist, it won't create the folder Scripts if it doesn't exist.

Sheldor
Posts: 3
Joined: 23 Apr 2013 13:12

Re: Batch programming help - variables

#3 Post by Sheldor » 23 Apr 2013 17:09

I agree with this solution abc. I was able to make my script work using this method. But I am more interested in knowing how the parameter passing works in such a scenario.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch programming help - variables

#4 Post by abc0502 » 23 Apr 2013 17:31

You can only pass variables from batch files while the batch file creates the inbound file, as the sftp command is the one who reads the file, so it can't modify or replace any words inside it, even if the batch reading the file it won't unless you code it in a way the make it do that.

Sheldor
Posts: 3
Joined: 23 Apr 2013 13:12

Re: Batch programming help - variables

#5 Post by Sheldor » 23 Apr 2013 23:19

Okay, I understand now. Thanks so much for your prompt assistance. :)

Regards,
Sheldor

Post Reply