Solved

Delete multiple text files via dos command line

Posted on 2012-03-15
14
598 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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). …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now