7UD9 Posted March 2, 2016 Posted March 2, 2016 Hi all I'm struggling more than is necessary for such a simple task... I have some folders folders; %path%\exam1\random_folder1 %path%\exam2\RandD0mFold3r2 %path%\exam3\rand0mfolder-3 I want to remove all files and folders within the exam directories, regardless of what the file or folder is called, I want everything gone and leave empty directories ready for reuse; %pathname%\exam1\ %pathname%\exam2\ %pathname%\exam3\ Anything I'm finding online only removes files not folders, or folders but not empty folders but also removes the root (examX in this case). Others suggest removing the root directory and recreating but this would destroy ACL set on the folders. There has to be an easy solution to this that I'm missing... any clues? Thanks
smithson83 Posted March 2, 2016 Posted March 2, 2016 (edited) I'm sure someone else may be able to better this, but, you could output the list of folders then kill all files and folders, then recreate the list of folders maybe something like (pseudo code); %path% = x\y\z cd /d %path% dir /b /ad> %temp%\listdir.txt for /f %d in (%temp%\listdir.txt) do md %d Edited March 2, 2016 by smithson83 1
clareq Posted March 2, 2016 Posted March 2, 2016 Would a search for *.* on the root %pathname% then selecting the files and folders you want rid of not work? Failing that, how about a dir>txt file then creating a batch file to delete the files and folders you don't want. 1
7UD9 Posted March 2, 2016 Author Posted March 2, 2016 I'm sure someone else may be able to better this, but, you could output the list of folders then kill all files and folders, then recreate the list of folders maybe something like (pseudo code); Removing and recreating folders will break permissions, these are home folders for accounts so need them intact. Would a search for *.* on the root %pathname% then selecting the files and folders you want rid of not work? Failing that, how about a dir>txt file then creating a batch file to delete the files and folders you don't want. I'm trying to automate this, I could search manually but we have hundreds of accounts, as it is a repeated task I want a script to do this for me. The closest I've got is del /s /q "\\%path%\examX\*.*" but that only removes files, subfolders are emptied but they remain in place.
smithson83 Posted March 2, 2016 Posted March 2, 2016 (edited) Removing and recreating folders will break permissions, these are home folders for accounts so need them intact. what about something like (again, pseudo code) %path% = x\y\z cd /d %path% dir /b /ad > %temp%\listdir.txt for /f %d in (%temp%\listdir.txt) do rmdir "%temp%\%d\*.*" /s /q in my head this walks through each dir name and runs "rmdir /s /q" on sub files and folders within... EDIT: just thinking you may need to nest the for loop Edited March 2, 2016 by smithson83
clareq Posted March 2, 2016 Posted March 2, 2016 Robocopy \\%path%\examX \\%path%\export *.* /E /MOVE Del /s /q "\\%path%\examX\export\*.*" To remove the empty folders left in export look at Create output file w/ empty folder - Delete empty directories (Page 1) / Windows CMD Shell / SS64 Forum 1
smithson83 Posted March 2, 2016 Posted March 2, 2016 Now im just clutching at straws, sorry, but maybe... %path% = x\y\z cd /d %path% dir /b /ad > %temp%\listdir.txt for /f %d in (%temp%\listdir.txt) do FOR /D %%p IN ("%temp%\%d\*.*") DO rmdir "%%p" /s /q 1
7UD9 Posted March 3, 2016 Author Posted March 3, 2016 Thanks for sticking with me! I've found something which does exactly what I need, found here. @ECHO del /a /f /q "%path%\exam1\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam1\*"') do @rd /q /s "%path%\exam1\%%i" I need a line of each for every folder I want to clear but using concatenate in excel, this will be easy. Completed batch script will look like; @ECHO del /a /f /q "%path%\exam1\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam1\*"') do @rd /q /s "%path%\exam1\%%i" del /a /f /q "%path%\exam2\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam2\*"') do @rd /q /s "%path%\exam2\%%i" del /a /f /q "%path%\exam3\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam3\*"') do @rd /q /s "%path%\exam3\%%i" del /a /f /q "%path%\exam4\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam4\*"') do @rd /q /s "%path%\exam4\%%i" del /a /f /q "%path%\exam5\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam5\*"') do @rd /q /s "%path%\exam5\%%i"
smithson83 Posted March 3, 2016 Posted March 3, 2016 you could add a variable for the exam number and loop round eg 20 times incrementing each loop
smithson83 Posted March 3, 2016 Posted March 3, 2016 (edited) maybe... SET counter=1 FOR /L %%i IN (1,1,20) DO ( del /a /f /q "%path%\exam%%counter\*" for /f "delims=" %%i in ('dir /ad /b "%path%\exam%%counter\*"') do @rd /q /s "%path%\exam%%counter\%%i" set /a counter=counter+1 ) 20 being the loop number Edited March 3, 2016 by smithson83 1
7UD9 Posted March 3, 2016 Author Posted March 3, 2016 interesting, I'll look at that another time, it would work very well providing I can also set a starting value (e.g. exam246) Thanks again for the help
Knil92 Posted March 3, 2016 Posted March 3, 2016 just had a quick google search and found this; set folder="C:\test" cd /d %folder% for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q) changing the C:\test to the directory you want, should delete all files and folders in that directory when you run it, hope it helps.
smithson83 Posted March 3, 2016 Posted March 3, 2016 interesting, I'll look at that another time, it would work very well providing I can also set a starting value (e.g. exam246) Thanks again for the help the starting value would be whatever you set counter as on the first line before you get into the loop, then change the 20 to how many times you want to run though eg set counter=246 and then if you wanted to run to exam378, 378-264 is 132 so youd change the (1, 1, 20) to (1, 1, 132) 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now