Windows:ftpbatch

From wiki
Revision as of 11:20, 6 December 2019 by Hdridder (talk | contribs) (Created page with "Sample script to SFTP to other systems. Not sure if this must be this complicated but it works. <syntaxhighlight lang=bat> ;@ECHO OFF ;SETLOCAL ; ;REM #######################...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Sample script to SFTP to other systems. Not sure if this must be this complicated but it works.

;@ECHO OFF
;SETLOCAL
;
;REM ##############################################################################
;REM #
;REM # Operating System: Windows 2008
;REM # Purpose: 
;REM #
;REM # to ensure that this script can be successfully invoked through the scheduler make sure:
;REM #   - there is no relative path within this script, all paths in this script should be absolute paths.
;REM #   - no mapped drives are used (e. g. X-drive), they may not be reached through the scheduler.
;REM #
;REM # URL's to code examples:
;REM # http://www.dostips.com/DtTipsFtpBatchScript.php   # Batch + FTP scripting with variables using ; lines
;REM # http://www.gutefrage.net/frage/ms-dos-aktuelles-datum-als-variable-im-format-yyyy-mm-tt
;REM # http://www.robvanderwoude.com/unixports.php#TEE
;REM # http://stackoverflow.com/questions/8701871/calling-sftp-process-from-vba-code   # CMD /c ECHO y 
;REM #
;REM #
;REM #
;REM ##############################################################################
;REM 
;REM System         Contact Person
;REM 
;REM  Ver  Update        Who    	Modification
;REM -------------------------------------------------------

;SET BASE_PATH=<Directorypath>
;SET SFTP_EXE="C:\PuTTY\psftp.exe"
;SET ZBIN="c:\Program Files\7-Zip\7z.exe"
;
;SET SCRIPT_NAME=%~n0
;SET CURRENT_FILE=%~f0
;SET YYYY=%date:~-4,4%
;SET   DD=%date:~-7,2%
;SET   MM=%date:~-10,2%
;SET  DAY=%date:~0,3%
;SET CURRENT_DATE=%DATE%
;SET CURRENT_TIME=%TIME%
;SET TIME_STAMP=%YYYY%%MM%%DD%
;
;SET SCRIPT_PATH=%BASEPATH%<morePath>
;SET LOGFILE=%BASEPATH%<morePath>
;SET TEMP_DIR=%BASEPATH%\temp
;SET TEMP_FILE=%TEMP_DIR%\%SCRIPT_NAME%.temp
;REM SET PRIVATE_KEY=<privatekeylocation>
;SET PRIVATE_KEY=<privatekeylocation>
;REM change to local directory of the script
;REM is needed if script is started e. g. from within Notepad++
;CD c:\tmp
;
;REM SET SFTP_SERVER=<IPaddress>
;SET SFTP_SERVER=<IPaddress>
;SET USERNAME=<username>
;SET LOCAL_DIR=%BASEPATH%<morePath>
;
;SET REMOTE_DIR=<directorypath>
;SET SEARCH_STRING="*%DD%%MM%%YYYY%*"
;
;REM check if directory %TEMP_DIR% exists and create it if it not exists...
;IF EXIST %TEMP_DIR% ( echo. ) ELSE ( mkdir %TEMP_DIR% && ECHO %TEMP_DIR% created)
;
;ECHO ##### File Transfer Started at %CURRENT_DATE% %CURRENT_TIME% ##### | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;
;REM if no files are found in local directory start file transfer, otherwise skip file transfer...
;IF EXIST %LOCAL_DIR%\%SEARCH_STRING% (
;ECHO "%SCRIPT_NAME% : Skipping file transfer, because files '%SEARCH_STRING%' already exist in local directory..." | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;GOTO :EOF
;)
;
;REM #######################################################################
;REM #                                                                     #
;REM # You shouldn't need to change anything below this comment            #
;REM #                                                                     #
;REM #######################################################################
;
;DEL /Q %LOCAL_DIR%\*.*

;(for /f "usebackq delims=" %%A in ("%CURRENT_FILE%") do call echo.%%A)>%TEMP_FILE%
;
;ECHO using filter: %SEARCH_STRING% | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;
;REM calls the SFTP command of PuTTY using the generated temporary file as command input...
;REM psftp -h  
;REM   -b file   use specified batchfile
;REM   -pw passw login with specified password
;REM   -1 -2     force use of particular SSH protocol version
;REM   -C        enable compression
;REM   -i key    private key file for authentication
;
;REM %SFTP_EXE% -b %TEMP_FILE% -2 -C %USERNAME%@%SFTP_SERVER% -pw %PASSWORD% | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;CMD /c ECHO y | %SFTP_EXE% -b %TEMP_FILE% -2 -C -i %PRIVATE_KEY% %USERNAME%@%SFTP_SERVER% | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;
;REM type gzip can only compress 1 file, tar first.
;%ZBIN% -y a -ttar %LOCAL_DIR%\2G_topo_%TIME_STAMP%.tar  %LOCAL_DIR%\*.*
;%ZBIN% -y a -tgzip %LOCAL_DIR%\2G_topo_%TIME_STAMP%.tgz  %LOCAL_DIR%\2G_topo_%TIME_STAMP%.tar
;DEL /Q %LOCAL_DIR%\2G_topo_%TIME_STAMP%.tar
;
;REM remove the temporary generated SFTP command file again...
;REM DEL "%TEMP_FILE%"
;
;SET CURRENT_DATE=%DATE%
;SET CURRENT_TIME=%TIME%
;ECHO ##### File Transfer Finished at %CURRENT_DATE% %CURRENT_TIME% ##### | %SCRIPT_PATH%\tee.bat -a %LOGFILE%
;
;REM PAUSE --> do not use this command if script is called by scheduler!
;GOTO:EOF
;REM #######################################################################
;ENDLOCAL

lcd %LOCAL_DIR%
cd %REMOTE_DIR%
mget %SEARCH_STRING%
close
bye