Jump to content

Recommended Posts

Posted

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

Posted (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 by smithson83
  • Thanks 1
Posted
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.
  • Thanks 1
Posted
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.

Posted (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 by smithson83
Posted

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

  • Thanks 1
Posted

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"

Posted (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 by smithson83
  • Thanks 1
Posted

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

Posted

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.

Posted
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)

  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...