0

I am trying to write the batch script but not sure how to check the below condition.

I want to read all 3-digit number's folders and the 'data' folder only as first level directory, and inside these folders there are date folders, and sync only 'ping' and 'event' folders files.

folder struture-

Also, is there any way to sync the date range instead of using the DAYS variable?

@echo off
 
SET SOURCE_PATH=Z:\Logs
SET DEST_PATH=/mnt/logs/test
 
set DAYS=2
 
for /l %%D in (1, 1, %DAYS%) do (
for /d %%i in (%SOURCE_PATH%) do (
REM synchronize folder from network drive to FTP
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Users\rjdhld\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://username:%%21o%%26mH%%[email protected]/ -hostkey=""ssh-ed29 255 s8HgnuJoit0""" ^
    "option batch continue" ^
    "mkdir %DEST_PATH%/%%~nxi" ^
    "mkdir %DEST_PATH%/%%~nxi/%%TIMESTAMP-%%DD#yyyy-mm-dd%%" ^
    "mkdir %DEST_PATH%/%%~nxi/%%TIMESTAMP-%%DD#yyyy-mm-dd%%/ping" ^
    "mkdir %DEST_PATH%/%%~nxi/%%TIMESTAMP-%%DD#yyyy-mm-dd%%/event" ^
    "option batch abort" ^
    "synchronize remote %%~fi\%%TIMESTAMP-%%DD#yyyy-mm-dd%%\ping %DEST_PATH%/%%~nxi/%%TIMESTAMP-%%DD#yyyy-mm-dd%%/ping" ^
    "synchronize remote %%~fi\%%TIMESTAMP-%%DD#yyyy-mm-dd%%\event %DEST_PATH%/%%~nxi/%%TIMESTAMP-%%DD#yyyy-mm-dd%%/event" ^
    "exit"
    )
)
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
PAUSE
exit /b %WINSCP_RESULT%
0

1 Answer 1

2
@ECHO OFF
SETLOCAL
SET "source_path=u:\your files"
SET DEST_PATH=/mnt/logs/test
 
set DAYS=2
SET /a winscp_result=0
 
for /l %%D in (1, 1, %DAYS%) do (
 for /d %%i in ("%SOURCE_PATH%\*") do (
  rem name of first-levelsubdirectory
  SET "processme="
  FOR /f "tokens=3delims=\" %%e IN ("%%i") do (
   FOR /L %%y IN (1000,1,1999) DO IF "%%y"=="1%%e" SET "processme=y"
   FOR    %%y IN (data) DO IF /i "%%y"=="%%e" SET "processme=y"
  )
  IF DEFINED processme (
   ECHO process %%i
rem CALL seterr 7   
   IF ERRORLEVEL 1 ECHO fail %%D FOR %%i&SET /a WINSCP_RESULT+=1
  ) 
 )
)
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
rem PAUSE
rem exit /b %WINSCP_RESULT%
GOTO :EOF

You would need to change the value assigned to source_path to suit your circumstances. The listing uses a setting that suits my system.

I've no idea how your WinSCP.com works. I believe all you would need to do is to plug it in place of the ECHO process %%i line which obviously just shows the directory to process. It would be shown twice since DAYS is 2.

processme is a simple flag, cleared for each %%i and then matched against 1leafdirname for 1000..1999 so each 3-digit numeric leafname, or against data (and the leafname list can be extended by simply adding new leaves into the list).

If processme becomes set to any value, then processing is invoked. Each process in individually analysed for errors; any error increments winscp_result.

The seterr 7 is simply a library routine I have that sets errorlevel to 7 for testing.

I've removed the set WINSCP_RESULT=%ERRORLEVEL% for reasons I commented on, and have remarked-out the ending to suit my testing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.