Solved

Delete a set of files

Posted on 2014-09-30
8
80 Views
Last Modified: 2014-10-04
I just posted a similar question. I need a script which deletes all files EXCEPT those whose names are included in a text file (e.g. nodelete.txt). I would be very grateful if someone could help me with this.
0
Comment
Question by:Rothbard
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Powershell solution:
 $files2Exclude=Get-Content .\nodelete.txt : Remove-Item * -Exclude $files2Exclude

Open in new window

0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
not as elegant
---
@echo off
set source=C:\30092014
for /f "Tokens=* Delims=" %%x in (list.txt) do copy %%x %temp%
del %source%\* /q
for /f "Tokens=* Delims=" %%x in (list.txt) do copy %temp%\%%x %temp% %source%
---
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
Sorry, wrong commands separator. Here it is:
 $files2Exclude=Get-Content .\nodelete.txt; Remove-Item * -Exclude $files2Exclude

Open in new window

0
 
LVL 15

Expert Comment

by:Haris Djulic
Comment Utility
Better late then never ;)

@echo off
set source=C:\30092014

for /f "tokens=*" %%G in ('dir /b /a-d "%source%\*"') do (call :del_proc "%%G")
GOTO :eof
:del_proc
findstr -sip %1 list.txt > NULL
IF  ERRORLEVEL 1  del %1

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Rothbard
Comment Utility
Thanks but it doesn't seem to work. I get the following error message:

Get-Content : A positional parameter cannot be found that accepts argument ':'.
At line:1 char:27
+ $files2Exclude=Get-Content <<<<  .\nodelete.txt : Remove-Item * -Exclude $files2Exclude
    + CategoryInfo          : InvalidArgument: (:) [Get-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
You need to give full path to the file or run it in the folder where's that file.
0
 

Author Closing Comment

by:Rothbard
Comment Utility
Thanks!
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
The easier you make the code to read the better off you are down the road when you look at the code a year or so from now.  Comments are always good
 $files2Exclude=Get-Content .\nodelete.txt
 Remove-Item * -Exclude $files2Exclude

Open in new window

$files2exclude = Read-Host "Please Enter the Path and File name of the Files to Exclude List"
if ((test-path $filestoexclude) -eq $false ) 
    {
    $output = $files2exclude + "Does Not Exist!"
        Write-output $output
     }
else {
    $filepath = read-host "Please Enter the Path of the files to delete"
    if ((test-path $filepath) -eq $false ) {
         $output = $filespath + "Does Not Exist"
         Write-output $output
         }
    else {
     #
     #assumes that you are on the same drive
     #
     cd $filepath
     Remove-Item * -Exclude $files2exclude
     }
}       

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 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

14 Experts available now in Live!

Get 1:1 Help Now