Windows – Delete specified files and log all output in CMD

batch filewindows

I've written a script for a school I'm working at to automatically mount a folder as drive X: and then to go into drive X and recursively scan all folders and sub folders for .bat, .cmd, .exe and .vbs files and delete them. The script works almost perfectly. My problem is that if there are any errors (e.g., a drive path is too long), it will not log it to a text file. Is there any way this can be done with a batch file?

@echo off
net use X: \\NETWORK PATH HERE
X:
cls
Echo Deleting bat files please wait...
del /s *.bat > DeletedFiles.txt
Echo Deleting CMD files please wait...
del /s *.cmd >> DeletedFiles.txt
Echo Deleting VBS files please wait...
del /s *.vbs >> DeletedFiles.txt
Echo Deleting Executable files please wait...
del /s *.exe >> DeletedFiles.txt
Echo Process Completed
set /p=Press Any Key To Close

Currently any errors I have to (manually) read from the CMD window and sort out. It would be handy to have all errors and deleted files saved to a text file for record keeping purposes.

Also PowerShell is out of the question; despite being a system admin, the education department will not give me rights to run PowerShell or VBS scripts.

Best Answer

From: https://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file

You can redirect both the standard output (stdout) as well as the standard error (stderr) to the same log file.

Such as

dir > a.txt 2>&1
Related Question