Announcement

Collapse
No announcement yet.

Q: Empty Shared folder in intervals

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

  • Q: Empty Shared folder in intervals

    Hello!

    I'm desperately searching for a script (prefferably batch file) to use on a shared folder within a server. The script I need should empty the contents of a shared folder (files and folders, any and all kind of data inside), without damaging the share properties. As in - either recreate the share with the desired permissions, or just delete all its content without removing the shared folder itself.

    Any help would be appreciated

    thanks!

  • #2
    Re: Q: Empty Shared folder in intervals

    Seems pretty simple to me. Why not create a batch file that does the following...

    Code:
    c:
    cd foldername
    attrib -r -a -s -h *.* /s /d
    dir /ad /b %folder%>temp.tmp
    for /f "tokens=*" %%f in (temp.tmp) do rd """%%f""" /q /s
    del *.* /q /s
    exit
    This will remove any and all folders/files in the share but do nothing to the shared folder itself.
    Schedule it to run at various times during the day.
    Last edited by ahinson; 19th March 2007, 20:45.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Q: Empty Shared folder in intervals

      it works.. up to a point.. it does not remove the empty directories..

      thanks for the help. if there's a way to make it complete i'd love to know though

      cheers.

      Comment


      • #4
        Re: Q: Empty Shared folder in intervals

        Originally posted by mulderfox View Post
        it works.. up to a point.. it does not remove the empty directories..

        thanks for the help. if there's a way to make it complete i'd love to know though

        cheers.
        Hmmm, it removes empty folders for me.

        To test this I created a folder:

        1->no files
        |_2->no files->3->test.txt
        |_3->no files->4->empty
        |_4->no files->5->empty

        It removed everthing, files and empty folders. (this was in Vista though) I'll try it on the server.

        There was however a typo in the batch script. The attrib line was "attrib -r -a -s- h"... it wouldn't remove the hidden attribute from files this way. Fixed it.
        Its possible the folders that weren't removed had hidden files in them. Dunno otherwise.

        -----

        Tested it on my server and it worked fine. Maybe someone else can verify that it works.
        Last edited by ahinson; 19th March 2007, 14:49.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          Re: Q: Empty Shared folder in intervals

          ** I think I found the fault in your batch.. the %%f variable does not include " before and after the folder's name as required by RD command..
          How do I add it? thanks **


          I created a test folder named "test"
          under it I created another folder called "new folder" and under that another folder with the same name ("new folder")
          in each of the folders I put empty files.
          c:\test
          c:\test\new folder
          c:\test\new folder\new folder


          and activated the batch after putting "test" instead of "foldername"

          the result was - all the files were deleted, but all the folders were not. (as in, none of the "new folder" folders)

          any idea?
          thanks.
          Last edited by mulderfox; 19th March 2007, 15:25.

          Comment


          • #6
            Re: Q: Empty Shared folder in intervals

            Originally posted by mulderfox
            ** I think I found the fault in your batch.. the %%f variable does not include " before and after the folder's name as required by RD command..
            How do I add it? thanks **
            In that case, use quotes between quotes;
            Code:
            c:
            cd "c:\Shared folder"
            attrib -r -a -s -h *.* /s /d
            dir /ad /b %folder%>temp.tmp
            for /f "tokens=*" %%f in (temp.tmp) do rd """%%f""" /q /s
            del *.* /q /s
            exit

            btw.
            If you don't care about the NTFS permissions you can also delete the complete folder:
            Code:
            regedit /E %temp%\exportShares.dat "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares"
            start /wait net stop lanmanserver
            rd /s /q "C:\Shared folder"
            md "C:\Shared folder"
            rem start /wait net start lanmanserver
            regedit /I /S %temp%\exportShares.dat
            start /wait net stop lanmanserver && start /wait net start lanmanserver
            exit
            \Rem


            EDIT:
            In addition to the last batch,
            In the short period the batch is running, you don't even have to copy all the share Settings from the registry, because it wil not be changed as long the service is stopped.
            So the batch can even be shorter:
            Code:
            start /wait net stop lanmanserver
            rd /s /q "C:\Shared folder"
            md "C:\Shared folder"
             :: the new folder must have exact the same name as the deleted folder!!!
            
            start /wait net start lanmanserver
            exit
            This method also keep the share pemissions, but it don't keep the NTFS permissions of that folder!
            Last edited by Rems; 19th March 2007, 19:07.

            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


            • #7
              Re: Q: Empty Shared folder in intervals

              Originally posted by mulderfox View Post
              ** I think I found the fault in your batch.. the %%f variable does not include " before and after the folder's name as required by RD command..
              How do I add it? thanks **


              I created a test folder named "test"
              under it I created another folder called "new folder" and under that another folder with the same name ("new folder")
              in each of the folders I put empty files.
              c:\test
              c:\test\new folder
              c:\test\new folder\new folder


              and activated the batch after putting "test" instead of "foldername"

              the result was - all the files were deleted, but all the folders were not. (as in, none of the "new folder" folders)

              any idea?
              thanks.
              Ah Rems had the solution with the quotes. I fixed my original post. I neglected to take into account a space in the foldername.

              Thank Rems.
              Andrew

              ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

              Comment


              • #8
                Re: Q: Empty Shared folder in intervals

                thanks everyone! and especially ahinson! you've really helped me out

                Comment

                Working...
                X