1. Script for SFTP download
Automate the retrieval of files from a remote SFTP server to a local directory using a private SSH key for authentication.
Instructions
Creating the script:
Copy the template script into your text editor (Notepad, Notepad++, Visual Studio Code, etc.).
Modify the "Custom variables - the only part to configure" section.
@echo offREM === Custom variables - the only part to configure ===
set SFTP_DIR=/SFTP_path/
set ARCHIVE_SFTP_DIR=/SFTP_archive_path/archives/
set LOCAL_DIR=C:\LocalFolder\
set USERNAME=sftp_user
set PRIVATE_KEY=C:\LocalFolder\private_key.ppk
set LOG_FILE=C:\LocalFolder\log.log
set WINSCP=C:\Program Files (x86)\WinSCP\winscp.comREM === SFTP download execution ===
echo. 2> "%LOG_FILE%"
"%WINSCP%" /log="%LOG_FILE%" /command ^
"open sftp://%USERNAME%@files.agicap.com:2222 -privatekey=""%PRIVATE_KEY%"" -hostkey=""*""" ^
"get -filemask=""!*/"" ""%SFTP_DIR%*"" ""%LOCAL_DIR%""" ^
"exit"REM === Post-download archiving ===
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
"%WINSCP%" /log="%LOG_FILE%" /command ^
"open sftp://%USERNAME%@files.agicap.com:2222 -privatekey=""%PRIVATE_KEY%"" -hostkey=""*""" ^
"mv ""%SFTP_DIR%*"" ""%ARCHIVE_SFTP_DIR%""" ^
"exit"
if %ERRORLEVEL% equ 0 (
echo Success: Files archived in %ARCHIVE_SFTP_DIR%
) else (
echo ERROR: Archiving failed (Code: %ERRORLEVEL%)
)
) else (
echo ERROR: Download failed (Code: %WINSCP_RESULT%). Check the log file: %LOG_FILE%
):exit
timeout /t 10
Customization:
LOCAL_DIR : Local directory where files will be downloaded.
ARCHIVE_SFTP_DIR : Directory for archiving files on the SFTP server.
SFTP_DIR : Source path of files on the SFTP server (ask Agicap).
USERNAME : Username provided by Agicap.
PRIVATE_KEY : Path to the SSH private key.
LOG_FILE : Log file to track script execution.
Save and execute:
Save the file with a .bat extension (example: sftp_download_script.bat).
Double-click to run the script and verify it works properly.
2. Script scheduling
After creating and validating your script, you can set up automatic execution using Windows Task Scheduler. This guide explains how to configure scheduled tasks for your scripts.
Directory structure
Recommended file organization:
C:\Agicap\ ├── Logs\ (for log files) ├── Exports\ (for files awaiting transfer) ├── Archives\ (for backup of transferred files) ├── Scripts\ (put your bat scripts) ├── Keys\ (put your private and public keys)
Setting up automated task execution
Open Task Scheduler
Search for "Task Scheduler" in the Windows Start menu
Launch the application
Create a new scheduled task
In the Action menu, select "Create Task"
Configure task settings
Basic Information
Assign a meaningful name and description
Schedule settings
Select "Daily" frequency
Choose your desired start time (e.g., 5:30 AM)
Confirm "Repeat every 1 day" is selected
Action configuration
Choose "Start a program"
Browse to and select your script file
Finalize setup
Review all configuration details
Click "Finish" to activate the schedule
Your task will now appear in the Task Scheduler library where you can modify settings as needed
