Solved

Delete Folders from a text file

Posted on 2010-11-18
9
863 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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