Skip to content

Instantly share code, notes, and snippets.

@ronknight
Last active May 20, 2024 22:17
Show Gist options
  • Save ronknight/94c12a368cc788770194af7cc0e42bda to your computer and use it in GitHub Desktop.
Save ronknight/94c12a368cc788770194af7cc0e42bda to your computer and use it in GitHub Desktop.
download unsubcribe.log using ftp
USERNAME: username
PASSWORD: password
SERVER:server_address
REMOTE_FILE: The_path_to_the_remote_file_on_the_FTP_server
LOCAL_PATH: The_local_directory_where_the_downloaded_file_should_be_stored
@echo off
setlocal enabledelayedexpansion
REM Load environment variables from .env file
for /f "delims=" %%a in (.env) do (
set "%%a"
)
REM Get current date and time in YYYYMMDD_HHMMSS format
for /f "tokens=2 delims==" %%G in ('wmic OS Get localdatetime /value') do set "datetime=%%G"
set "datestamp=!datetime:~0,4!-!datetime:~4,2!-!datetime:~6,2!_!datetime:~8,2!-!datetime:~10,2!-!datetime:~12,2!"
REM Construct local path with timestamp
set LOCAL_PATH_WITH_TIMESTAMP=%LOCAL_PATH%\%datestamp%
REM Create directory if it doesn't exist
if not exist "%LOCAL_PATH_WITH_TIMESTAMP%" mkdir "%LOCAL_PATH_WITH_TIMESTAMP%"
REM Connect to FTP server and download the file
echo user %USERNAME%>%TEMP%\ftp.txt
echo %PASSWORD%>>%TEMP%\ftp.txt
echo binary>>%TEMP%\ftp.txt
echo get %REMOTE_FILE% "%LOCAL_PATH_WITH_TIMESTAMP%\unsubscribe.log">>%TEMP%\ftp.txt
echo quit>>%TEMP%\ftp.txt
ftp -n -s:%TEMP%\ftp.txt %SERVER%
del %TEMP%\ftp.txt
REM Done
echo File downloaded successfully to: %LOCAL_PATH_WITH_TIMESTAMP%\unsubscribe.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment