Solved

Delete Folders from a text file

Posted on 2010-11-18
9
859 Views
Last Modified: 2012-05-10
I've got a PS script to delete files and that works fine (see attached)

What i'm now looking to add to it is a Get-Content for it.

I have a Text file that contains a whole list of names, which will be folder names.  How can i get the Delete to only look for those folders and delete them and any sub folder/files that may reside in them?

NB: currently in the script you'll see ive got ti set up to only include TMP files obviously this will not be the case for this as i'll want it to delete all folders/files from with the named folders
$drive = new-object -com wscript.network
$drive.MapNetworkDrive("Y:", "\\<Server name>\E$")
get-childitem y:\ -include *.tmp -recurse -errorAction silentlyContinue| foreach ($_) {remove-item -recurse $_.fullname -whatif}
$drive = New-Object -com WScript.Network
$drive.RemoveNetworkDrive("Y:")

Open in new window

0
Comment
Question by:Colchester_Institute
  • 4
  • 4
9 Comments
 
LVL 1

Expert Comment

by:tuxnani
ID: 34162878
rm command is supposed to have very powerful nature and can, in recursive mode, delete the files and directory as well.
so use
rm -R .
for the purpose
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34162927
thats not really telling me how to get the names from a text file.

I've currently written an old script using the RMDIR /S /Q parameters (see Attached)

but ideally was looking for how to do this using powershell


REM - Map drive to server

net use L: \\<server name>\E$

REM - Delete Profiles

For /f %%N in (name.txt) DO CALL rmdir /S /Q L:\<folder1>\<folder2>\%%N

REM - Delete User Files

For /f %%N in (Name.txt) DO CALL rmdir /S /Q L:\<folder1>\<folder2>\%%N

REM - Remove Mapped Drive

Net use L: /delete

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34163087

Something like this:
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Recurse -Force -WhatIf
  }
}

Open in new window

HTH

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34163677
ok i think that will work and ive added on the mapping to the drive command.

Just two things i've noticed.  How can i get it not to display errors

i.e. its coming up red telling me that so and so folder i dont have permissions well thats ok as thats not the folder i'm trying to delete??? so should it not just skip it with out even telling me?

and then when it got to the folder it was meant to delete it tells me the fully qaulified file name must be less than 260 characters and the directory name must be less than 248...how can i adjust this so it just deletes everything even if 204-260+ in length
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 70

Expert Comment

by:Chris Dent
ID: 34164677

> permissions well thats ok as thats not the folder i'm trying to delete???

It'll be permission to access the folder to read it, we're traversing all folders (-Recurse) looking for specific folders with the name you define.

The error can be suppressed by adding -ErrorAction SilentlyContinue to Get-ChildItem. e.g.
Get-Content YourList.txt -ErrorAction SilentlyContinue | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -WhatIf
  }
}

Open in new window

I've removed the -Recurse flag from Remove-Item. Lets see if it'll just trash the entire tree from there.

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34165172
ohh so close yet so far...

Now it brings up the ask box for me to say yes, yes to all, no, no to all and suspend.

Also the get-childitem is still trying to access other folders not specifed but returning the error the Access to path is denied

which is fine as it shouldnt eve n be attempting to look at those paths?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34165214
Ah, I'm sorry I put the ErrorAction flag in the wrong place.
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse -ErrorAction SilentlyContinue | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -WhatIf
  }
}

Open in new window

Good job it's almost time to go home :)

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
ID: 34186533
ok i think thats solved it barring the fact it prompts me for a response to see if i want to delete.  In otherwords it dont just go ahead and do it
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 34186578

Upgraded again :)

Note that this version really does delete, no more -WhatIf.

Chris
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse -ErrorAction SilentlyContinue | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -Confirm:$False
  }
}

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

17 Experts available now in Live!

Get 1:1 Help Now