How to silently delete files with a bat file

45,072

Solution 1

Turn echo off to suppress showing the command being run, and redirect output to null as @Sico suggested.

@echo off
del /s *.jpg  >nul 2>&1

You should see nothing displayed when the bat file is run.

Solution 2

In jammykam's answer, he uses >nul 2>&1. What this does is redirect both standard output and standard error to the null device. However, hiding the standard error is not best practise and should only be done if necessary.

@echo off
del /s *.jpg 1>nul

In this example, 1>nul only hides the standard output, but standard error will still show. If del fails to delete some files, you will be informed.

Read more about redirection

Share:
45,072
Deb
Author by

Deb

A Full Stack Developer with 6+ years of experience in a reputed organization with proficiency in some of the latest technologies like Java-8, Spring Boot. Capable of working in challenging environments. Having a consistent and sound educational background. A quick learner and believer in continuous learning. Passionate about coding and programming. Focused and dedicated to deliver quality works.

Updated on July 09, 2022

Comments

  • Deb
    Deb almost 2 years
    del /s .jpg
    

    deletes all .jpgs .. but the problem is: it shows, in cmd when executed =>
    C:\blabla..\this.jpg is deleted..

    I want to turn this off. Such that user will not know what is happening (i.e, what files are being deleted).

  • user3020494
    user3020494 over 10 years
    Why was I down voted? same answer is given by another person later than me but is upvoted :)
  • Deb
    Deb over 10 years
    Well, I did not down voted you. But @echo off really do not do anything in this case. and Thanks for first reply. :)
  • GolezTrol
    GolezTrol over 10 years
    The other answer is also about redirecting output to null. @echo off does hide the call, but not the output.
  • Deb
    Deb over 10 years
    Yes, >nul is the answer of my question, I already used @echo off in my program. But that does not hide the output. (However I should have mentioned that.) It only hides the command.