ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    How To: for files

    IT Discussion
    windows 10 cmd forfiles delete older than
    3
    9
    973
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • gjacobseG
      gjacobse
      last edited by

      We have a DEV, working with MSSQL.

      This is the third time that I have had to go and delete files just so they can even sign into it. It’s been about 1,503 files each time.

      This time i used a winrs session to connect to his machine.

      From there, i drilled down to the folder and entered the command:

      Forfiles  /s /d -15 /c “cmd /c del *.tmp”
      

      Some how, it managed to delete - all files.

      From my breaking down the command, it should have worked, leaving the last 15 days worth.

      What - if anything - did I miss here?

      1 Reply Last reply Reply Quote 0
      • J
        JasGot
        last edited by

        Let's assume you have 1503 files. Your command runs "del *.tmp" 1503 times!

        You need to use: /M *.tmp to apply your del command to ONLY those *.tmp files.

        Try this as your new command:

        Forfiles /s /d -15 /M *.tmp /c “cmd /c del @file”

        1 1 Reply Last reply Reply Quote 0
        • 1
          1337 @JasGot
          last edited by 1337

          @JasGot said in How To: for files:

          Let's assume you have 1503 files. Your command runs "del *.tmp" 1503 times!

          You need to use: /M *.tmp to apply your del command to ONLY those *.tmp files.

          Try this as your new command:

          Forfiles /s /d -15 /M *.tmp /c “cmd /c del @file”

          Don't be sloppy, keep the case consistent.
          And skip the /S because we are not looking to delete anything in subdirectories.

          And don't forget Rule No 1 (check that it works first):

          forfiles /M *.tmp /D -15 /C "cmd /c echo @file"
          

          (You can actually skip /C "cmd /c echo @file" because that is the default command and just do forfiles /M *.tmp /D -15)

          Then actually delete the files:

          forfiles /M *.tmp /D -15 /C "cmd /c del @file"
          
          J 1 Reply Last reply Reply Quote 0
          • J
            JasGot @1337
            last edited by

            @Pete-S said in How To: for files:

            Don't be sloppy, keep the case consistent.

            Pretty code is his problem not mine. "/M" is from the man pages.

            Why would you alter his code (/s ) without knowing if he wanted subs or not? He never addressed that. Further, the issue was deleting, not going too deep.

            See if it works? Are we debugging his code? Or teaching him how to write pretty code?

            You could have made your remarks without quoting me and would have been successful without being offensive.

            1 1 Reply Last reply Reply Quote 1
            • 1
              1337 @JasGot
              last edited by

              @JasGot said in How To: for files:

              @Pete-S said in How To: for files:

              Don't be sloppy, keep the case consistent.

              Pretty code is his problem not mine. "/M" is from the man pages.

              Why would you alter his code (/s ) without knowing if he wanted subs or not? He never addressed that. Further, the issue was deleting, not going too deep.

              See if it works? Are we debugging his code? Or teaching him how to write pretty code?

              You could have made your remarks without quoting me and would have been successful without being offensive.

              You're right and I apologize. It wasn't my intention to offend anyone and I while I was writing in a hurry, I could have worded it differently.

              J 1 Reply Last reply Reply Quote 2
              • J
                JasGot @1337
                last edited by

                @Pete-S Thank you. I still respect you and what you offer here.

                1 Reply Last reply Reply Quote 1
                • gjacobseG
                  gjacobse
                  last edited by

                  As the CMD was closed when I perform my weekly reboot of my computer, I can’t go back now a compare the syntax.

                  That said, from what you are saying; is my ‘failure’ was omitting the /M which specifies the ‘searchmask’. And because of that omission, it didn’t have the comparative variable, and deleted everything.

                  Which would make sense. As the user reported this happens every 45 days, and while i sent him all of the information to address this himself, I’ll give it a few days to build up some files, and then look at it again.

                  And likely, use the >NUL switch, >NUL is good way to test i do agree.

                  J 1 2 Replies Last reply Reply Quote 0
                  • J
                    JasGot @gjacobse
                    last edited by

                    @gjacobse said in How To: for files:

                    As the CMD was closed when I perform my weekly reboot of my computer, I can’t go back now a compare the syntax.

                    That said, from what you are saying; is my ‘failure’ was omitting the /M which specifies the ‘searchmask’. And because of that omission, it didn’t have the comparative variable, and deleted everything.

                    Which would make sense. As the user reported this happens every 45 days, and while i sent him all of the information to address this himself, I’ll give it a few days to build up some files, and then look at it again.

                    And likely, use the >NUL switch, >NUL is good way to test i do agree.

                    The /M tells ForFiles which files you want to run your command against. So if you use /M *.tmp, your action will only be run against the tmp files. If you omit /M, your command (which is a del command) will be run against all files.

                    The best way to test your command line, is to use the Echo instead of Del, as Pete pointed out.

                    1 Reply Last reply Reply Quote 0
                    • 1
                      1337 @gjacobse
                      last edited by 1337

                      @gjacobse said in How To: for files:

                      That said, from what you are saying; is my ‘failure’ was omitting the /M which specifies the ‘searchmask’. And because of that omission, it didn’t have the comparative variable, and deleted everything.

                      There were two things that tripped you up.

                      • The lack of /M to just find *.tmp files
                      • Using del *.tmp as the command

                      First forfiles is a for-loop that executes whatever command you want for each file it finds.
                      As @JasGot mentioned you will get one hit for each file it will find.

                      With /D -15 you are specifying that you only want to find files older than 15 days.
                      With /S you are telling forfiles to look in all subdirectories as well. You may or may not want that.
                      With /M you can specify a filter which matching filenames you want to search for. Without /Mit will find all the files regardless if it's named 123.tmp or installation.txt

                      Your initial command would find all files in all subdirectories regardless if it what name it had. That's why you needed /M.

                      Secondly, forfiles will execute whatever command you want for each and every file it finds.
                      Since your command was del *.tmp it would delete all tmp files every time it found a file.
                      What you really wanted was to just delete the file it found.
                      The @file will have the name of the file so del @file would only delete the file that was found.

                      1 Reply Last reply Reply Quote 2
                      • 1 / 1
                      • First post
                        Last post