Scripts Thread, Script to Delete Files and folders within a folder in Coding and Web Development; Hi Guys,
i need to automate the deletion of folders and files within 90 Home Directories. as the file and ...
23rd March 2011, 01:04 PM #1
Script to Delete Files and folders within a folder
i need to automate the deletion of folders and files within 90 Home Directories. as the file and folder names can be anything this script has to work on the assumption that files and folder names are not known.
23rd March 2011, 01:09 PM #2
Dunno if this is will help - it's a script I used to run at the end of term. It will delete everything in the folder apart from a folder called 'keep me'
What we used to tell the kids is if you want stuff saved then move it into a folder called 'keep me' as during the holidays this will run in each of their home drives and anything stored thats not in the 'keep me' folder will be deleted.
You can edit it to your own requirements.
It's done in AutoIT
$search = FileFindFirstFile("*.*")
; Check if the search was successful
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
$file = FileFindNextFile($search)
If @error <> 0 Then ExitLoop
If $file = "keep me" Then ContinueLoop
If $file = "." Or $file = ".." Then ContinueLoop
If StringInStr(FileGetAttrib(@SCRIPTDIR & "\" & $file) , "D") Then
FileDelete(@SCRIPTDIR & "\" & $file)
23rd March 2011, 02:08 PM #3
Thanks for that will try this. will this run as a normal .vbs because i will have to go through Change Advisory Board to get that 3rd party software approved.
23rd March 2011, 02:32 PM #4
mattx has suggested an AutoIT script so different to vbs but still a scripting language
23rd March 2011, 02:39 PM #5
5th April 2011, 12:53 PM #6
would anyone know how to achieve this using VB or Batch Scripts?
5th April 2011, 12:54 PM #7
What exactly do you want it to do? Just delete "everything" under a path? or some things not?
Are the 90 directories expected to be deleted in 1 script? or multiple runs? :P
Last edited by Steve21; 5th April 2011 at 12:58 PM.
5th April 2011, 12:57 PM #8
yeah so would like it to delete all folders, subfolders and files but as i mentioned above not sure of the naming of these so it just need work on the assumtion that the names arent known. and obviously it must not delete the Users Home Directory just the folders, sub folders and files
Hope that makes any sense,
5th April 2011, 01:03 PM #9
rd /s is the command you want
you could feed this a list of dir names
5th April 2011, 01:04 PM #10
So will you run this from their home area? or you wanting one that just runs over entire server? Bearing in mind if it has no way of telling what a users home directory is.
If you're going to run it from within their home directory, and just delete everything below it, it's simple script. But if you're wanting it run from "root" as such, you'll need to give some way of knowing how the structure is, else it'll delete everything.
5th April 2011, 01:09 PM #11
Thanks for that "somabc" i will have a look at this
it will be running from the server. i know the names of the users home folder. just not sure of the naming of the folders and files within them
5th April 2011, 01:11 PM #12
Originally Posted by somabc
this removes the parent drectory also which i need to keep as its the users home folder.
5th April 2011, 01:27 PM #13
The code below is written in vbscript. The lines in between the rows of asterisks are examples - repalce them with the folders you want to clean. It will delete everything in (eg) d:\home\user1 but leave d:\home\user1 alone
if ofso.folderexists(x) then
for each oSubFolder in oFolder.subfolders
for each oFile in oFolder.files
Thanks to srochford from:
doddsworthy (5th April 2011)
5th April 2011, 01:30 PM #14
Seems I got pipped to the post :P
Could also try it like:
Think that should work, takes directories from the testing.txt file (you need to put them in etc etc)
for /F "delims=" %%a in (C:\Testing.txt) do (
for /f "tokens=*" %b in ('dir /b *.*') do RD /S /Q %b
for /f "tokens=*" %c in ('dir /b *.*') do del %c
Removed all directories, and sub folders/files,
then removes any remaining files.
Not tried it for obvious reasons :P So not 100% it works, but might want to test it on a quiet area (Remember it needs direct paths, CMD doesnt support UNC or doesnt seem to)
Thanks to Steve21 from:
doddsworthy (5th April 2011)
5th April 2011, 01:47 PM #15
If you want this just as a batch script then you could use
This will first delete all files in all sub directories and then it deletes all sub folders too (I couldn't find a command to do both at the same time)
for /f "delims=" %%j in ( ' dir /b /ad' ) do del /f /s /q %%j
for /f "delims=" %%j in ( ' dir /b /ad' ) do for /f "delims=" %%i in ( ' dir %%j /b /ad' ) do rd /s /q "%%j\%%i"
so if you had for example a folder structure:-
If you ran the batch file from inside each of the y2010, y2009 and y2008 folders then it would go into each sub folder (e.g. pupil1, 2, etc...) and empty both files and folders while leaving the pupil1, pupil2 folders in place.
Obviously you need to be very careful with a batch file like this... running it in the wrong place could be bad
2 Thanks to AlexB:
doddsworthy (5th April 2011), Zoom7000 (17th June 2011)
Last Post: 1st July 2014, 08:34 AM
By Pumaedition in forum Scripts
Last Post: 22nd April 2008, 10:08 PM
By JPhillips173 in forum Scripts
Last Post: 17th April 2008, 11:25 AM
By projector1 in forum Scripts
Last Post: 9th March 2008, 12:15 AM
By OutToLunch in forum Scripts
Last Post: 25th September 2006, 12:27 PM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread