How to download today's file using script

Advertisement

GianTrinidad
Guest

How to download today's file using script

Hello all, I would like to know how to build a script to download a file that someone has previously uploaded in server today, I mean not downloading the old ones but only the file with today's date.

Any help is really appreciated. Thanks.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
42,081
Location:
Prague, Czechia

Re: How to download today's file using script

For general scripting, please refer to:
https://winscp.net/eng/docs/guide_automation

And use get command with -filemask switch with time constraint:
get /path/* -filemask=*>1D
or:
get /path/* -filemask=*>=%TIMESTAMP#yyyy-mm-dd%
https://winscp.net/eng/docs/script_download_most_recent_file
https://winscp.net/eng/docs/file_mask
https://winscp.net/eng/docs/scriptcommand_get
https://winscp.net/eng/docs/scripting#timestamp

Reply with quote

geekldy
Guest

issues trying to get file from remote to local directory using a filter and latest file

my script
@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\temp\WinSCP.log" /ini="c:\temp\winscp.ini" ^
  /command ^
    "open sftp://otrp179894:nkqZdcrmi.7b@sftpus1.mft.wextransfer.com/ -hostkey=""ecdsa-sha2-nistp521 521 bEeu0cRwcO3+N2OldezKEeOvY27vltwSexqHVrUmVfs"" -rawsettings FSProtocol=2 DeleteToRecycleBin=1 RecycleBinPath=""/tmp""" ^
    "get -filemask="*.dat=today"  D:\Data\PersonalFolders\Accounting\Fuel\ ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%
the error log:
. 2025-05-27 12:35:06.812 --------------------------------------------------------------------------
. 2025-05-27 12:35:06.812 WinSCP Version 6.5.1 (Build 16311 2025-05-05) (OS 10.0.20348 – Windows Server 2022 Standard)
. 2025-05-27 12:35:06.814 Configuration: c:\temp\winscp.ini
. 2025-05-27 12:35:06.814 Log level: Normal
. 2025-05-27 12:35:06.814 Local account: GSE\administrator
. 2025-05-27 12:35:06.814 Working directory: C:\Temp
. 2025-05-27 12:35:06.814 Process ID: 31956
. 2025-05-27 12:35:06.847 Ancestor processes: WinSCP, cmd, explorer, ...
. 2025-05-27 12:35:06.849 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" /console=6.5.1 /consoleinstance=_28252_635 "/log=C:\temp\WinSCP.log" "/ini=c:\temp\winscp.ini" "/command" "open sftp://otrp179894:***@sftpus1.mft.wextransfer.com/ -hostkey=""ecdsa-sha2-nistp521 521 bEeu0cRwcO3+N2OldezKEeOvY27vltwSexqHVrUmVfs"" -rawsettings FSProtocol=2 DeleteToRecycleBin=1 RecycleBinPath=""/tmp""" "get -filemask=*.dat=today  D:\Data\PersonalFolders\Accounting\Fuel\ ^"
. 2025-05-27 12:35:06.850 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/9/2025, DST End: 11/2/2025
. 2025-05-27 12:35:06.850 Login time: Tuesday, May 27, 2025 12:35:06 PM
. 2025-05-27 12:35:06.850 --------------------------------------------------------------------------
. 2025-05-27 12:35:06.850 Script: Retrospectively logging previous script records:
> 2025-05-27 12:35:06.850 Script: open sftp://otrp179894:***@sftpus1.mft.wextransfer.com/ -hostkey="ecdsa-sha2-nistp521 521 bEeu0cRwcO3+N2OldezKEeOvY27vltwSexqHVrUmVfs" -rawsettings FSProtocol=2 DeleteToRecycleBin=1 RecycleBinPath="/tmp"
. 2025-05-27 12:35:06.850 --------------------------------------------------------------------------
. 2025-05-27 12:35:06.850 Session name: otrp179894@sftpus1.mft.wextransfer.com (Ad-Hoc site)
. 2025-05-27 12:35:06.850 Host name: sftpus1.mft.wextransfer.com (Port: 22)
. 2025-05-27 12:35:06.850 User name: otrp179894 (Password: Yes, Key file: No, Passphrase: No)
. 2025-05-27 12:35:06.850 Tunnel: No
. 2025-05-27 12:35:06.850 Transfer Protocol: SFTP
. 2025-05-27 12:35:06.850 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2025-05-27 12:35:06.850 Disable Nagle: No
. 2025-05-27 12:35:06.850 Proxy: None
. 2025-05-27 12:35:06.850 Send buffer: 262144
. 2025-05-27 12:35:06.850 Compression: No
. 2025-05-27 12:35:06.850 Bypass authentication: No
. 2025-05-27 12:35:06.850 Try agent: Yes; Agent forwarding: No; KI: Yes; GSSAPI: Yes
. 2025-05-27 12:35:06.850 GSSAPI: KEX: No; Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2025-05-27 12:35:06.850 Ciphers: aes,chacha20,aesgcm,3des,WARN,des,blowfish,arcfour; Ssh2DES: No
. 2025-05-27 12:35:06.850 KEX: ntru-curve25519,mlkem-curve25519,mlkem-nist,ecdh,dh-gex-sha1,dh-group18-sha512,dh-group17-sha512,dh-group16-sha512,dh-group15-sha512,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2025-05-27 12:35:06.850 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2025-05-27 12:35:06.850 Simple channel: Yes
. 2025-05-27 12:35:06.850 Return code variable: Autodetect; Lookup user groups: Auto
. 2025-05-27 12:35:06.850 Shell: default
. 2025-05-27 12:35:06.850 EOL: LF, UTF: Auto
. 2025-05-27 12:35:06.851 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2025-05-27 12:35:06.851 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
. 2025-05-27 12:35:06.851 SFTP Bugs: Auto,Auto
. 2025-05-27 12:35:06.851 SFTP Server: default
. 2025-05-27 12:35:06.851 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2025-05-27 12:35:06.851 Cache directory changes: Yes, Permanent: Yes
. 2025-05-27 12:35:06.851 Recycle bin: Delete to: Yes, Overwritten to: No, Bin path: /tmp
. 2025-05-27 12:35:06.851 DST mode: Unix
. 2025-05-27 12:35:06.851 --------------------------------------------------------------------------
< 2025-05-27 12:35:06.851 Script: Searching for host...
. 2025-05-27 12:35:06.852 Looking up host "sftpus1.mft.wextransfer.com" for SSH connection
. 2025-05-27 12:35:06.889 Connecting to 52.12.138.137 port 22
. 2025-05-27 12:35:06.953 Connected to 52.12.138.137 (from 10.0.1.8:53884)
< 2025-05-27 12:35:06.953 Script: Connecting to host...
. 2025-05-27 12:35:06.953 We claim version: SSH-2.0-WinSCP_release_6.5.1
. 2025-05-27 12:35:06.953 Connected to 52.12.138.137 (from 10.0.1.8:53884)
. 2025-05-27 12:35:07.011 Remote version: SSH-2.0-ssh2.0ga
. 2025-05-27 12:35:07.011 Using SSH protocol version 2
. 2025-05-27 12:35:07.015 Have a known host key of type ecdsa-sha2-nistp521
. 2025-05-27 12:35:07.020 Doing ECDH key exchange with curve nistp256, using hash SHA-256
. 2025-05-27 12:35:07.456 Server also has rsa-sha2-512/rsa-sha2-256 host keys, but we don't know any of them
. 2025-05-27 12:35:07.456 Host key fingerprint is:
. 2025-05-27 12:35:07.456 ecdsa-sha2-nistp521 521 SHA256:bEeu0cRwcO3+N2OldezKEeOvY27vltwSexqHVrUmVfs
< 2025-05-27 12:35:07.456 Script: Authenticating...
. 2025-05-27 12:35:07.469 Host key matches cached key
. 2025-05-27 12:35:07.470 Initialised AES-256 SDCTR (AES-NI accelerated) [aes256-ctr] outbound encryption
. 2025-05-27 12:35:07.470 Initialised HMAC-SHA-256 outbound MAC algorithm
. 2025-05-27 12:35:07.470 Initialised AES-256 SDCTR (AES-NI accelerated) [aes256-ctr] inbound encryption
. 2025-05-27 12:35:07.470 Initialised HMAC-SHA-256 inbound MAC algorithm
! 2025-05-27 12:35:07.533 Using username "otrp179894".
< 2025-05-27 12:35:07.533 Script: Using username "otrp179894".
. 2025-05-27 12:35:07.591 Server offered these authentication methods: password,publickey,keyboard-interactive
. 2025-05-27 12:35:07.591 Attempting keyboard-interactive authentication
. 2025-05-27 12:35:07.644 Prompt (keyboard interactive, "SSH server: password", "Enter password for otrp179894", "Password:")
. 2025-05-27 12:35:07.644 Using stored password.
< 2025-05-27 12:35:07.645 Script: Authenticating with pre-entered password.
. 2025-05-27 12:35:07.718 Access granted
. 2025-05-27 12:35:07.719 Opening main session channel
. 2025-05-27 12:35:07.770 Opened main channel
. 2025-05-27 12:35:07.818 Started a shell/command
< 2025-05-27 12:35:07.818 Script: Authenticated.
. 2025-05-27 12:35:07.818 --------------------------------------------------------------------------
. 2025-05-27 12:35:07.818 Using SFTP protocol.
. 2025-05-27 12:35:07.818 Doing startup conversation with host.
< 2025-05-27 12:35:07.818 Script: Starting the session...
. 2025-05-27 12:35:07.818 Not well known server, limiting to safe SFTP version 3.
> 2025-05-27 12:35:07.818 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2025-05-27 12:35:08.009 Type: SSH_FXP_VERSION, Size: 106, Number: -1
. 2025-05-27 12:35:08.009 SFTP version 3 negotiated.
. 2025-05-27 12:35:08.009 Unknown server extension newline@vandyke.com="\n"
. 2025-05-27 12:35:08.009 Server software: Maverick SSHD 1.7.34 (0) by JADAPTIVE Limited
. 2025-05-27 12:35:08.009 We believe the server has signed timestamps bug
. 2025-05-27 12:35:08.009 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
> 2025-05-27 12:35:08.009 Type: SSH_FXP_EXTENDED, Size: 63, Number: 200
. 2025-05-27 12:35:08.009 Getting current directory name.
. 2025-05-27 12:35:08.009 Getting real path for '.'
> 2025-05-27 12:35:08.009 Type: SSH_FXP_REALPATH, Size: 10, Number: 272
< 2025-05-27 12:35:08.063 Type: SSH_FXP_STATUS, Size: 64, Number: 200
. 2025-05-27 12:35:08.063 Discarding reserved response
< 2025-05-27 12:35:08.063 Type: SSH_FXP_NAME, Size: 43, Number: 272
. 2025-05-27 12:35:08.063 Real path is '/'
. 2025-05-27 12:35:08.063 Startup conversation with host finished.
< 2025-05-27 12:35:08.063 Script: Session started.
< 2025-05-27 12:35:08.063 Script: Active session: [1] otrp179894@sftpus1.mft.wextransfer.com
> 2025-05-27 12:35:08.064 Script: get -filemask=*.dat=today  D:\Data\PersonalFolders\Accounting\Fuel\ ^
. 2025-05-27 12:35:08.064 Listing file "D:\Data\PersonalFolders\Accounting\Fuel\".
> 2025-05-27 12:35:08.064 Type: SSH_FXP_LSTAT, Size: 50, Number: 519
< 2025-05-27 12:35:08.267 Type: SSH_FXP_STATUS, Size: 83, Number: 519
< 2025-05-27 12:35:08.267 Status code: 2, Message: 519, Server: /D:\Data\PersonalFolders\Accounting\Fuel\ is not a valid file path, Language:  
< 2025-05-27 12:35:08.274 Script: Can't get attributes of file 'D:\Data\PersonalFolders\Accounting\Fuel\'.
< 2025-05-27 12:35:08.274 No such file or directory.
< 2025-05-27 12:35:08.274 Error code: 2
< 2025-05-27 12:35:08.274 Error message from server: /D:\Data\PersonalFolders\Accounting\Fuel\ is not a valid file path
. 2025-05-27 12:35:08.274 Script: Failed
. 2025-05-27 12:35:08.274 Script: Exit code: 1
. 2025-05-27 12:35:08.274 Closing connection.
. 2025-05-27 12:35:08.274 Sending special code: 1
. 2025-05-27 12:35:08.320 Session sent command exit status 0
. 2025-05-27 12:35:08.320 Main session channel closed
. 2025-05-27 12:35:08.320 All channels closed
Can anyone see what I'm doing wrong?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
42,081
Location:
Prague, Czechia

Re: issues trying to get file from remote to local directory using a filter and latest file

@geekldy: You have several syntactical problems in your get command. And additionally, there's nothing like "equals today" operator. There are only before/after operators.
A possible correct syntax is:
    "get -filemask=*.dat>=today * D:\Data\PersonalFolders\Accounting\Fuel\" ^

Reply with quote

Advertisement

You can post new topics in this forum