Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Unable to use dateTime stamp in log file name

Using a batch file for such complicated code is a bad idea. Use some more powerful language, like PowerShell.
Then you can use WinSCP .NET assembly.
You code will be way more readable and error prone.
See https://winscp.net/eng/docs/library_powershell
ashish.atwal

Re: Unable to use dateTime stamp in log file name

Hi Martin,

I am explaning my code again so that you get some idea :

I have made a bat file named "Transfer.bat" in which I am scanning each folder of source for .txt file and transfer the files to the destination folder (source and destination folder names are exactly same)
My Transfer.bat contains the following code :
FOR /R "C:\source\" %%G IN (*.txt) DO ( 

FOR /F %%i in ("%%G") do (
set dest_path=%%~pi
set dest_path=!dest_path:\=,!

FOR %%i in (!dest_path!) do (
echo %%i
set dest_folder=%%i

)
)
"C:\Program Files (x86)\WinSCP\winscp.exe" /ini=nul /log='C:\WinSCPScript\DateWiseTest\File_Put_!M!D!Y.log' /script=C:\WinSCPScript\DateWiseTest\File_Put.txt /parameter // %%G /!dest_folder!/
)

exit /b 0

so in the above script scan all folders under "source" folder and it is sending two parameters to "File_Put.txt"
1. %%G :- the current txt file name.
2. dest_folder :- folder name contains the txt file (source and destination folder are same for each file)

and my "File_Put.txt" is just making the SFTP connection to server and sending the file to destination folder as shown below :

open sftp://"*****":"****"***** 

option transfer binary
put -nopermissions -nopreservetime %1% %2%
close
exit


where %1% and %2% are the parameters passed from "Transfer.bat".
connection details are hidden but are working fine at my end.

Your help is needed in the following queries :

- As 'Transfer.bat' is scanning each folder and uses 'File_Put.txt' to SFTP file to the server if any found, my question is how do I confirm back in my 'Transfer.bat' that my file is transferred so that I can move that file to a backup folder.
- As for now for each file 'File_Put.txt' will be called which will open and close the connection every time. Can we have some better approach in which firstly it'll open the connection then scan the whole folders recursively, transfers the file and in the end closes the connection?

Hope I am able to make you understand what I wanted to do. Could you please help me in this? Thanx in advance :)
martin

Re: Unable to use dateTime stamp in log file name

Could you please elaborate "delayed expansion"?

That has nothing to do with WinSCP.
The first hit in google:
https://ss64.com/nt/delayedexpansion.html
ashish.atwal

Re: Unable to use dateTime stamp in log file name

martin wrote:

I assume you have a delayed expansion enabled in your batch file (although your code snippet does not show that).
With delayed expansion, you have to escape the ! with ^.
Also, you have to use double quotes.
/log="C:\WinSCPScript\DateWiseTest\File_Put_^!M^!D^!Y.log"


Thanx buddy it solved my problem :). Could you please elaborate "delayed expansion"?
martin

Re: Unable to use dateTime stamp in log file name

I assume you have a delayed expansion enabled in your batch file (although your code snippet does not show that).
With delayed expansion, you have to escape the ! with ^.
Also, you have to use double quotes.
/log="C:\WinSCPScript\DateWiseTest\File_Put_^!M^!D^!Y.log"
ashish.atwal

Unable to use dateTime stamp in log file name

Hi Team,

I have made SFTP connection in file named "File_Put.txt" which is working fine

I have made a bat file named "Transfer.bat" in which I am scanning each folder of source for .txt file and transfer the files to the destination folder (source and destination folder names are exactly same)
My Transfer.bat contains the following code :
FOR /R "C:\source\" %%G IN (*.txt) DO (
FOR /F %%i in ("%%G") do (
set dest_path=%%~pi
set dest_path=!dest_path:\=,!

FOR %%i in (!dest_path!) do (
echo %%i
set dest_folder=%%i

)
)
"C:\Program Files (x86)\WinSCP\winscp.exe" /ini=nul /log='C:\WinSCPScript\DateWiseTest\File_Put_!M!D!Y.log' /script=C:\WinSCPScript\DateWiseTest\File_Put.txt /parameter // %%G /!dest_folder!/
)

exit /b 0


so in the above script scan all folders under "source" folder and it is sending two parameters to "File_Put.txt"
1. %%G :- the current txt file name.
2. dest_folder :- folder name contains the txt file (source and destination folder are same for each file)

and my "File_Put.txt" is just making the SFTP connection to server and sending the file to destination folder as shown below :

open sftp://"*****":"****"*****
option transfer binary
put -nopermissions -nopreservetime %1% %2%
close
exit


where %1% and %2% are the parameters passed from "Transfer.bat".
connection details are hidden but are working fine at my end.

Now the problem is that if you observe the below line in the "Transfer.bat" :

"C:\Program Files (x86)\WinSCP\winscp.exe" /ini=nul /log='C:\WinSCPScript\DateWiseTest\File_Put_!M!D!Y.log' /script=C:\WinSCPScript\DateWiseTest\File_Put.txt /parameter // %%G /!dest_folder!/

in the bold area if I name my log file as "File_Put.log" then it runs fine but when I want to apply DateTime stamps to it as already done like "File_Put_!M!D!Y.log" then it is not running it throws error as attached.

I am very sure that it is taking wrong parameters to File_Put.txt which throws exception.

Please help me to resolve this issue as my target is to generate session log file date wise daily.

Regards,
Ashish Singh Atwal