Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Hi guys ive written a script for a school im 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. The script works almost perfectly my problem is that if there are any errors, eg; 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
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 read from the command line 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.

share|improve this question
This may help : stackoverflow.com/questions/1420965/… –  BrianAdkins Aug 7 '14 at 1:36
@BrianAdkins you should post that as an answer. That's exactly what I was going to answer but you got to it first. This will do exactly what the OP's looking for. –  Twisty Aug 7 '14 at 2:31

2 Answers 2

up vote 1 down vote accepted

From: http://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
share|improve this answer

It doesn't look like you've written anything in the code to tell the computer to write a long file. Is that the script in it's entirety or just the piece you're working on? This isn't my core area but I would expect to see an "if" or "ElseIf" statement to tell the computer what to do. I'm making the language up but here's the general idea.

IF File cannot be deleted (Return Code X) THEN Output Return Code + Filename to Logfile.txt

share|improve this answer
This is the code in its entirety. I would have thought that any output whether it be an error or a successful deletion after the command has been run would be logged. I am unsure as to how I would write an "if" or "elseif" statement to log the error. The problem is students like to put folders within folders within folders etc; so cmd can not delete any files that are deep enough within the file structure (im pretty sure its anything past 256 characters in total eg. \\student\file1\file2\file3\file4(more folders until past 256 characters in total path name)\script.bat) –  Super Geoff Aug 7 '14 at 2:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.