Skip to main content

Automatic upload (script + scheduler) - Windows

Learn to automate SFTP file transfers with a batch script and schedule them using Windows Task Scheduler.

Written by Manon Champion

1. Script for SFTP upload

This script automates the transfer of files from a local directory to a remote SFTP server using a private SSH key for authentication.

Instructions

  • Creating the script:

    1. Copy the template script into your text editor (Notepad, Notepad++, Visual Studio Code, etc.).

    2. Modify the "Custom variables - the only part to configure" section.

@echo offREM === Custom variables - the only part to configure ===
set LOCAL_DIR=C:\FolderOnYourServer\
set ARCHIVE_DIR=C:\ArchiveFolderOnYourServer\
set SFTP_DIR=/SFTP_path/
set USERNAME=sftp_user.agicap
set PRIVATE_KEY=C:\FolderOnYourServer\private_key.ppk
set LOG_FILE=C:\FolderOnYourServer\log.log
set WINSCP=C:\Program Files (x86)\WinSCP\winscp.comREM === Basic checks ===
if not exist "%WINSCP%" (
    echo Error: WinSCP is not installed.
    goto :exit
)if not exist "%PRIVATE_KEY%" (
    echo Error: Private key not found.
    goto :exit
)REM === SFTP transfer execution ===
echo. 2> "%LOG_FILE%"
"%WINSCP%" /log="%LOG_FILE%" /command ^
    "open sftp://%USERNAME%@files.agicap.com:2222 -privatekey=""%PRIVATE_KEY%"" -hostkey=""*""" ^
    "put -transfer=auto ""%LOCAL_DIR%\*"" ""%SFTP_DIR%""" ^
    "exit"REM === Post-transfer archiving ===
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
    move /Y "%LOCAL_DIR%\*" "%ARCHIVE_DIR%"
) else (
    echo "The transfer failed, check the log file: %LOG_FILE%"
):exit
timeout /t 10

  • Customization:

    1. LOCAL_DIR : Directory on your server containing files to be sent.

    2. ARCHIVE_DIR : Directory on your server to archive sent files.

    3. SFTP_DIR : Target path on the SFTP server (ask Agicap).

    4. USERNAME : Username provided by Agicap.

    5. PRIVATE_KEY : Path to the SSH private key.

    6. LOG_FILE : Log file to record execution details.

  • Save and execute:

    1. Save the file with a .bat extension (example: sftp_upload_script.bat).

    2. Double-click the file to run the script and verify it works as expected.

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

    1. Search for "Task Scheduler" in the Windows Start menu

    2. Launch the application

  • Create a new scheduled task

    1. In the Action menu, select "Create Task"

  • Configure task settings

    1. 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

    1. Review all configuration details

    2. Click "Finish" to activate the schedule

    3. Your task will now appear in the Task Scheduler library where you can modify settings as needed

Did this answer your question?