Topic "File name with Current Date in it"

Author Message
lex0429
[View user's profile]

Joined: 2010-01-29
Posts: 4
I am trying to automate a process where we download a few files each day from an SFTP server and these files each have a name that changes according to the date. For example today, January 29th, 2010 the file would be named 20100129.daily.pdf. Can someone either point me in the right direction as far as something i can pull an example from or provide some assistance?
Advertisements
lex0429
[View user's profile]

Joined: 2010-01-29
Posts: 4
Here is what I did in case this helps anyone out. When i get some time i want to add functionality to fire off an email if they download or fail so the user knows. Great program, glad I found it. Saved me a lot of time.



I am using a batch file, to download 3 different files, each that contains the current date in it. After i connect i change to a different folder then download each file and disconnect.

Code:
@echo off
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
   Set Month=%%A
   Set Day=%%B
   Set Year=%%C
       
        set file1=company.%Year%%Month%%Day%.x.pdf
        set file2=company.%Year%%Month%%Day%.y.pdf
        set file3=company.%Year%%Month%%Day%.z.pdf
)

cd "c:\Program Files\WinSCP"
WinSCP.com /console /command "option batch on" "option confirm off" "open user:password@SomeDomain.com" "cd /daily/files/" "get %file1% c:\download\" "get %file2% c:\download\" "get %file3% c:\download\" "exit"
exit
[/code]
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Also read documentation.
_________________
Martin Prikryl
lex0429
[View user's profile]

Joined: 2010-01-29
Posts: 4
prikryl wrote:
Also read documentation.


I did, very thoroughly but I couldn't quite find what i was looking for. I may have to go from a batch file to a script such as in the link you provided. I now need to calculate the date of the prior business day, not the current day and i think via batch file might be too much work.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
OK, let me know if you need any help with it.
_________________
Martin Prikryl
lex0429
[View user's profile]

Joined: 2010-01-29
Posts: 4
Thanks for following up.
With some help from someone over at experts-exchange.com and looking at some examples here I was able to use a js instead of a simple batch file. Here is that code in case anyone finds it useful. Just working on sending off an email after the downloads are successful preferably with the files attached. Thanks again.

Code:

// Function to figure out the date of the previous business day
// in the format YYYYMMDD including place holding zeros.
function PreviousBizDay() {
    function D2( val ) {
      return ( ( val < 10 ) ? '0' : '' ) + val;
    }
    var today = new Date();
    today.setDate( today.getDate() - 1 );   // Yesterday...
    while ( ( today.getDay() < 1 ) || ( today.getDay() > 5 ) ) {
      today.setDate( today.getDate() - 1 ); // Previous day
    }
    return today.getFullYear() + D2( today.getMonth() + 1 ) + D2( today.getDate() )
  }
// Local path to download to (MUST keep trailing slashes)
var LOCALPATH = "c:\\folder\\";
// Remote path to search in (MUST keep trailing slash)
var REMOTEPATH = "/daily/downloads";
// Session to connect to
var SESSION = "username:password@someserver.com";
// Path to winscp.com (MUST keep trailing slashes)
var WINSCP = "c:\\program files\\winscp\\winscp.com";
var filesys = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");
var FILE1 = "file." + PreviousBizDay() + ".example.pdf";
var FILE2 = "file." + PreviousBizDay() + ".example.doc";
var FILE3 = "file." + PreviousBizDay() + ".example.xls"
var exec;
 
// Run WINSCP and download the 3 files, then exit
exec = shell.Exec("\"" + WINSCP + "\"");
exec.StdIn.Write(
    "option batch abort\n" +
    "option confirm off\n" +
    "open \"" + SESSION + "\"\n" +
    "get \"" + REMOTEPATH + FILE1 + "\" \"" + LOCALPATH + "\"\n" +
    "get \"" + REMOTEPATH + FILE2 + "\" \"" + LOCALPATH + "\"\n" +
    "get \"" + REMOTEPATH + FILE3 + "\" \"" + LOCALPATH + "\"\n" +
    "exit\n");
hyperV

Guest


Hi lex0429,

Are you able to do the sending of the emails after the successful download?

How do you schedule the java script for the download??
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License