Batch file to generate batch files to check for missing files on an SFTP server.
Needed a simple solution to repeatedly check an external SFTP server for a missing file to become available. This is a regularly occuring problem for me with different files/servers each time. This batch file asks for server name, username, password, etc, and then writes a customized “WaitFor_%filename%.bat” file to run, along with a helper file “WaitFor_%filename%_FTPCommands.txt” to store the FTP commands. The FTP commands are ran through psftp.exe. When the file is found and downloaded, a big red command-prompt popup will be made to get your attention.
- Download and install PuTTY https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- Ensure psftp.exe from the PuTTY install is in your PATH
- Run the batch file, answer the questions
- Run the generated batch file customized to your answers to begin waiting. Be responsible with your passwords!
REM GenerateSFTPChecker.bat REM Kristofer Christakos REM Batch file to generate batch files to check for missing files on an SFTP server. REM https://kriscoder.com/, https://github.com/kriscode1/ REM License: Modify as desired. @echo off set /P servername="Enter the SFTP server name: " set /P username="Enter the username: " set /P password="Enter the password: " set /P directory="Enter the directory of the file to wait for (or leave blank for default directory): " set /P filename="Enter the filename to wait for: " set /P seconds="Enter how often you want to check (in number of seconds): " set batchname="WaitFor_%filename%.bat" set helpbatchname="WaitFor_%filename%_FTPCommands.txt" IF EXIST %batchname% del %batchname% IF EXIST %batchname% echo Error clearing %batchname%&pause ( echo @echo off echo :loop echo psftp %username%@%servername% -pw %password% -b %helpbatchname% echo IF NOT EXIST %filename% GOTO :sleep echo start "Available File!" cmd /c "color C0&date /T&time /T&echo File %filename% is downloaded.&pause" echo exit echo :sleep echo timeout /t %seconds% echo goto :loop)>>%batchname% IF EXIST %helpbatchname% del %helpbatchname% IF EXIST %helpbatchname% echo Error clearing %helpbatchname%&pause IF DEFINED directory echo cd %directory%>>%batchname%&timeout /t 1 /nobreak>nul echo get %filename%>>%helpbatchname% echo Done. pause