Source file is moving to remote server directory, but content of the file is not exactly the same

Advertisement

avles
Donor
Joined:
Posts:
1
Location:
Singapore

Source file is moving to remote server directory, but content of the file is not exactly the same

Hi All,
I am new to the WinSCP batch script. I have the below script to transfer the file to the remote server directory. My scope is to transfer the file to the remote server directory and copy the file to the archive folder on the same server. The current script is helping to move the file to the remote server directory, but the content of the transferred file coming with the below script. The exact content of the file is "sample test," but its appearing as below.
Also, the file is not copying to the archive folder on the same server. Please help with this. I really appreciate any help you can provide.

File content appearing as below in remote server:
@batch
ECHO cd "/agency1/office1/hr1/out/" >> %SCRIPTFILE%
ECHO lcd "%SFTP_HOME%scripts\agency\office\hr\out\"" >> %SCRIPTFILE%
ECHO put -delete "%SFTP_HOME%scripts\agency\office\hr\out\*.txt" >> %SCRIPTFILE%

This is the script
@echo off
::: Begin set date
 
for /f "tokens=1-4 delims=/-. " %%i in ('date /t') do (call :set_date %%i %%j %%k %%l)
goto :end_set_date
 
:set_date
if "%1:~0,1%" gtr "9" shift
for /f "skip=1 tokens=2-4 delims=(-)" %%m in ('echo,^|date') do (set %%m=%1&set %%n=%2&set %%o=%3)
goto :eof
 
:end_set_date
::: End set date
 
SET CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
SET CUR_NN=%time:~3,2%
SET CUR_SS=%time:~6,2%
SET CUR_MS=%time:~9,2%
 
SET SFTP_HOME=E:\sftp\
SET SCRIPTFILE=%SFTP_HOME%\scripts\agency\office\hr\out\sample.txt
SET LOGFILENAME=%SFTP_HOME%scripts\agency\SFTPLogs\hr\sample-out_%yy%%mm%%dd%.log
ECHO SFTP_HOME=%SFTP_HOME%
ECHO SCRIPTFILE=%SCRIPTFILE%
ECHO LOGFILENAME=%LOGFILENAME%
 
ECHO Copy File to Archive Folder
 
COPY %SFTP_HOME%\scripts\agency\office\hr\out\*.txt "E:\sftp\agency\archive\hr\out\"
 
ECHO Write WinSCP Script File
 
ECHO option confirm off >> %SCRIPTFILE%
ECHO option batch continue >> %SCRIPTFILE%
 
ECHO cd "/agency1/office1/hr1/out/" >> %SCRIPTFILE%
ECHO lcd "%SFTP_HOME%scripts\agency\office\hr\out\"" >> %SCRIPTFILE%
ECHO put -delete "%SFTP_HOME%scripts\agency\office\hr\out\*.txt" >> %SCRIPTFILE%
 
 
 
ECHO Start to push files to IT using WinSCP
 
"C:\Program Files (x86)\WinSCP\WinSCP.exe" agency@dc.com /privatekey="E:\key\agency.ppk" /hostkey="*" /script=%SCRIPTFILE% /log=%LOGFILENAME%
 
ECHO End to push files from App to IT using WinSCP
 
DEL %SCRIPTFILE%

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
36,025
Location:
Prague, Czechia

Re: Source file is moving to remote server directory, but content of the file is not exactly the same

I'm not sure I follow. Anyway, what I see in your batch file: You are uploading all *.txt files from %SFTP_HOME%\scripts\agency\office\hr\out folder to the server. At the same time you are generating a WinSCP script file sample.txt in the same folder. So you in the end your WinSCP script is uploading itself to the server. That possibly the problem you are facing. If you do not want that, do not create the script in the %SFTP_HOME%\scripts\agency\office\hr\out folder (or do not give it .txt extension). Actually, your script file is not even dynamic, so there's no need to generate it on the fly in the batch file. You can use fixed pre-made script file.

Regarding the archive folder: Why do you think your script should copy the file to an archive folder? I do not see any attempt to do that in your code.

Note that your generate WinSCP script file misses exit command. So the WinSCP.exe process will never finish. Also, in a batch file, you should use WinSCP.com, not WinSCP.exe.
See https://winscp.net/eng/docs/executables

Reply with quote

Advertisement

You can post new topics in this forum