Delete multiple text files via dos command line


I need to remove multiple text files via the command line.
In example:


I tried the following batch file with no success, deletes the first line and then the batch files just "HANGS".

@echo off
del /q /s \\computername1\c$\1.txt
del /q /s \\computername1\c$\2.txt
etc etc etc
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
why not:

del /q /s \\computername1\c$\*.txt

Put the computer names in a text file.  Then use this:

for /f "tokens=*" %%a in (textfile.txt) do del /q /s \\%%a\c$\*.txt

careful that:

del /q /s \\computername1\c$\1.txt

does not hang.  The "/s" flag is looking for instances of "1.txt" in all subdirectories and if it finds them it will delete them from all subdirectories.

del /q \\computername1\c$\1.txt

would only delete "1.txt" in the c:\ drive of computername1.


if Randall were to run the statement you suggested he would wipe out ALL the files on the in the C: drive with the extention *.txt

Randal I would suggest:
FOR /F %%a in (ComputersR.txt) do (
  FOR %%c IN (1.TXT 2.TXT 3.TXT 4.TXT 5.TXT) DO ( 
    IF EXIST \\%%a\c$\%%c (
      ECHO ---FOUND "\\%%a\c$\%%c", will delete it...
      DEL /Q \\%%a\c$\%%c

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Usually the root of a drive is not shared, ergo you would not have permission to delete C:\*.txt

What version[s] of windows are you using?
try "net share" from windows 7 down see what you get :)
Go to another machine and try deleting a file from that 'default share'.
would mind qualifying "Usually the root of a drive is not shared"...

C:\>type \\<host_name>\c$\test.txt
7:49 PM 9/22/2011
C:\>del /q \\<host_name>\c$\test.txt

C:\>type \\<host_name>\c$\test.txt
The system cannot find the file specified.


RandallVillalobosAuthor Commented:
Awesome knowledge! thanks for sharing.
Let me test it out!
Don ThomsonCommented:
Darr247 is correct

Usually the root of the Sys drive is not shared - in the normal sense

If you are running XP PRo, Vista PRo or Win7 Pro and are not using simple sharing then there is only an Administrative share on the actual root of the hard drives.

It is just a bad idea from a security point of view to do a visible share on the root - The c$ is not visible when looking at the network (For that matter anything that has a $  after the share name is a hidden share. Most IT departments will only put normal visible shares  on folders that are available to all users. Things like accounting folders should be shared as accounting$ and then if a drive needs to be mapped for someone that needs access to that folder - is is either done manually or via the login profile on a DC

From a security point of view - no one but the administrator should have access to the Root of any users PC - In the case that  Randall is asking about - the files that need to be deleted should be put in a folder off the root - not in the root itself. There are many reasons for this - especially on some of the older OS. I remember working on systems that were extremely slow that we found thousands of files in the root directory - What we found was any more than 256 ITEMS (Files or Folders) in the Root directory caused the PC to grind to a halt.  Any other folder was good for more items but not the root - keep it clean
Lee W, MVPTechnology and Business Process AdvisorCommented:
Your right, I didn't check the switches carefully on the del command.  The problem is, the question is too vague from my perspective and I should have requested clarification.

What can really help a solution is understanding WHY the user needs to do these things and what application is generating the files.  Why would you be deleting every file named 1.txt, 2.txt, etc on a drive?  I'm pretty sure those aren't actual file names, but if we knew more, a better solution might present itself.
RandallVillalobosAuthor Commented:
Hi Everybody thanks for all the assistance.  This is the entire story:
The IT department received an incorrect package file for deployment. This file creates a folder "XYZ" and outside on the "ABC" folder it  includes .exe, text files and .doc  files.

The path is
C:\Program Files\ABC\XYZ
Inside the ABC folder, we find many .exe and .txt files that we don't need.

So I want to delete the entire"XYZ" folder, and many of the .exe, .txt and .doc folder on the "ABC" directory.

In example
Path : C:\Program Files\ABC
delete "test.txt"
delete "file.doc"
delete "program.exe"

Path : C:\Program Files\ABC\XYZ  (delete the entire "XYZ" folder and everything inside of it.

I hope this clarifies what I'm trying to accomplish on a whole bunch of computers.

Lee W, MVPTechnology and Business Process AdvisorCommented:
Why not just delete the XYZ folder?

Also now that we know things aren't in the root of the drive, commands like
del /q /s \\computername1\c$\*.txt
for /f "tokens=*" %%a in (textfile.txt) do del /q /s \\%%a\c$\*.txt
are far less risky.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RandallVillalobosAuthor Commented:
Thanks, I believe that scripting is something I will enjoy.
If you have any recommended websites links that provide an explanation to all this parameters it will be great!
Lee W, MVPTechnology and Business Process AdvisorCommented:
Scripting (in batch) is just knowing what the commands do and what information is likely to be returned and a series of delete commands IS a script... but where things are repetitive, loops should be looked at.

As for sites, for reference, i prefer - reference the cmd section.

As basic tutorials, I recommend
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.