@echo off
setlocal
REM full path for teraterm macro
set TTMACRO_PATH="C:\Program Files (x86)\teraterm5\ttpmacro.exe"
REM full path for winscp
set WINSCP_PATH="C:\Program Files (x86)\WinSCP\WinSCP.com"
REM server address, account
set HOSTNAME=127.0.0.1
set PORT=22
set USERNAME=root
set PASSWORD=root
REM execute command on server
set COMMAND="echo 'abc' > /home/root/file_1234"
REM create output directory
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%i in ('"wmic os get localdatetime /value"') do set datetime=%%in
set YEAR=!datetime:~0,4!
set MONTH=!datetime:~4,2!
set DAY=!datetime:~6,2!
set HOUR=!datetime:~8,2!
set MINUTE=!datetime:~10,2!
set SECOND=!datetime:~12,2!
set FORMAT_DATETIME=%YEAR%%MONTH%%DAY%_%HOUR%%MINUTE%%SECOND%
set WORKDIR=%~dp0
set OUTDIR=%WORKDIR%out_%FORMAT_DATETIME%\
REM connect ssh server and execute command
%TTMACRO_PATH% sshv2_login_and_command.ttl %HOSTNAME% %PORT% %USERNAME% %PASSWORD% %COMMAND%
REM params for winscp command
set REMOTE_PATH=/home/root/file*
set LOCAL_PATH=%OUTDIR%file*
If not exist %LOCAL_PATH% mkdir %LOCAL_PATH%
REM execution winscp command
%WINSCP_PATH% /command ^
"open sftp://%USERNAME%:%PASSWORD%@%HOSTNAME%" ^
"get %REMOTE_PATH% %LOCAL_PATH%" ^
"exit"
start explorer "%OUTDIR%"
::set LOG_PATH=C:\work\workspace\tools\ssh_fileget\file.log
::%WINSCP_PATH% /log=%LOG_PATH% /command ^
:: "open sftp://%USERNAME%:%PASSWORD%@%HOSTNAME%" ^
:: "get %REMOTE_PATH% %LOCAL_PATH%" ^
:: "exit"
endlocal