Solved

Delete files with names matching a list

Posted on 2014-09-30
5
184 Views
Last Modified: 2014-09-30
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
Comment
Question by:Rothbard
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
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

Open in new window

0
 

Author Comment

by:Rothbard
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.

Thanks in advance.
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 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

Open in new window

0
 

Author Comment

by:Rothbard
ID: 40351815
Thanks, works great
0
 
LVL 1

Expert Comment

by:ExpertNotReally
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}
  }
}

Open in new window


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

Open in new window


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

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parental control on huwei HG658b 1 26
powershell script 9 63
Script to install exe on remote PCs in a domain 2 81
Turn off power management for the wireless nic card 3 27
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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