Announcement

Collapse
No announcement yet.

Script to delete Files inside a dir

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

  • Script to delete Files inside a dir

    Hi Guys,

    i need a script to delete files on a specific dir,
    I don't want to remove the dir and recreate it because of security permissions on that dir.
    i need to erase all inside \\SERVERNAME\TRANSFER
    and all subdirectories once a week through scheduled tasks.
    i found some VBScripts but they were not what i want.
    Anybody got some idea?

    Thanks,
    Retaliator
    Thanks & Regards

    Retaliator

    MCSA/MCSE/CCNA
    Computer Science Graduate

  • #2
    Re: Script to delete Files inside a dir

    What about

    del \\SERVERNAME\TRANSFER\*.* /Q

    Save it as a batch file and it should work.

    I just tested it from my machine to a test folder on my server and it works without a hitch.
    Last edited by wullieb1; 19th July 2007, 10:56.

    Comment


    • #3
      Re: Script to delete Files inside a dir

      This will not delete sub-dirs as far as i tried.

      Retaliator.
      Thanks & Regards

      Retaliator

      MCSA/MCSE/CCNA
      Computer Science Graduate

      Comment


      • #4
        Re: Script to delete Files inside a dir

        To delete sub-dirs add one more command

        rd /s \\SERVERNAME\TRANSFER /q
        Cheers!!
        MCSE 2003,MCSA- Messaging 2003, VCP

        Comment


        • #5
          Re: Script to delete Files inside a dir

          This will remove the root dir (Transfer)
          Retaliator.
          Thanks & Regards

          Retaliator

          MCSA/MCSE/CCNA
          Computer Science Graduate

          Comment


          • #6
            Re: Script to delete Files inside a dir

            Let's say your source folder is C:\TRANSFER so you can create a batch file on the C: drive (i.e. above TRANSFER) which says this:

            Code:
            cd "C:\Transfer"
            del *.* /q /s
            rd . /q /s
            Note the spaces before and after the dot on the rd line

            Now, the rd command will issue an error saying that it cannot access the file because it is in use by another process, but this is just it's way of saying it cannot remove the source folder "TRANSFER" (which is dot, because we have changed to that folder using the cd command) and that's just what we want.

            The source folder "C:\TRANSFER" ends up staying, but is emptied of all files and folders.

            It works on my Vista PC so I hope it's what you want.
            Best wishes,
            PaulH.
            MCP:Server 2003; MCITP:Server 2008; MCTS: SBS2008

            Comment


            • #7
              Re: Script to delete Files inside a dir

              Thanks a lot,
              Retaliator
              Thanks & Regards

              Retaliator

              MCSA/MCSE/CCNA
              Computer Science Graduate

              Comment


              • #8
                Re: Script to delete Files inside a dir

                I've been thinking this is a dangerous command file, so you may want to put some error checking in there, such as create a file explicitly in the TRANSFER folder by doing this in the first line of the bacth file:
                Code:
                echo hello > c:\transfer\errorcheck.txt
                then before doing any deleting, do a check for that file by seeing if it exists, [i]without[/] it's full path, to make sure you are "in" the transfer folder like this:
                Code:
                cd c:\tansfer
                if not exist errorcheck.txt goto INWRONGFOLDER
                   ...do your deletion here...
                :INWRONGFOLDER
                I'm just concerned that if there is a del or a rd anywhere in a command file, it's a pretty dangerous thing to do, so you need to think about having some safety in there before doing the deletion.
                Best wishes,
                PaulH.
                MCP:Server 2003; MCITP:Server 2008; MCTS: SBS2008

                Comment


                • #9
                  Re: Script to delete Files inside a dir

                  Okay, this is my script now with your addon:

                  Code:
                  echo hello > g:\transfer\errorcheck.txt
                  cd "G:\Transfer"
                  g:
                  if not exist errorcheck.txt goto INWRONGFOLDER
                  del *.* /q /s
                  rd . /q /s
                  :INWRONGFOLDER
                  On the 3rd line i added g: because when i tried that manually after the change directory line 2 (cd "G:\Transfer"),
                  you still showed up on the last directory so just in case.

                  Thanks, Retaliator
                  Last edited by Retaliator; 24th July 2007, 12:40.
                  Thanks & Regards

                  Retaliator

                  MCSA/MCSE/CCNA
                  Computer Science Graduate

                  Comment


                  • #10
                    Re: Script to delete Files inside a dir

                    OK that's good - regarding putting the G: in there, you should change to the G: drive first, then do the CD command, so:
                    Code:
                    echo hello > g:\transfer\errorcheck.txt
                    g:
                    cd "G:\Transfer"
                    if not exist errorcheck.txt goto INWRONGFOLDER
                    del *.* /q /s
                    rd . /q /s
                    :INWRONGFOLDER
                    Best wishes,
                    PaulH.
                    MCP:Server 2003; MCITP:Server 2008; MCTS: SBS2008

                    Comment


                    • #11
                      Re: Script to delete Files inside a dir

                      As i see it you are emptying the folder of both files and folders so you could delete the actual folder then recreate the folder at the end of the script.

                      There is always the option of copying the permissions from the folder to a text file then adding them when at the end after the recreate.

                      I'll get working on something.

                      Comment


                      • #12
                        Re: Script to delete Files inside a dir

                        Yup, Mr. W., you are correct. His first wish was that he did not wish to erase the G:\TRANSFER folder becuase of the permissions on it. I just think that resetting the permissions would be harder than doing this simple batch file, but that's just my little thought. Cheers for now,
                        Best wishes,
                        PaulH.
                        MCP:Server 2003; MCITP:Server 2008; MCTS: SBS2008

                        Comment


                        • #13
                          Re: Script to delete Files inside a dir

                          Originally posted by PaulH View Post
                          Yup, Mr. W., you are correct. His first wish was that he did not wish to erase the G:\TRANSFER folder becuase of the permissions on it. I just think that resetting the permissions would be harder than doing this simple batch file, but that's just my little thought. Cheers for now,
                          Yeah i think i was mind wandering when i wrote that.

                          Here is something i have played with over the past couple of days.

                          Code:
                          dir = "C:\Test"
                          
                          Dim fso 
                          Dim objFolder
                          Dim objFile
                          Dim objSubFolder
                          
                          Set fso = createobject("Scripting.FileSystemObject")
                            
                          Set objFolder = fso.GetFolder(dir)
                          
                          Set colSubfolders = objFolder.Subfolders
                            
                          For Each objFile In oFolder.files
                            objFile.Delete True
                          Next
                          
                          
                          Set objFolder = fso.GetFolder(dir)
                          For Each oFile In objFolder.files
                            objFile.Delete True
                          Next
                          
                          For Each objSubfolder in colSubfolders
                            fso.DeleteFolder(objSubFolder)
                          Next
                          
                          Set oSubFolder = Nothing
                          Set oFolder = Nothing
                          Set fso = Nothing
                          This deletes the files and folders in the directory c:\test.

                          Comment


                          • #14
                            Re: Script to delete Files inside a dir

                            Nice script to delete files by date. (older then)

                            Dim fso, f, f1, fc, s, folderspec, diff
                            folderspec = "d:\sqlbackup\hourly" 'Change folder path here
                            Set fso = CreateObject("Scripting.FileSystemObject")
                            Set f = fso.GetFolder(folderspec)
                            Set fc = f.Files
                            For Each fl in fc
                            diff = date - fl.datelastmodified
                            if diff > 7 then
                            ' wscript.echo fl.name & " deleted"
                            fl.delete
                            end if
                            Next
                            Last edited by Retaliator; 30th July 2007, 11:17.
                            Thanks & Regards

                            Retaliator

                            MCSA/MCSE/CCNA
                            Computer Science Graduate

                            Comment


                            • #15
                              Re: Script to delete Files inside a dir

                              Originally posted by Retaliator View Post
                              Nice script to delete files by date. (older then)

                              Dim fso, f, f1, fc, s, folderspec, diff
                              folderspec = "d:\sqlbackup\hourly" 'Change folder path here
                              Set fso = CreateObject("Scripting.FileSystemObject")
                              Set f = fso.GetFolder(folderspec)
                              Set fc = f.Files
                              For Each fl in fc
                              diff = date - fl.datelastmodified
                              if diff > 7 then
                              ' wscript.echo fl.name & " deleted"
                              fl.delete
                              end if
                              Next

                              or a simpler batch (with FORFILES included in Windows 2003):
                              Code:
                              forfiles /P C:\LogFiles\myLogs /M *.log /S /D -30 /C "cmd /c del /q @file"
                              (where 30 is the number of days)

                              Comment

                              Working...
                              X