I decomissioned my DNS-323 NAS which was setup to automatically copy scanned pdf’s off my Brother MFC-440CN Scanner. This meant I needed to re-write my scanning copy shell script to run as a batch script on my main computer.
Re-writing the script was harder than I anticipated. The command line ftp client on windows expects you to know the names of each file. It does however have the ability to run a script file full of ftp commands. So the solution to getting the script working was:
- Run a temporary script for ftp to get a file listing
- Run another temp script for ftp that fetchs and deletes each file in the file listing.
- Cleanup/delete temporary scripts/files
What it does:
Moves files from the specified ftp directory to the specified local directory prepended with the date and time.
Usage
Create a .bat file with the script below. Then set the parameters at the beginnng of the script as follows:
- set server= the ip address of your ftp server (in my case my scanner)
- set fetch_directory= the directory on the ftp server to copy/move files from
- set target_dir = the directory on the local computer to copy/move the files to
The Script
SETLOCAL ENABLEDELAYEDEXPANSION @echo off :: downloads then deletes ftp files from the given location set server=192.168.1.6 :: no trailing / on fetch dir path set fetch_directory=/BROTHER :: no trailing \ on target dir path set target_dir=c:\Users\myusername\Docs\Scan set temp_script_file=ftp_script.txt set temp_dir_list=ftp_file_list.txt :: create temp file with ftp script to output file list echo open %server% > %temp_script_file% echo. >> %temp_script_file% echo mdir %fetch_directory% %temp_dir_list% >> %temp_script_file% echo y >> %temp_script_file% echo bye >> %temp_script_file% :: now run the script with ftp ftp -s:%temp_script_file% :: now we have a list of the files in the ftp directory :: now make a script from the file listing to copy each file. echo open %server% > %temp_script_file% echo. >> %temp_script_file% echo lcd %target_dir% >> %temp_script_file% :: copy each file to a dated file locally for /F "tokens=9 skip=3" %%i in (%temp_dir_list%) do ( SET get_file=%%i echo binary >> %temp_script_file% echo get %fetch_directory%/!get_file:~0,-1! "%target_dir%\%date:~10,4%-%date:~7,2%-%date:~4,2% %time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2% !get_file:~0,-1!" >> %temp_script_file% echo delete %fetch_directory%/!get_file:~0,-1! >> %temp_script_file% ) echo bye >> %temp_script_file% :: now run the new script ftp -s:%temp_script_file% ::remove temporary script files del %temp_dir_list% del %temp_script_file%