0

I have this batch file which is supposed to find and delete specific files if found, but it keeps returning and displaying errors when the files are not found. I had deleted the /S switch from the DELETE command to make the found files silent. The code is below:

For /F "delims=" %%A in (
  'Dir /B/S "*,*" ^| findstr ",[0-9][0-9]*$" '
) Do Del "%%A"
DEL /Q *.bak
ECHO bak files deleted
DEL /Q *.prjpcb.zip
ECHO Project History Zips deleted
DEL /Q *.pcbdoc.zip
ECHO PCB History Zips deleted
DEL /Q *.SchDoc.zip
ECHO Schematic History Zips deleted
DEL /Q *.PcbLib.zip
ECHO PCB Library History Zips deleted
DEL /Q *.SchLib.zip
ECHO Schematic Library History Zips deleted
DEL /Q *.OutJob.zip
ECHO Output Jobs History Zips deleted
ECHO on
TIMEOUT 5 

How do I get the file to display only the echo when it deletes the files and nothing if nothing is found?

Lonewolf
  • 23
  • 3
  • 1
    Type `del /?` into a command prompt window, so you'll find out that the `/S` option has nothing to do with a kind of silent mode. Anyway, you could use `if exist *.bak del /Q *.bak` to avoid error messages if the files do not exist, or you could also use `del /Q *.bak 2> nul` to suppress error messages... – aschipfl May 14 '20 at 17:58
  • 1
    What is really silly is the fact that the `del` command *clears* the `ErrorLevel` even in case of errors, so when files could not be found or not be accessed, for instance; perhaps you want to take a look at [this post](https://stackoverflow.com/a/33403497) or [this one](https://stackoverflow.com/a/31077745)... – aschipfl May 14 '20 at 18:10
  • I'd also offer the option to check existence before invoking a delete instruction, then check again before outputting a message. e.g. `If Exist "glob" (Del /A /F /Q "glob" 1>NUL & If Not Exist "glob" Echo All glob files deleted.)`. – Compo May 15 '20 at 01:47
  • OK. I have restored the /S switch. But I now get the files found and deleted or files not found! All I require is 'Files deleted' if found or nothing if nothing found. – Lonewolf May 17 '20 at 10:43
  • How does the 'Exist "glob" ' work? Would I need to replace 'glob' with the names of the various files to find and delete? Do I need the speech quotes too? – Lonewolf May 25 '20 at 09:03

1 Answers1

0

You can redirect the output to nul, if you really just never want to see any information from a command:

del /q *.bak >nul 2>&1

>nul redirects standard output, and 2>&1 redirects error output to the same place as standard output. Note that >nul can be written as 1>nul because it's the "first" output stream (standard).

Further note that nul represents a special case for the 2 output streams, such that you can do this:

del /q *.bak >nul 2>nul

https://ss64.com/nt/syntax-redirection.html

avery_larry
  • 2,069
  • 1
  • 5
  • 17