Solved

Delete multiple text files via dos command line

Posted on 2012-03-15
14
604 Views
Last Modified: 2012-06-22
Experts,

I need to remove multiple text files via the command line.
In example:
C:\1.txt
C:\2.txt
C:\3.txt
C:\4.txt
C:\5.txt

------------

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
0
Comment
Question by:RandallVillalobos
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37727897
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
0
 
LVL 3

Expert Comment

by:telczj9
ID: 37727905
Randal,

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.

Cheers!
0
 
LVL 3

Assisted Solution

by:telczj9
telczj9 earned 250 total points
ID: 37727964
leew,

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 ( 
    ECHO LOOKING FOR FILES ON HOST: %%a
    IF EXIST \\%%a\c$\%%c (
      ECHO ---FOUND "\\%%a\c$\%%c", will delete it...
      DEL /Q \\%%a\c$\%%c
    )
  )
)

Open in new window


cheers!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 44

Expert Comment

by:Darr247
ID: 37727967
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?
0
 
LVL 3

Expert Comment

by:telczj9
ID: 37727984
try "net share" from windows 7 down see what you get :)
0
 
LVL 44

Expert Comment

by:Darr247
ID: 37728010
Go to another machine and try deleting a file from that 'default share'.
0
 
LVL 3

Expert Comment

by:telczj9
ID: 37728029
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.

C:\>

;)
0
 

Author Comment

by:RandallVillalobos
ID: 37729071
Awesome knowledge! thanks for sharing.
Let me test it out!
0
 
LVL 14

Expert Comment

by:Don Thomson
ID: 37729113
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
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37731050
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.
0
 

Author Comment

by:RandallVillalobos
ID: 37731511
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.

thanks!
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
ID: 37731603
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
and
for /f "tokens=*" %%a in (textfile.txt) do del /q /s \\%%a\c$\*.txt
are far less risky.
0
 

Author Closing Comment

by:RandallVillalobos
ID: 37738555
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!
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37738651
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 ss64.com - reference the cmd section.

As basic tutorials, I recommend www.robvanderwoude.com
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question