3

I am a long time viewer but this is my first question, so I apologize if I do it wrong.

I need to recursively delete all folders and files on a remote machine, and I want to prevent specific folders from getting deleted.

These excluded folders are listed in a text file called excludeDirectories.txt, and there is one folder-name on each line.

My first attempt is a combination of command-prompt commands and the use of psexec. The script is listed below. Note I am running these from a batch command, thus the double %%:

net use r: \\machine\sharedFolderName

FOR /D %%a IN (R:\*.*) DO (
    c:\pstools\psexec cmd /c rmdir /s /q R:\
)

This script deletes everything but I can't figure out how to check the text file for the current folder in the loop, and skip to the next folder in the FOR loop, if the file is found in the text file.

I have spent days on this and have had trouble getting code to work that reads the folders into something I can use in an IF statement and branching out of the psexec line when a match is found.

Again, to beat a dead horse, what I am trying to accomplish is:

  1. Recursively loop through folders on the R: drive
  2. Check each folder in the FOR loop to see if it is in the text file
  3. If the current folder in the FOR loop is found in the text file, skip the line that deletes the folder and advance to the next folder in the FOR loop
  4. I can't use powershell
  5. I have to store the excluded folders in a text file
Synetech
  • 9,643
  • 9
  • 64
  • 96

2 Answers2

1

This assumes that the text file contains just the names, not the drive letter:

for /d %%a in (R:*) do findstr /i /x /c:"%%~nxa" excludeDirectories.txt || rd /s /q %%a
Neil
  • 54,642
  • 8
  • 60
  • 72
  • Thank you sir. It worked like a charm! I never could get the syntax right. –  Nov 30 '12 at 21:12
  • Are you able to update your answer to show how this would work if you wanted to exclude .svn folders and their contents? – Sjwdavies Dec 19 '12 at 17:54
  • @Sjwdavies Unfortunately it doesn't prevent the for loop from descending into excluded folders and removing subfolders therein, but I suppose if the subfolders of the .svn folders have consistent names then you can simply exclide those names too. – Neil Dec 19 '12 at 20:15
  • Just testing this I'm finding the last line on the excludeDirectories.txt is being ignored? ie if the file lists DirA,DirB,DirC the command will skip DirA and DirB but will remove DirC? Why is this? – mapface Apr 26 '22 at 05:17
  • 1
    @mapface The `/x` switch fails to find the last line if it doesn't end in a newline. Some text editors always append a newline to the file, but others, such as Notepad, do not, and you have to add it manually. – Neil Apr 26 '22 at 08:45
0

Sorry, I did not notice you can't use powershell. But this answer could help those who reach this link.

You can do it better using PowerShell. I am using PowerShell 2.0.

Get-ChildItem -Path BASE_PATH_TO_SEARCH -Include FOLDER_TO_SEARCH -Recurse | ? { $_.FullName -inotmatch 'FOLDER_NAME_TO_EXCLUDE'} | Remove-Item -WhatIf -Recurse
  • Remove the -WhatIf switch to delete the folders. Using -WhatIf simulates deletion but doesn’t actually do it.
  • Set BASE_PATH_TO_SEARCH as . (dot) if you are searching from current directory.
  • This command excludes the 'FOLDER_NAME_TO_EXCLUDE' folders.
  • Paths longer than 260 characters throw exceptions.

    Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.


The good thing is that the Remove-Item cmdlet continues with the rest of the paths leaving those which throw errors.

  • WARNING: While using -WhatIf, any errors like permission denied are not shown. These could occur when you actually run the cmdlet with -WhatIf removed.

One issue though is that running 'Get-Help Remove-Item -detailed' informs that '-Recurse' on 'Remove-Item' does not work properly. Well, for me this was alright. :-). No bad behaviour noticed.

I had used this to recursively delete bin folders from my VS solution base directory. That was a great relief. :-)

For details on Get-ChildItem read all the answers here: Excluding multiple folders when using Get-ChildItem

Community
  • 1
  • 1
B Charles H
  • 95
  • 2
  • 9