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:
- Recursively loop through folders on the
R:
drive - Check each folder in the
FOR
loop to see if it is in the text file - 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 theFOR
loop - I can't use powershell
- I have to store the excluded folders in a text file