Thursday, July 11, 2019

Batch File - How to check if a folder is empty, then upload to FTP, and then move files if error or not using WinSCP

echo on

call :ReportFolderState "C:\FTPS\EXPORT"
Pause
@exit /b

:ReportFolderState
@call :CheckFolder "%~f1"
@set RESULT=%ERRORLEVEL%
@if %RESULT% equ 999 @echo Folder doesn't exist
@if %RESULT% equ 0   (@echo Folder is Empty!)
@if %RESULT% equ 1   goto Notempty
@exit /b

:CheckFolder
@if not exist "%~f1" @exit /b 999
@for %%I in (""%~f1"\*.*") do @exit /b 1
@exit /b 0

:Notempty
@echo Not Empty!
setlocal enableextensions

    for %%a in ("%~f1\*.xml") do (
        set "fileName=%%~na"
set "NamePath=%%~fa"
echo "%%~fa"
        setlocal enabledelayedexpansion
goto Sendfile
)
exit /b

:SendFile
echo %Namepath%
@echo sending File!
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open ftpes://myftpsite.com" ^
    "lcd ""%~f1""" ^
    "cd /" ^
    "put %Namepath%" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  move %Namepath% C:\FTPS\ARCHIVE
  echo Uploaded Successfully
) else (
  move %Namepath% C:\FTPS\ERROR
  echo Error Uploading
)

exit /b %WINSCP_RESULT%

No comments:

Post a Comment