Solved

Delete a set of files

Posted on 2014-09-30
8
94 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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