Script for copy files, move if succes and create log

Advertisement

grego
Joined:
Posts:
2
Location:
Lodz, Poland

Script for copy files, move if succes and create log

Hello All,
I have a problem with creating a script. It should copy local files to remote server and if success move to another folder and create a log with a format: filename Done!
I used some different script based on windows bash and created two scripts.
First:
@echo off
SETLOCAL
set file=x:\mypathtolog\*.log

set TRIES=10
set INTERVAL=30
set REMOTE_PATH=/remotepath/

:retry

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="winsclogpath\!Y!M!D_Log.log" /ini=nul ^
  /command ^
    "open ftp://myftpip/" ^
    "put *.* %REMOTE_PATH%" ^
    "exit"
   
set WINSCP_RESULT=%ERRORLEVEL%

if %WINSCP_RESULT% equ 0 (

   echo Upload succeeded, moving local files
   move mylocalpath\*.* mylocalpathdone\
   
   exit /b 0

) else (
   set /A TRIES=%TRIES%-1
   if %TRIES% gtr 1 (
      echo Failed, retrying in %INTERVAL% seconds...
      timeout /t %INTERVAL%
      goto retry
   ) else (
      echo Failed, aborting
      exit /b 1
   )
)

exit /b %WINSCP_RESULT%

This code working fine but.... If broken connection try to reconnect and copy files. There are some issues with this. There is not possible to create own format log. If no connection trying to reconnect and start puting all files again. If no success cannot move files to another directory.

Second one:
@echo off
SETLOCAL
SET TRIES=10
SET INTERVAL=30
SET REMOTE_PATH=/remotepath/
SET MyLogOK=mylogpath\MyLogOK.txt
SET MyLogERR=myerrorlogpath\MyLogOK.txt
SET LOCAL_PATH=localpath
SET LOCAL_PATH_DONE=localpathdone\

set file=%LOCAL_PATH%\*.log

FOR %%i IN ("%file%") DO (

   "C:\Program Files (x86)\WinSCP\WinSCP.com" ^
    /log="pathtolog\log\!Y!M!D_Log.log" /ini=nul ^
    /command ^
    "open ftp://ftpserverip" ^
   "put %%~di%%~pi%%~ni%%~xi %REMOTE_PATH%" ^
   "exit"

   if %ERRORLEVEL%   equ 0 (
      echo Upload succeeded, moving local files
      move %%~di%%~pi%%~ni%%~xi %LOCAL_PATH_DONE%%%~ni%%~xi
      echo %%~ni%%~xi   DONE! >> %MyLogOK%
   ) else (
      echo %%~ni%%~xi   ERROR! >> %MyLogERR%
   )
)

exit /b 0

This one is almost working as I need but to copy each file in directory I need to start a new session of winscp and create a new connection.
Do you have any idea how to combine these scripts to make one or how to create script with functionality what I want?
Best regards
Grzegorz

Reply with quote

Advertisement

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

Re: Script for copy files, move if succes and create log

I'm not sure I understand your problem.
Is the problem with the first script that you want to create a simple log of successfully transferred files?
You can use XML logging and that convert that XML to any format you want.
https://winscp.net/eng/docs/logging_xml

But I'd recommend you to use WinSCP .NET assembly from a PowerShell script.
Your code would be a way more efficient and robust.
https://winscp.net/eng/docs/library_powershell

Reply with quote

grego
Joined:
Posts:
2
Location:
Lodz, Poland

Re: Script for copy files, move if succes and create log

martin wrote:

I'm not sure I understand your problem.
Is the problem with the first script that you want to create a simple log of successfully transferred files?
You can use XML logging and that convert that XML to any format you want.
https://winscp.net/eng/docs/logging_xml

But I'd recommend you to use WinSCP .NET assembly from a PowerShell script.
Your code would be a way more efficient and robust.
https://winscp.net/eng/docs/library_powershell

Thank you for your reply. I supposed your second link will be much better to me.
My goal is to create a script which put files to remote ftp but I want to know if a transfer was success or fail for each file. If success must save specific data to a log file and move a local file to another directory. First script is working fine but for ALL files. If connection break it start again and try to transfer ALL files again. When finish move ALL files and not possible to save a log.
Second put each file to remote server, save log but need a new ftp connection to each file in directory. If I have 100 files, 1000 files it need to create 100 connections, 1000 connections. It need a time.
The best script:
1. connect to remote ftp
2. start loop
3. put file
4. if success save log and move file to another directory
5. if error try to put again
6. end loop
7. quit

I will try to use power shell script,
Thank you.
Best regards,
Grzegorz

Reply with quote

Advertisement

You can post new topics in this forum