+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
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 ...
  1. #1
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Script to Delete Files and folders within a folder

    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

  2. #2

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,240
    Thank Post
    1,058
    Thanked 1,068 Times in 625 Posts
    Rep Power
    740
    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

    Code:
    $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)

  3. #3
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

  4. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,715
    Thank Post
    3,246
    Thanked 1,048 Times in 970 Posts
    Rep Power
    364
    mattx has suggested an AutoIT script so different to vbs but still a scripting language

  5. #5
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    ok thanks for that guys

  6. #6
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Hi People,

    would anyone know how to achieve this using VB or Batch Scripts?

    Cheers

  7. #7

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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
    Last edited by Steve21; 5th April 2011 at 12:58 PM.

  8. #8
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

  9. #9
    somabc's Avatar
    Join Date
    Oct 2007
    Location
    London
    Posts
    2,337
    Thank Post
    83
    Thanked 388 Times in 258 Posts
    Rep Power
    111
    rd /s is the command you want

    you could feed this a list of dir names

  10. #10

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    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

  11. #11
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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

  12. #12
    doddsworthy's Avatar
    Join Date
    Jul 2009
    Location
    Washington
    Posts
    18
    Thank Post
    3
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by somabc View Post
    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.

  13. #13

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,154
    Thank Post
    114
    Thanked 527 Times in 450 Posts
    Blog Entries
    2
    Rep Power
    123
    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

    Code:
    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

  14. Thanks to srochford from:

    doddsworthy (5th April 2011)

  15. #14

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,689
    Thank Post
    334
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Seems I got pipped to the post :P

    Could also try it like:

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

    Steve

  16. Thanks to Steve21 from:

    doddsworthy (5th April 2011)

  17. #15
    AlexB's Avatar
    Join Date
    Jul 2006
    Location
    Warwickshire
    Posts
    365
    Thank Post
    36
    Thanked 36 Times in 32 Posts
    Rep Power
    23
    If you want this just as a batch script then you could use

    Code:
    @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

  18. 2 Thanks to AlexB:

    doddsworthy (5th April 2011), Zoom7000 (17th June 2011)

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 1st July 2014, 08:34 AM
  2. Script to delete certain folders
    By Pumaedition in forum Scripts
    Replies: 4
    Last Post: 22nd April 2008, 10:08 PM
  3. Replies: 2
    Last Post: 17th April 2008, 11:25 AM
  4. Replies: 5
    Last Post: 9th March 2008, 12:15 AM
  5. Replies: 3
    Last Post: 25th September 2006, 12:27 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •