Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting multiple files at once with a Text file

Status
Not open for further replies.

Seegee

Technical User
Jun 12, 2001
170
0
0
US
Anyone know what the syntax would be to delete a bunch of files that are listed in a text file?

I've tried del > filename.txt and del < filename.txt

I can generate a directory listing with dir /b /s > filename.txt. I want to take some of the files from that generated list and delete them all at once by using the del command to get the filenames from the filename.txt file

any ideas?
 
if you are creating a batch file

it would be just "del file.txt" or "del c:\junk\file.txt
 
It's not the text file I'm trying to delete. It's the files listed in the text file I want to delete.

Like using the file.txt to provide the data the DEL command will use to delete the files.

Cliff
A+, Net+
 
So if I read your question correctly, you want to have words/lines within the file.txt deleted, correct?

Maybe something like this?
Code:
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)
strContents = objFile.ReadAll
objFile.Close

strContents = Replace(strContents, Chr(34), "")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.WriteLine strContents
objFile.Close
This script replaces all "" marks within a text file. If you wanted to delete, just place delete instead of replace.

Found it at:
 
NO, I want the delete command to take a line like C:\music\song.mp3 from within a text file and use that line to delete a real file called song.mp3 which i located in the C:\music folder and delete the song.mp3 file from within that folder.

so it would be like I actually type Del C:\music\song.mp3 at a command prompt



Cliff
A+, Net+
 
Sorry I didn't mean NO as in I was yelling. I only meant to capitalize the N and have a lowercase o.



Cliff
A+, Net+
 
Seegee,
Sounds like you might get somewhere by combining techniques here. Assume you genererate a text file called "delfiles.bat" which contains filenames as path\name strings:

C:\junk\file1.txt
C:\junk\file2.txt
D:\otherjunk\file3.mp3

etc., ...

Then, load the file "delfiles.bat" into a text editor and use a <ReplaceAll> function to replace "C:" with "del C:" and "D:" with "del D:" and you then have a file with contents:

del C:\junk\file1.txt
del C:\junk\file2.txt
del D:\otherjunk\file3.mp3

THEN run the batch script by typing ...

delfiles.bat

... at the command prompt from within the directory containing that file. I'm just guessing here. No guarantees.

You might also write a script to edit the text file and do the <ReplaceAll> operations automatically.

--torandson
 
torandson,

That is a great idea! I already have a text editor with the replace all feature.

Thank all of you for your input!

Cliff
A+, Net+
 
Being a scripter (.bat's are on the way out), I thought I would provide you with a .vbs to do what you are looking for:

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
	("C:\Scripts\badfiles.txt", ForReading)

Do Until objTextFile.AtEndOfStream
    filestodelete = objTextFile.ReadLine



objTextFile.Close
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFile(filestodelete)
Loop
 
If your text file (lets call it FILES.TXT) has each file listed line by line like this:

file1.txt
file2.txt
file3.txt

then you can use the command below in a batch file which will delete the files in FILES.TXT one by one as required:

for /F %%s in (files.txt) do (del %%s)

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
Do all the files have the same file extension or are they all different types of files?
 
They all have the same file extension .mp3

Cliff
A+, Net+
 
Here's a batch file that may work:

@ECHO OFF
FIND /V /N "blah.mpa" "FILENAME" > %temp%\DelMpg.txt

set /a numb=1

:BEGIN

FOR /F "TOKENS=1,2 DELIMS=]" %%A IN ('FIND "[%NUMB%]" %temp%\DelMpg.txt') DO SET FILE=%%B
ECHO %FILE%
set /a numb=%NUMB%+1
IF %FILE%=="ECHO is on" GOTO :EXIT
DEL /Q %FILE%
PAUSE
GOTO :BEGIN

:EXIT
DEL /Q %temp%\DelMpg.txt
EXIT


FILENAME should be replace by the text file you have. And I put a PAUSE in there just in case it started deleting stuff you didn't want. Feel free to take that out if you just want it to run by itself. Please let me know if this works for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top