Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete Folders from a text file

Posted on 2010-11-18
9
Medium Priority
?
866 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
[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
  • 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 71

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

 
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 71

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 71

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 71

Accepted Solution

by:
Chris Dent earned 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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