?
Solved

Delete a set of files

Posted on 2014-09-30
8
Medium Priority
?
95 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 51

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 82

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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 51

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 82

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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.
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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.
Suggested Courses

770 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