Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Delete files with names matching a list

Posted on 2014-09-30
Medium Priority
192 Views
I am using Windows 7. I have a directory with a very large number of xml files. I want to delete all files such as the name is included in a list I have. For the sake of argument, let's say this consists only of the names

file1.xml
file2.xml

I think it should be possible to create a .bat file which does this for me, rather than do it manually, which would take ages? If so, I would be very grateful if someone could provide an example of such a Windows script.
0
Question by:Rothbard
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 15

Expert Comment

ID: 40351676
Here is the code...

You just need to set source folder and the script will erase anything that matches the pattern... FILE*.XML..

@echo off
set source=C:\30092014
for /f "tokens=*" %%a in ('dir /b /a-d "%source%\file*.xml"') do call del %%a

0

Author Comment

ID: 40351703
Thanks, but as I said the names I gave were just an example. The names of the files I need to delete don't match any sort of pattern, unfortunately.

Let's say I have a file called NODELETE.txt, which is just a long list of names of files I do NOT want to delete. I need a script which will delete all files whose names are not included in NODELETE.txt.

0

LVL 15

Accepted Solution

Haris Djulic earned 2000 total points
ID: 40351753
ok, try this.. file list.txt contains list of file to be deleted

@echo off
set source=C:\30092014
for /f "Tokens=* Delims=" %%x in (list.txt) do del %%x

0

Author Comment

ID: 40351815
Thanks, works great
0

LVL 1

Expert Comment

ID: 40351823
Based off of your first question, you have a list of files that you need deleted.  Below is a powershell script that will do just what you need.  I know you asked for a bat file, but having Windows 7 gives you powershell which is by far more robust than command prompt.  The below code has the paths hard coded in but can easily be changed to take the paths as parameters.  Save the code to a file with the .ps1 extension and replace the paths with the proper paths to the text file that has the list of files to be deleted and the directory the files to be deleted are located.  Be sure the text file has one name per line.

$fileNameList = gc "C:\path\to\list\of\names.txt"$directory="C:\path\to\directory"
$filesInDir = gci$directory

foreach ($file in$filesInDir) {
foreach ($name in$fileNameList) {
if (($file.name).Contains($name)) { remove-item \$file.fullname}
}
}


If you have administrator rights to the system you will have to change the execution policy on powershell to allow .ps1 scripts to be ran.  This only has to be done once for the user running .ps1 scripts, run powershell as administrator and copy/paste the below code to it.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted


If you prefer to keep the ExecutionPolicy Restricted which requires the scripts be signed.  Then run the below code in powershell or command prompt.

powershell -executionpolicy bypass -file C:\path\to\script.ps1

0

## Featured Post

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
###### Suggested Courses
Course of the Month11 days, 18 hours left to enroll