Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Why Doesn't This Batch File Work?  (Read 11387 times)

0 Members and 1 Guest are viewing this topic.

gmgdr11

    Topic Starter


    Rookie

    • Experience: Experienced
    • OS: Windows 7
    Why Doesn't This Batch File Work?
    « on: November 01, 2014, 08:24:13 PM »
    Hi everyone,

    I'm having problems with the final step of my batch file and I can't figure out why it doesn't work.

    The final step is to remove the larger directories and to keep the smallest. The code is as follows:

    Code: [Select]
    @echo off
    setlocal EnableDelayedExpansion

    rem Get size of all folders
    set smallestSize=9999999999
    for /D %%a in (*) do (
       set size=0
       for %%b in (%%a\*.*) do set /A size+=%%~Zb
       if !size! lss !smallestSize! (
          set smallestSize=!size!
          set smallestName=%%a
       )
    )

    echo Deleted folder: "%smallestName%"
    pause

    rem Delete all folders, except the smallest one
    for /D %%a in (*) do (
       if "%%a" neq "%smallestName%" rmdir /S /Q "%%a"

    It properly identifies the smallest directory, but after the point where it displays the smallest and pauses awaiting a key press, it fails to remove the larger directories.

    Any help would be greatly appreciated.

    Thanks.

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Why Doesn't This Batch File Work?
    « Reply #1 on: November 01, 2014, 10:52:46 PM »
    You forgot to close the last for loop with a ).

    rem Delete all folders, except the smallest one
    for /D %%a in (*) do (
       if "%%a" neq "%smallestName%" rmdir /S /Q "%%a"
    )
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    gmgdr11

      Topic Starter


      Rookie

      • Experience: Experienced
      • OS: Windows 7
      Re: Why Doesn't This Batch File Work?
      « Reply #2 on: November 01, 2014, 11:40:01 PM »
      ^^Yes that was so obvious--thank you!

      Now that this works, I have another issue that I would like to correct with this batch program.

      I have the following directory structure where this batch file will be executed:

      Code: [Select]
      DIR1
         DIRA
         DIRB
         DIRC
      DIR2
         DIRD
         DIRE
         DIRF
         DIRG
      DIR3
         DIRH
         DIRI

      When run, it deletes DIR1 and DIR3 and only keeps DIR2 because it is the largest directory. What I'd like the result to be is as follows:

      Code: [Select]
      DIR1
         DIRC
      DIR2
         DIRE
      DIR3
         DIRH[code]

      In other words, I want to keep all folders in the root directory where the batch file located and only keep the smallest directory within each of the subdirectories.

      Is this possible? If not, then what I'd have to do is copy the batch file into each of the main root folders (DIR1, DIR2, DIR3) and run it each time in each of those folders.

      Any further (and final) help would be very helpful.

      Lemonilla



        Apprentice

      • "Too sweet"
      • Thanked: 70
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Why Doesn't This Batch File Work?
      « Reply #3 on: November 01, 2014, 11:44:45 PM »
      Look at the for /r command, I believe this is what you are looking for.  I'm not very well versed in it's uses, so I can't write you one off the top of my head.
      Quote from: patio
      God Bless the DOS Helpers...
      Quote
      If it compiles, send the files.

      gmgdr11

        Topic Starter


        Rookie

        • Experience: Experienced
        • OS: Windows 7
        Re: Why Doesn't This Batch File Work?
        « Reply #4 on: November 01, 2014, 11:51:11 PM »
        Look at the for /r command, I believe this is what you are looking for.  I'm not very well versed in it's uses, so I can't write you one off the top of my head.

        It's ok, I appreciate your help.

        Maybe someone else knows and can help me out with this one.

        Lemonilla



          Apprentice

        • "Too sweet"
        • Thanked: 70
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: Why Doesn't This Batch File Work?
        « Reply #5 on: November 01, 2014, 11:53:56 PM »
        If no one else replies when I get back on tomorrow I'll start running some tests and see if I can get it for you.  Good luck though.
        Quote from: patio
        God Bless the DOS Helpers...
        Quote
        If it compiles, send the files.

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Why Doesn't This Batch File Work?
        « Reply #6 on: November 02, 2014, 12:34:30 AM »
        Batch math fails above a number around 2 GB.

        An accurate description of the file size limits would help to provide something that works.



        This question has been multiposted:

        http://stackoverflow.com/questions/26695993/final-step-of-batch-file-doesnt-work-why
        http://stackoverflow.com/questions/26689398/batch-file-commands-to-keep-smallest-directory-and-delete-all-others

        gmgdr11

          Topic Starter


          Rookie

          • Experience: Experienced
          • OS: Windows 7
          Re: Why Doesn't This Batch File Work?
          « Reply #7 on: November 02, 2014, 01:13:01 AM »
          Batch math fails above a number around 2 GB.

          An accurate description of the file size limits would help to provide something that works.



          This question has been multiposted:

          http://stackoverflow.com/questions/26695993/final-step-of-batch-file-doesnt-work-why
          http://stackoverflow.com/questions/26689398/batch-file-commands-to-keep-smallest-directory-and-delete-all-others

          Yes, I had posted this question elsewhere. The original problem I had has been resolved. The only thing I need to figure out is what I asked in post #3 of this thread.

          The sizes of the directories are not over 2GB. Comparing the sizes and deleting the smallest directories (DIRA, DIRB, DIRC, etc.) is what I am trying to achieve. Comparing the sizes and deleting the smallest directories (DIR1, DIR2, DIR3, etc.) which are in the same folder as the batch file is NOT what I want to achieve but that is what the batch file does now in the way that it's written.

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: Why Doesn't This Batch File Work?
          « Reply #8 on: November 02, 2014, 02:58:11 AM »
          Remove the echo keyword after testing - at present it will echo the RD commands to the console.

          Code: [Select]
              @echo off
               :: keeps only the smallest folder, deletes all the largest folders, in all the next level folders below this folder
              setlocal EnableDelayedExpansion
              for /d %%a in (*) do (
                (for /d %%b in ("%%a\*") do (
                   for /f "tokens=3" %%c in ('dir "%%b" /s /-c ^|find "File(s)" ') do set "size=000000000000000%%c"
                   echo !size:~-15! "%%b"
                  )
                 )>"%temp%\checksize.bin"
              sort <"%temp%\checksize.bin" >"%temp%\checksize2.bin"
              for /f "usebackq skip=1 tokens=1,*" %%d in ( "%temp%\checksize2.bin") do echo rd /s /q "%%~e"
              del "%temp%\checksize?.bin"
              )
              pause
          « Last Edit: November 02, 2014, 03:31:22 AM by foxidrive »

          gmgdr11

            Topic Starter


            Rookie

            • Experience: Experienced
            • OS: Windows 7
            Re: Why Doesn't This Batch File Work?
            « Reply #9 on: November 02, 2014, 06:13:38 AM »
            Remove the echo keyword after testing - at present it will echo the RD commands to the console.

            Code: [Select]
                @echo off
                 :: keeps only the smallest folder, deletes all the largest folders, in all the next level folders below this folder
                setlocal EnableDelayedExpansion
                for /d %%a in (*) do (
                  (for /d %%b in ("%%a\*") do (
                     for /f "tokens=3" %%c in ('dir "%%b" /s /-c ^|find "File(s)" ') do set "size=000000000000000%%c"
                     echo !size:~-15! "%%b"
                    )
                   )>"%temp%\checksize.bin"
                sort <"%temp%\checksize.bin" >"%temp%\checksize2.bin"
                for /f "usebackq skip=1 tokens=1,*" %%d in ( "%temp%\checksize2.bin") do echo rd /s /q "%%~e"
                del "%temp%\checksize?.bin"
                )
                pause

            Created a new batch file with your code. When executed, the result is line after line of this:

            Code: [Select]
            'find' is not recognized as an internal or external command,
            operable program or batch file.

            foxidrive



              Specialist
            • Thanked: 268
            • Experience: Experienced
            • OS: Windows 8
            Re: Why Doesn't This Batch File Work?
            « Reply #10 on: November 02, 2014, 06:30:02 AM »
            Your Windows seems broken for some reason.

            A likely reason is when someone uses the PATH variable for some other use, or the path variable has been edited in the Windows GUI and parts removed.

            Salmon Trout

            • Guest
            Re: Why Doesn't This Batch File Work?
            « Reply #11 on: November 02, 2014, 06:30:08 AM »
            Created a new batch file with your code. When executed, the result is line after line of this:

            Code: [Select]
            'find' is not recognized as an internal or external command,
            operable program or batch file.

            Do you have a variable called %path% in your script?

            gmgdr11

              Topic Starter


              Rookie

              • Experience: Experienced
              • OS: Windows 7
              Re: Why Doesn't This Batch File Work?
              « Reply #12 on: November 02, 2014, 06:42:47 AM »
              Do you have a variable called %path% in your script?

              Well no. What I did to test that and generate that error was to copy the code in the post into a blank Notepad window, save it as "test.bat" in root folder of DIR1, DIR2, DIR3 and executed.

              I apologize for my very basic knowledge of batch file writing. Programming, even at the most basic level, is something I am not very familiar with.

              foxidrive



                Specialist
              • Thanked: 268
              • Experience: Experienced
              • OS: Windows 8
              Re: Why Doesn't This Batch File Work?
              « Reply #13 on: November 02, 2014, 07:01:36 AM »
              Well no. What I did to test that and generate that error was to copy the code in the post into a blank Notepad window, save it as "test.bat" in root folder of DIR1, DIR2, DIR3 and executed.

              Open a cmd window and type find /? and sort /?

              See if they both give you help.

              gmgdr11

                Topic Starter


                Rookie

                • Experience: Experienced
                • OS: Windows 7
                Re: Why Doesn't This Batch File Work?
                « Reply #14 on: November 02, 2014, 07:45:42 AM »
                Open a cmd window and type find /? and sort /?

                See if they both give you help.

                Ok! Fixed it. The issue was that I had the PATH environment variable set to C:\Cygwin64\.

                It displays the correct folders to be deleted. I get the following:

                Code: [Select]
                rd /s /q "DIR1\DIRA"
                rd /s /q "DIR1\DIRC"
                rd /s /q "DIR2\DIRG"
                rd /s /q "DIR2\DIRF"
                rd /s /q "DIR2\DIRE"
                rd /s /q "DIR3\DIRH"

                How does the batch file need to be modified then to execute those commands rather than display them now that it is confirmed that it accurately finds the proper directories to delete?

                Thanks.