Announcement

Collapse
No announcement yet.

Deleting contents of multiple user home folders

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting contents of multiple user home folders

    Hi,

    I'm looking for a way that I can delete the contents of multiple usersí home folders without deleting the folder itself.

    User home folders are within a DFS share so the structure would look something like this:

    Student folders

    \\domainname\domain\share\username\

    and also:

    Staff Folders

    \\domainname\domain\share\username\home

    I hope that I have provided enough information.
    Any assistance would be appreciated.

    Thanks

  • #2
    Re: Deleting contents of multiple user home folders

    sure. thats not a problem at all. i have to do this periodically at my work for the work stations.

    you didnt really say what you wanted deleted, when, or by what means, so ill just give you a frig sample...

    the people at my work use a web page in house to retrieve court files, tax deeds, marriage license, probate docs, etc, etc... the images are tiff files and they are pulled and then incorporated into a.pdf on the fly. this crowds the temp directory after a while, so i run a batch every couple weeks. the .bat files are created unique for each department, but the .bat is started from one called common.bat. it calls other .bats in the sysvol... like the line in red below:
    (all these .bat examples are stripped so you can see what were trying to accomplish... besides, my code looks like crap, unlike Rems )
    Code:
    @echo off
    net time /set /rtsdomain:DOMAIN_NAME_HERE.com /y
    REM Calls ClearTemp.bat: unREM to activate!!
    call \\alpha\sysvol\duvalflclerk.com\scripts\cleartemp.bat
    it calls ClearTemp.bat which has some lines like this:

    Code:
    cd\
    cd "WINDOWS\Temp"
    erase *.* /q
    you could do the same by changing that to something like this:
    Code:
    if exist h: goto delete
    :delete
    net use h: /delete 
    net use h: \\<file_server>\home\%username%
    cd h:
    erase *.* /q <--- i cant remember the arguement to recurse subdirectories in windows!! anybody? rems buddy??
    exit
    that will delete the contents of their home folder, but not the contents of any folders contained therein, cause i cant remember the switch. in addition, this is a logon script, thus it only works when they logon and sometimes i have people that dont come in for a while. if you are trying to reclaim space, then you may want to run it from the server via cmd prompt to have it done immediately.

    then, you can rem the line when you dont want to use it, and if you need to do it again, unrem it.

    thats just one method.. there are several others, but like i said, you didnt say how you wanted to accomplish it.

    hope thats enuf to get you started. if not, let us know and we can go another way!

    ***EDIT***


    ok, you can use the '/s' switch to delete the contents of the folders in directory, but it still wont delete the folders themselves... sorry, i just cant find it and cant remember. sux...
    Code:
    erase * /s /q
    and i forgot to tell you wha tthe '/q' switch is for... when you delete or erase folders via cmd line, it will ask you 'Are you sure? y/n:"... the '/q' just answers yes to all the prompts. if you dont include it the batch will hang waiting for user input.
    Last edited by James Haynes; 29th December 2007, 05:16. Reason: cause i forgot stuff
    its easier to beg forgiveness than ask permission.
    Give karma where karma is due...

    Comment


    • #3
      Re: Deleting contents of multiple user home folders

      Hi James, Thanks for the info.
      What I am trying to delete is all files and folders under the main home folder that resides on a server under a DSF namespace.

      Everyone has backed up their folders contents( too bad if they have not) and I need to now delete it all, however I donít wish to go to each folder individually and delete the contents as this will take too long.
      So what I am trying to achieve is to run a script that will delete ALL content (including folders) but leave the main home folder intact.

      So

      (Staff folder directory)

      //domain.com.au/domain/share/$username%/home / -everything in this folder will need to be deleted (subfolders included)

      (Student folder directory)

      //domain.com.au/domain/share/$username%/ -everything in this folder will need to be deleted (subfolders included)

      As I am not a scripter, I need help.
      Thanks

      Comment


      • #4
        Re: Deleting contents of multiple user home folders

        im sorry, i just dont know how to make it delete directories without specifiing a name... when you use the 'rmdir' you still have to say 'rmdir <folder name>'... it doesnt take wildcards.

        maybe you should have put this in the scripting forum. someone that could answer this for us both is more likely to see it there... and this cant be that difficult. surely someone has written a batch file to delete all the contents of a folder.

        ***EDIT***

        actually, i was just thinking.. everyone had to back up their stuff already, right? and you said that you have the path for the two kinds of home folder users...

        so, are you assigning the home folder location in the ADUC section using the "/domain.com.au/domain/share/$username%/ "?

        then just delete the contents of the parent directory with explorer by dragging a box around all the shares, then deleting them. that will clear ALL the contents, then when the user logs back on, the file will be recreated. no harm done.

        is that a viable solution? i like it...
        Last edited by James Haynes; 29th December 2007, 22:19.
        its easier to beg forgiveness than ask permission.
        Give karma where karma is due...

        Comment


        • #5
          Re: Deleting contents of multiple user home folders

          Hi James, Thanks again for your help.
          i'll give your sugestion a go later this afternoon.

          however i would still need to get the home folder directories (//domain.com.au/domain/share/$username%/home / -everything in this folder will need to be deleted (subfolders included))
          cleaned out from staff so i will also post in the scripting area

          Thanks again for everything.

          Comment


          • #6
            How to delete all contents under the main home folder

            What I am trying to delete is all files and folders under the main home folder that resides on a server under a DSF namespace.

            Everyone has backed up their folders contents( too bad if they have not) and I need to now delete it all, however I donít wish to go to each folder individually and delete the contents as this will take too long.
            So what I am trying to achieve is to run a script that will delete ALL content (including subfolders) but leave the main home folder intact.

            So...

            (Staff folder directory)

            //domain.com.au/domain/share/$username%/home /
            -everything in this folder will need to be deleted (subfolders included)

            (Student folder directory)

            //domain.com.au/domain/share/subfolder/%username%/
            -everything in this folder will need to be deleted (subfolders included)

            As I am not a scripter, I need help.
            Thanks
            Last edited by eyeofra; 30th December 2007, 10:12. Reason: Adjust title

            Comment


            • #7
              Re: delete is all contents under the main home folder

              del /s (delete everything within a subdir)
              del /q (quiet mode - doesn't ask for confirmation)

              have no way of testing it here, but you might need to map the share to a local drive to run the "del /s /q" command, since I'm not sure whether it will accept UNC paths
              ________
              Iphone games
              Last edited by DYasny; 6th March 2011, 17:58.
              Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

              BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

              Comment


              • #8
                Re: Deleting contents of multiple user home folders

                I would just delete everything. after that, if a directory structure is important and needs to be maintained, put directory creation commands in the user logon script

                Code:
                if EXIST z:\path\%username% mkdir z:\path\%username%\anotherdir
                ________
                Fn platform
                Last edited by DYasny; 6th March 2011, 17:59.
                Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

                BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

                Comment


                • #9
                  Re: Deleting contents of multiple user home folders

                  Originally posted by DYasny View Post
                  I would just delete everything. after that, if a directory structure is important and needs to be maintained, put directory creation commands in the user logon script

                  Code:
                  if EXIST z:\path\%username% mkdir z:\path\%username%\anotherdir
                  Thanks DYasny,
                  i'll give it ago on wednesday at work

                  Comment


                  • #10
                    Re: Deleting contents of multiple user home folders

                    correction in the code there:

                    if NOT EXIST...
                    ________
                    EASY VAPE VAPORIZER
                    Last edited by DYasny; 6th March 2011, 17:59.
                    Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

                    BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

                    Comment


                    • #11
                      Re: Deleting contents of multiple user home folders

                      I think you should look into using Robocopy, which can deal with levels of folders, using the /LEV:n switch.

                      Also, robocopy can use the /XF *.* switch to simply create a folder structure, i.e. no files.

                      So step one is to create a folder structure, 1 level deep, to another place (share, drive, whatever). Step two is to purge the entire source tree. Step three is to copy the folder tree from step one back to it's original place. Here goes:

                      I created a folder called TOPLEVEL which has several folders and files within, down to many layers of sub-sub-sub folders. The folders one level below TOPLEVEL are the ones we wish to keep. So we put a LEV:2 switch. The /E switch includes empty subfolders, for those people who have a home folder but with nothing in, which is valid. The /SEC switch copies folder security.
                      So step one is this:
                      Code:
                      md tempfolder
                      robocopy toplevel tempfolder /xf *.* /LEV:2 /E /SEC
                      Step two is to get rid of the entire orginal tree:
                      Code:
                      rd toplevel /s /q
                      Step three is to rename our temp folder to the original named folder:
                      Code:
                      rename tempfolder toplevel
                      Hope all that makes sense! I have just tested it and it all works fine, resulting in a folder called toplevel with another (single) layer of folders inside it, with no files at all and no folders beneath the second layer of folders.
                      Best wishes,
                      PaulH.
                      MCP:Server 2003; MCITP:Server 2008; MCTS: SBS2008

                      Comment


                      • #12
                        Re: Deleting contents of multiple user home folders

                        Originally posted by eyeofra View Post
                        So what I am trying to achieve is to run a script that will delete ALL content (including subfolders) but leave the main home folder intact.

                        So...

                        (Staff folder directory)
                        \\domain.com.au\domain/share\%username%\home / - everything in this folder will need to be deleted (subfolders included)

                        (Student folder directory)
                        \\domain.com.au\domain\share\subfolder\%username% / -everything in this folder will need to be deleted (subfolders included)
                        In what way do you want to execute the script/batch?
                        Do you planned to Run it once as Administrator and clean all the home folders at once (then, there is a chance to don't have access permissions and must take ownership of the folders and all content first)?
                        Then check RoboCopy.
                        Or, do you want to run the script as a user logon script, that cleans the user folder at logon? (per user)


                        The Del (or Erase) command delete files from the folder (and from the subfolders if you are using the /s swith), this command does accept UNC-paths.
                        But is does not delete the folder and subfolders, just the files in them.
                        If you are looking for a script solution, for a batch you have to use the Del command for the files in the main folder (to preserve the main folder, its security included).
                        And you use the RD (or Rmdir) command to delete all the subfolders and automatically all what is in them. This command does also accept UNC-paths

                        But that will not be enough!
                        The Del command does not delete all files or folders that have certain attributes assigned to them. Therefor you might want to use the Attrib.exe command as wel.
                        Now The Attrib command does not accept UNC-paths, since you cannot make an UNC-path the active drive\folder in Dos.

                        This batch use an on_the_fly drive mapping as workaround (it will find by it self any available drive letter, this will be disconnected again at the end). The temp drive mapping will be set as the active path for the Attrib command.
                        Code:
                        :: use this batch as User Logon script
                        :: (it is using the user's own environment
                        ::  variable: %username% as the root folder)
                        
                        @echo off
                        
                        (Set DFSPath=\\domain.com.au\domain\share\%username%)
                        
                        title,Clean %DFSPath% & COLOR 9E
                        
                         For /F "tokens=2" %%* In ('Net Use * "%DFSPath%" ^|Find ":"') Do (
                          (Set $Drive=%%*)&(ping localhost -w 1 -n 2 >Nul) )
                        
                        :: Delete all files and subfolders
                        If EXIST "%$Drive%" (
                           cd/D "%$Drive%" && (
                           ATTRIB -S -H -R -A /D /S >nul & (
                           For /f "Tokens=*" %%* in ('dir "%$Drive%" /B') Do (
                            RD "%$Drive%\%%*" /S /Q || Del /F "%$Drive%\%%*")))
                         ) ELSE echo.The target path does not exist on this computer
                        
                        cd/D %temp%
                        Net use %$Drive% /Delete /Yes >Nul
                        The batch creates a temp drive mapping, using Net Use * to connect the first avaiable drive letter starting from Z: .

                        In the batch above the returned results of the Net Use * command is trapped to determine the drive letter from it. This works well for most languages.
                        If not, or you want to set a set of available drive letters your self you can also use this part of code.
                        Code:
                        : <setting DFSPath>
                        
                           (Set $Drive=A B Z Y X W M L K)
                              For %%* In (%$Drive%) Do (
                               If Not EXIST %%*: (
                                (Set $Drive=%%*:)&GoTo:MapDrive))
                           :MapDrive
                           Net use %$Drive% "%DFSPath%" /Persistent:No
                        
                        : <delete files and subfolders>
                        
                        cd/D %temp%
                        Net use %$Drive% /Delete /Yes >Nul
                        \Rems
                        Last edited by Rems; 31st December 2007, 08:59. Reason: added the line: cd/D %temp% (is needed in the case the folder was already empty)

                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        __________________

                        ** Remember to give credit where credit's due **
                        and leave Reputation Points for meaningful posts

                        Comment

                        Working...
                        X