Batch programming help - variables

Discussion in 'Software' started by Sheldor, Apr 23, 2013.

  1. Sheldor

    Sheldor Private E-2

    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.
     
  2. GermanOne

    GermanOne Guest

    The reason why it doesn't work is that environment variables are not supported in simple text files. Also your sftp tool does not support them. Your current construct works as follows:
    - the sftp tool is called via command line
    - the sftp tool reads the content of the text file and processes it line by line
    - if it reaches the 3rd line it reads %ts% as string (nothing but a literal expression)

    There is only one solution how to solve that problem. Change the content of the text file each time before you call the sftp tool using echo-redirection.

    Untested example:
    Code:
    @echo off &setlocal
    
    :: Calculate and define variable %ts% here! You didn't tell us how you do it.
    
    >"C:\Scripts\Inbound.txt" (
      echo cd test
      echo lcd C:\data\in\
      echo get goods_receipt.-%ts%.txt
      echo close
      echo quit
    )
    psftp user@server -pw "password" -batch -be -b C:\Scripts\Inbound.txt
    
    Regards
    GermanOne
     
  3. theefool

    theefool Geekified

    Awesome, I learn so much from you, GermanOne.
     
  4. GermanOne

    GermanOne Guest

    You're welcome.
    I always appreciate (any kind of) feedback :)
     
  5. theefool

    theefool Geekified

    Yeah, I'm slowly getting involved with powershell, which is a whole different beast. I'd post fixes here based on powershell, but, I feel I'd get boo'ed. heh.

    Hence why I still work with "dos" based scripts, here. :cry

    As soon as my work gets full on windows 7, I'll be writing a lot of fixes based on powershell. Looking forward to it. I've already been to a class on it.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds