WinSCP

WinSCP can also be used for ftp and secured sftp

Run all from 1 command (no separate script file needed!)

"\\svr\myshare\WinSCP.exe" /log=mylog.txt /command  ^
 "option batch continue" "option confirm off" ^
 "open ftp://myftpuser:mypswd@myftpsvr.com/ -passive=on" ^
 "mput *.bin /someremotedir/" "exit"



filemask

C:\app\WinSCP>winscp

#Open VPS connection (should be stored in winSCP.ini)
winscp> open vps
Searching for host...
Connecting to host...
Authenticating...
Using username "someuser".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] VPS
winscp> pwd

winscp> cd /home
/home

winscp> ls
drwxr-xr-x  22 root     root          4096 Aug 25 15:52:09 2015 ..
drwxr-xr-x   3 root     root          4096 Jan 29 16:13:40 2016 .
drwxr-xr-x   4 root     root          4096 Jan 29 16:13:58 2016 dir1


winscp> get -filemask=*.txt *
dir1                      |            0 B |    0.0 KiB/s | binary |   0%
subdir2                   |            0 B |    0.0 KiB/s | binary |   0%
HaxLogs2.txt              |          142 B |    0.0 KiB/s | ascii  | 100%
subdir1                   |            0 B |    0.0 KiB/s | ascii  |   0%
HaxLogs.txt               |          142 B |    0.5 KiB/s | ascii  | 100%


#view options

winscp> option
echo            off
batch           off
confirm         on
reconnecttime   off

#disable overwrite file prompt
winscp> option confirm off
confirm         off

#get files created today (aka: less than 1 day old)

winscp> get -filemask=*.txt>1d *
dir1                      |            0 B |    0.0 KiB/s | binary |   0%
subdir2                   |            0 B |    0.0 KiB/s | binary |   0%
HaxLogs2.txt              |          142 B |    0.0 KiB/s | ascii  | 100%
subdir1                   |            0 B |    0.0 KiB/s | ascii  |   0%
HaxLogs.txt               |          142 B |    0.5 KiB/s | ascii  | 100%


#get files created after 28Jan2016

winscp> get -filemask=*.txt>2016-01-28 *
dir1                      |            0 B |    0.0 KiB/s | binary |   0%
subdir2                   |            0 B |    0.0 KiB/s | binary |   0%
subdir1                   |            0 B |    0.0 KiB/s | binary |   0%

#Other examples
#get public_html/index.html d:\www\about.*
#get -filemask=*.html -resumesupport=on *

based on size

>size[KMG] Matches files larger than size. (in Kilobyte, Megabyte or Gigabyte)
  ex: *.bin>100M   ...matches bin files larger than 100MB
    Note: dirs are considered to have a zero size.
===========================================

# Dont prompt when overwriting files

option confirm off

Sample typo/error/invalidSyntax (I left out dashes in the date)

#winSCP_script.txt
option confirm off
open IRD
get -filemask=*.txt>20160128 *
close
exit

C:\app\Traffic_IRD_20160129>winSCP.bat

C:\app\Traffic_IRD_20160129>winscp.com /script=winSCP_script.txt /log=script.log

confirm         off
Searching for host...
Connecting to host...
Authenticating...
Using username "someuser".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Reading remote directory...
Session started.
Active session: [1] IRD
ASCII_Reports             |            0 B |    0.0 KiB/s | binary |   0%
955300                    |            0 B |    0.0 KiB/s | binary |   0%
2016                      |            0 B |    0.0 KiB/s | binary |   0%
02                        |            0 B |    0.0 KiB/s | binary |   0%
01                        |            0 B |    0.0 KiB/s | binary |   0%
...
2016                      |            0 B |    0.0 KiB/s | binary |   0%
02                        |            0 B |    0.0 KiB/s | binary |   0%
01                        |            0 B |    0.0 KiB/s | binary |   0%
Session 'IRD' closed.
No session.
C:\app\Traffic_IRD_20160129>


log file shows:
20160201_530_IRD ASCII Raw Data.txt
;-;5361679;2016-02-02T13:31:39.000Z;"1001" [1001];"113"
[113];rw-r--r--;0.;d;4096;2016-02-02T13:31:38.000Z;"1001"
[1001];"113"
[113];rwxr-xr-x;2

2016-02-02 09:25:27.023
File "/in_data/ASCII_Reports/955300/2016/02/20160201_530_IRD ASCII Raw Data.txt" excluded from transfer (EScpSkipFile)


hmmm....

https://winscp.net/eng/docs/file_mask#size_time

ahhhhaaa!  it wants the dashes in the date of the filemask!!!

# Dont prompt when overwriting files
option confirm off
open IRD
get -filemask=*.txt>2016-01-28 *
close
exit

date and time

example format syntax with date AND time:  <yyyy-mm-dd[ hh:mm[:ss]]
   Match files modified before the date or time (i.e. “older than date and time”).
         get -filemask=*.doc<2012-02-29 08:54:21 *


get file listing from command line to output file

--uses saved connection data from the ini file
 "WinSCP.exe" /command "open inftp@ftp.mysftpsvrprofile.com" "ls"  "exit" /log="winscplog.txt"


.NET for cSharp

http://winscp.net/eng/docs/library_install
The package includes the assembly itself (winscpnet.dll)
 and a required dependency, WinSCP executable winscp.exe
  The binaries interact w/each other and must be kept in the same folder!!!

In rare situations this is not possible (e.g. when installing the assembly to GAC),
 make use of Session.ExecutablePath property to force the assembly to look for the winscp.exe in a different location.

Using Visual Studio
 If you reference WinSCP assembly from your project
   it copies the assembly during build into the project Output path (e.g. <your_project_path>/obj/Debug).

 You may want to add winscp.exe to your Visual Studio project,
  and have it copied to Output path automatically (by setting file property Copy to Output Directory to Copy if newer).
   The Build Action should be automatically set to Content, what means that the file
    will be included when deploying your application (e.g. an ASP.NET web app).



Registering for COM
 WinSCP .NET assembly exposes its full interface to COM.
 As a COM library, it needs to be registered before use.
 If you are going to use the COM interface, register the assembly:
%WINDIR%\Microsoft.NET\Framework\<version>\RegAsm.exe WinSCPnet.dll /codebase /tlb
Typical registration commands for .NET 4.0 on 64-bit system would be:
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb
%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb

 You do not need to register the assembly, if you are going to use it directly as a .NET assembly,
   e.g. from C#, VB.NET or PowerShell.



using System;
using WinSCP;
class cShare_Example{
    public static int Main(){
        try{// Setup session options
            SessionOptions sessionOptions = new SessionOptions{
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            }; 
            using (Session session=new Session()){// Connect
                session.Open(sessionOptions); 
                RemoteDirectoryInfo directory = session.ListDirectory("/home/martin/public_html"); 
                foreach (RemoteFileInfo fileInfo in directory.Files){
                    Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
                        fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
                }
            } 
            return 0;   
        }catch (Exception e){
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}



-------- pass local dir into winscp script

@echo ======================================= >> log.txt
@echo Running %0 at %date% %time% >> log.txt
@echo ======================================= >> log.txt
@echo Exporting and uploading project data >> log.txt
sqlplus -silent dwtarchea/dwParget@MYSAVEDSITECONINFO @TMC_ProjectData.sql > TMC_Proj_Data.csv
winscp.com /script=winscp_script.txt /log="winscp_script.log" /parameter %CD%

---- winscp_script.txt -----
option confirm off
open TMC_Project
lcd %1%
put TMC_Proj_Data.csv
close
exit


Comments

Popular Posts