- lex0429
- Joined:
- Posts:
- 4
File name with Current Date in it
Advertisement
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?
Advertisement
- lex0429
- Joined:
- Posts:
- 4
Figured it out
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]
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.
@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
-
martin◆
Site Admin - Joined:
- Posts:
- 41,454
- Location:
- Prague, Czechia
Re: File name with Current Date in it
Also read documentation.
- lex0429
- Joined:
- Posts:
- 4
Re: File name with Current Date in it
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◆
Site Admin
Re: File name with Current Date in it
OK, let me know if you need any help with it.
Advertisement
- lex0429
- Joined:
- Posts:
- 4
Example 2
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.
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.
// 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??
Are you able to do the sending of the emails after the successful download?
How do you schedule the java script for the download??
-
HappyPaul
Guest
Re: Figured it out
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
Yep that did it for me to generate a date for my logfile.
Thank you for you tip! :-)
@echo off @For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( Set Month=%%A Set Day=%%B Set Year=%%C Set Logfile="%Year%%Month%%Day%_winSCP.log" ) "F:\WinSCP\WinSCP.com" ^ /log=E:\Log\%Logfile% /ini=nul ^
-
martin◆
Site Admin - Joined:
- Posts:
- 41,454
- Location:
- Prague, Czechia
Re: Figured it out
WinSCP can do this on its own, without that huge (and unreliable) batch file construct:Yep that did it for me to generate a date for my logfile.
Thank you for you tip! :-)
@echo off @For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( Set Month=%%A Set Day=%%B Set Year=%%C Set Logfile="%Year%%Month%%Day%_winSCP.log" ) "F:\WinSCP\WinSCP.com" ^ /log=E:\Log\%Logfile% /ini=nul ^
/log=E:\Log\!Y!M!D_WinSCP.log
See https://winscp.net/eng/docs/ui_pref_logging#session_log
Advertisement
You can post new topics in this forum