Jump to content

Recommended Posts

Posted

Hi Guys,

 

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.

 

Cheers

 

Ryan

Posted

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")
   Exit
EndIf

While 1
   $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
       DirRemove($file, 1)
   Else
       FileDelete(@SCRIPTDIR & "\" & $file)
   EndIf
WEnd
FileClose($search)

Posted

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.

 

Cheers

 

Ryan

  • 2 weeks later...
Posted (edited)

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

 

Steve

Edited by Steve21
Posted

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,

 

Ry

Posted

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.

 

Steve

Posted

Thanks for that "somabc" i will have a look at this

 

Steve,

 

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

Posted
rd /s is the command you want

 

you could feed this a list of dir names

 

 

this removes the parent drectory also which i need to keep as its the users home folder.

Posted

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

 

set ofso=createobject("scripting.filesystemobject")
'***********************
delfolder "d:\home\user1"
delfolder "d:\home\user2"
'***********************

Sub delfolder(x)
 dim oFolder
 if ofso.folderexists(x) then
   set oFolder=ofso.getfolder(x)
   for each oSubFolder in oFolder.subfolders
     ofso.deletefolder oSubfolder,true
   next
   for each oFile in oFolder.files
     oFSO.deletefile oFile,true
   next
 end if
End Sub

  • Thanks 1
Posted

Seems I got pipped to the post :p

 

Could also try it like:

 


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
)

 

Think that should work, takes directories from the testing.txt file (you need to put them in etc etc)

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 :D (Remember it needs direct paths, CMD doesnt support UNC or doesnt seem to)

 

Steve

  • Thanks 1
Posted

If you want this just as a batch script then you could use

 

@echo off

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"

 

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)

 

so if you had for example a folder structure:-

 

D

--users

----y2010

------pupil1

------pupil2

----y2009

------pupil3

------pupil4

----y2008

------pupil5

------pupil6

 

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

  • Thanks 2
  • 2 months later...
Posted
If you want this just as a batch script then you could use

 

@echo off

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"

 

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)

 

so if you had for example a folder structure:-

 

D

--users

----y2010

------pupil1

------pupil2

----y2009

------pupil3

------pupil4

----y2008

------pupil5

------pupil6

 

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

Thanks for this! Worked exactly as I wanted it to!

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...