none
Add Timestamp - batch file that calls a script CMD

    Question

  • Hello,

    I'm having a bugger of a time. I read all these posts about adding a timestamp to a file using batch files and scripts.

    What I'm trying to do, Call a batch file which consists of "ftp -i -s:orders.script"

    That batch file is called orders.bat

    The script is a little different...

    Starts off:

    Open 192.168.1.5

    username

    password

    cd Client

    cd Customer

    cd Order

    put order.csv order.csv

    quit

    My question is how do I accomplish putting a timestamp in the file name of "order.csv" that I am putting on the one server from the other?

    Do I put it in the script file, or the batch file? Assuming script... Could be wrong though. There has got to be a simple way to do this with how I'm doing this. Or maybe I'm doing it the hard way... Any help would be greatly appreciated. I'm using Command prompt to accomplish this, the batch file is ran from a job in the task scheduler that goes off 3 times a day. the file gets transferred from an SBS 2008 server to an IBM AS400. I hope I'm not missing any information that seems relevant.


    • Edited by JLH1982 Friday, November 9, 2018 3:28 PM
    Thursday, November 8, 2018 11:04 PM

Answers

  • TO begin with you should not be using batch and FTP scrips. Batch is obsolete and will be removed from the next version of Windows.  Why waste time learning an obsolete method when you can more easily use PowerShell.

    The following uploads a single file to the FTP folder without a lot of messing around.

    $file = 'order.csv'
    $ftpfile = 'ftp://ftp.server.com/dir/order {0:MM-dd-YYYY:HHMMSS}.csv' -f [datetime]::Now
    $user = 'username'
    $pass = 'password'
     
    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
    $webclient.UploadFile($ftpfile, $file)
    $webclient.Dispose()


    \_(ツ)_/


    • Edited by jrv Friday, November 9, 2018 3:47 PM
    • Marked as answer by JLH1982 Friday, November 9, 2018 6:38 PM
    Friday, November 9, 2018 3:46 PM

All replies

  • You cannot do a timestamp from an FTP script.  The file will have a creationtime of the time the download started.

    Windows CSV files do not have timestamps.  CSSV files do not have timestamps.

    The creation time will be created by the IBM system is the case of a PUT.


    \_(ツ)_/


    • Edited by jrv Friday, November 9, 2018 12:08 AM
    Friday, November 9, 2018 12:07 AM
  • Maybe I wasn't very clear. The final output should be something like order11-09-18:123055.csv

    putting the timestamp in the file name.

    Friday, November 9, 2018 3:20 PM
  • So  the real question is how to rename a file.  Is that what you are asking?  If so this has nothing to do with FTP or with FTP script files.

    To rename a file just use the "ren" in the batch. 

    Help for the command is:

    ren /?


    \_(ツ)_/

    Friday, November 9, 2018 3:27 PM
  • echo Open 192.168.1.5 > orders.script
    echo username >> orders.script
    echo password >> orders.script
    echo cd Client >> orders.script
    echo cd Customer >> orders.script
    echo cd Order >> orders.script
    echo put order.csv order%date:~12,2%-%date:~4,2%-%date:~7,2%-%time%.csv >> orders.script
    echo quit >> orders.script
    ftp -i -s:orders.script
    Friday, November 9, 2018 3:30 PM
  • TO begin with you should not be using batch and FTP scrips. Batch is obsolete and will be removed from the next version of Windows.  Why waste time learning an obsolete method when you can more easily use PowerShell.

    The following uploads a single file to the FTP folder without a lot of messing around.

    $file = 'order.csv'
    $ftpfile = 'ftp://ftp.server.com/dir/order {0:MM-dd-YYYY:HHMMSS}.csv' -f [datetime]::Now
    $user = 'username'
    $pass = 'password'
     
    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
    $webclient.UploadFile($ftpfile, $file)
    $webclient.Dispose()


    \_(ツ)_/


    • Edited by jrv Friday, November 9, 2018 3:47 PM
    • Marked as answer by JLH1982 Friday, November 9, 2018 6:38 PM
    Friday, November 9, 2018 3:46 PM
  • Thank you for pointing me in the right direction. Powershell was super easy, I did modify it a bit, mapped the ftp to a network drive and just did a copy-item then added "...{0:yyyyMMdd-HHmmss}order.csv" -f (get-date)) and I'm done.

    Thank you again!!

    Friday, November 9, 2018 8:53 PM