2

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.

2

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
1

Alternative delete with output log:

robocopy [empty folder] [path of file] [file to delete or wildcards] /purge

To test use /l:

robocopy [empty folder] [path of file] [file to delete or wildcards] /purge /l

Real sample:

C:\TEMP>dir /b

FILE_to_DELETE.txt

C:\TEMP>robocopy C:\EMPTY %cd% FILE_to_DELETE.txt /purge /l
-------------------------------------------------------------------------------    ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------
Started: quinta-feira, 10 de dezembro de 2015 13:42:34
Source: C:\EMPTY\
Dest: C:\TEMP\
Files: FILE_to_DELETE.txt
Options: /L /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30
------------------------------------------------------------------------------
           0    C:\EMPTY\
          *EXTRA File                  0        FILE_to_DELETE.txt

------------------------------------------------------------------------------
        Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         1         0         0         0         0         0     Files :         0         0         0         0         0      
    1    Bytes :         0         0         0         0         0      
    0    Times :   0:00:00   0:00:00                       0:00:00  
    0:00:00    Ended : quinta-feira, 10 de dezembro de 2015 13:42:34


C:\TEMP>robocopy C:\EMPTY %cd% FILE_to_DELETE.txt /purge

-------------------------------------------------------------------------------
ROBOCOPY     ::     Robust File Copy for Windows   
-------------------------------------------------------------------------------

      Started : quinta-feira, 10 de dezembro de 2015 13:42:41    Source
    : C:\EMPTY\
         Dest : C:\TEMP\

        Files : FILE_to_DELETE.txt

      Options : /DCOPY:DA /COPY:DAT /PURGE /R:1000000 /W:30
-----------------------------------------------------------------------------
              0    C:\EMPTY\
              *EXTRA File                  0        FILE_to_DELETE.txt
------------------------------------------------------------------------------
       Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         1         0         0         0         0         0    Files :         0         0         0         0         0      
    1    Bytes :         0         0         0         0         0      
    0    Times :   0:00:00   0:00:00                       0:00:00  
    0:00:00    Ended : quinta-feira, 10 de dezembro de 2015 13:42:41

C:\TEMP>dir /b

C:\TEMP>
1

It doesn't look like you've written anything in the code to tell the computer to write a log file. Is that the script in its 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
  • 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
0

From your bat file you'll get a file "DeletedFiles.txt" which will have the list of all deleted files. If you want the all the text generated while executing a bat file, you can use the following command: $mybatfile.bat > mylogfile.txt 2>&1 where mybatfile.bat is the bat file which you want to execute, mylogfile.txt is the name of file in which output generated in the command prompt (which will have all the errors) is stored which executing the bat file.

You can also use command $dir /s /b *.c to output all *.c files in the directories and sub directories recursively with out actually deleting them. To log it you can use $dir /s /b *.c >listofcfiles.txt

  • (1) Your first paragraph is little more than a rehash of the accepted answer.  P.S. What does $ have to do with Windows CMD?  (2) Why are you talking about *.c file?  There's nothing in the question about *.c files. – G-Man Dec 11 '15 at 8:26

protected by JakeGould Dec 12 '15 at 5:23

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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