Unable to use dateTime stamp in log file name

Advertisement

ashish.atwal
Joined:
Posts:
3
Location:
Noida

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

winSCPerror.jpg

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,390
Location:
Prague, Czechia

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"

Reply with quote

ashish.atwal
Joined:
Posts:
3
Location:
Noida

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"?

Reply with quote

ashish.atwal
Joined:
Posts:
3
Location:
Noida

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 :)

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum