SFTP File Check



Imagine, that you have a set of files on SFTP server, possibly with a general mask, like a date in their names (for easier identification) and you need to check if all of them are present without having access to actual server besides the SFTP connection.

If there are two or three files - easy. But what if there are hundreds of them and the count may vary? This script will help you.

You put a list of files into a filelist.txt in format like ".extension Mandatory" (instead of .extension another portion of mask may be used; Mandatory - is an optional description) and run the script. It will ask for username and password (but better use a key authentication, if available), get the list of files and then parse that output letting you know if any of the files are missing.

Sample uses SSH Tectia Client, but it should be possible to replace it with any other CLI-capable client. My own Global Functions is also used here.


@echo off

cls

rem *******************************

rem * Settings: General *

rem *******************************

set SCRIPTNAME=Citi9 Files Check

set globalfuncpath=\\path\globalfunctions.cmd

setlocal enabledelayedexpansion

call %globalfuncpath% getdate

SET yy=%yy:~2,2%

call %globalfuncpath% datemath %yy% %mm% %dd% - 1

set dt=%_yy_int%%_mm_str%%_dd_str%

rem *******************************

rem * End of Settings: General *

rem *******************************


rem **************************************************************************************************************

rem Settings: Paths

rem **************************************************************************************************************

rem ***Apps***

CALL %globalfuncpath% tectiacheck

set logapp=\\path\Tee.vbs

set sername=server_name

set filelist=\\path\filelist.txt

rem ***Logs directory***

set batchfile=\\path_logs\batch.txt

set lsfile=\\path_logs\ls.txt

SET LOGSDIR=\\path_logs\Logs

rem **************************************************************************************************************

rem End of Settings: Paths

rem **************************************************************************************************************


Echo Script is starting with %dt% as previous business date. Is the date correct?

call %globalfuncpath% choice "Press 1 for Yes, 2 to change date and Q to Quit:" 1 2

if %chosen% equ 1 (

GOTO mainscript

)

If %chosen% equ 2 (

set /p dt=Please, enter correct date in YYYYMMDD format:

goto mainscript

)




:mainscript

set /p usname="Input your ID:"

set /p pass="Input %usname%'s password:"

cls

If exist %batchfile% (

del /Q /F %batchfile%

)

set /a bostefiles=0

Echo Started at %date% %time% >>%LOGSDIR%\%dt%.txt

CALL %globalfuncpath% makemisdir %LOGSDIR%

CALL %globalfuncpath% makemisdir %LOGSDIR%\TempTectia


Echo Writing ls batch file...

Echo cd /cbr/upload_data >%batchfile%

Echo ls -z %dt%*>>%batchfile%


If exist %batchfile% (

sftpg3.exe -B %batchfile% --password=%pass% %usname%@%sername% > %lsfile%

del /Q /F %batchfile%

)


Echo Checking files...

Echo.


FOR /F "tokens=1,2" %%A IN (%filelist%) DO (

Call :filecheck %%A %%B

)


del /Q /F %lsfile%

If exist %batchfile% (

del /Q /F %batchfile%

)

Echo Checking completed. Press any key to exit...

pause > nul

exit


:filecheck

FINDSTR /I %dt%%1 %lsfile% > nul

If %errorlevel% neq 0 (

Echo %2 file %dt%%1 is missing

)

exit /b

[Snippets]