Solved

Delete a set of files

Posted on 2014-09-30
8
93 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
[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
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 50

Expert Comment

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

Open in new window

0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40351935
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 50

Accepted Solution

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

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

Expert Comment

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

Author Comment

by:Rothbard
ID: 40351960
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 50

Expert Comment

by:Vitor Montalvão
ID: 40351968
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
ID: 40358874
Thanks!
0
 
LVL 81

Expert Comment

by:David Johnson, CD, MVP
ID: 40361492
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
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 …

734 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