Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Delete Folders from a text file

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
Colchester_Institute
Asked:
Colchester_Institute
  • 4
  • 4
1 Solution
 
tuxnaniCommented:
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
 
Colchester_InstituteAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Colchester_InstituteAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

> 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
 
Colchester_InstituteAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:
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
 
Colchester_InstituteAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now