How do I perform an action that depends on a file’s arrival?

John Savill

January 8, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Users on hosts often transfer a file over an FTP link from another host and need to perform an action on the file when the file arrives. The following batch file, which requires the Microsoft Windows NT Resource Kit, lets you check for a file and run the file when it arrives.

:filecheck
if exist e:uploadfile.txt goto actionfile
sleep 100
goto filecheck
:actionfile
...

This batch file checks for the file file.txt every 100 seconds. You might run into problems if the file is large and is still under construction when the batch file looks for the file (e.g., if the file is transferring over an FTP link and is still writing). To solve this problem, rename the file to itself, as the following command shows.

RENAME e:uploadfile.txt file.txt
if not errorlevel 0 goto actionfile

The rename command generates an error message if the file doesn't exist or isn’t available to write to (e.g., because it is still being written to). The errorlevel is the same, but the error message changes, in case you want to distinguish between the two in the .bat file.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like