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

Author Topic: Run 2 Commands, Keep Smallest File  (Read 5128 times)

0 Members and 1 Guest are viewing this topic.

gmgdr11

    Topic Starter


    Rookie

    • Experience: Experienced
    • OS: Windows 7
    Run 2 Commands, Keep Smallest File
    « on: December 08, 2014, 06:00:15 PM »
    Hi again everyone,

    After the help I was given previously I was attempting to write a new batch script to do pretty much what the one that was written for me does, but with different programs being run and a little different setup...Long story short, the "little" difference was enough to stump me again--so I need some help.

    Here is what I have. When run, this produces 2 files for each file:

    Code: [Select]
    dir /s /b /A-D DIR_1\ > list.txt
    for /r DIR_1 %%a in (*.*) do (dime -D CO=Q -L %%~dpa "%%a" "%%a.q")
    for /r DIR_1 %%a in (*.*) do (make -D CO=X -L %%~dpa "%%a" "%%a.x")
    for /F "tokens=*" %%b in (list.txt) do del /Q "%%b"

    The goal is to keep the smallest file of the two created. If the .q file is smaller, the script should delete the .x file, if the .x file is smaller, delete the .q file.

    Any help would be great! I've learned a lot from you all so far, so I'm hoping to learn some more with this one and reduce the amount of help I have to ask you all for :)

    Thanks.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Run 2 Commands, Keep Smallest File
    « Reply #1 on: December 09, 2014, 01:36:58 AM »
    The q and x files could be the same size.  What should happen if that occurs?

    gmgdr11

      Topic Starter


      Rookie

      • Experience: Experienced
      • OS: Windows 7
      Re: Run 2 Commands, Keep Smallest File
      « Reply #2 on: December 09, 2014, 03:47:50 AM »
      The q and x files could be the same size.  What should happen if that occurs?

      Well, the two different commands use two completely different compression algorithms. It is very unlikely that they would be exactly the same filesize. But, in that event, I would say it's better to keep the .q file and delete the .x file.

      Thanks.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Run 2 Commands, Keep Smallest File
      « Reply #3 on: December 09, 2014, 03:59:57 AM »
      Test this on some sample files.


      Code: [Select]
      @echo off
      for /r DIR_1 %%a in (*) do (
         dime -D CO=Q -L %%~dpa "%%a" "%%a.q"
         make -D CO=X -L %%~dpa "%%a" "%%a.x"
            for %%b in ("%%a.q") do for %%c in ("%%a.x") do if %%~zc LEQ %%~zb (del "%%a.x") else (del "%%a.q")
         )
      pause

      gmgdr11

        Topic Starter


        Rookie

        • Experience: Experienced
        • OS: Windows 7
        Re: Run 2 Commands, Keep Smallest File
        « Reply #4 on: December 09, 2014, 05:11:01 AM »
        Test this on some sample files.


        Code: [Select]
        @echo off
        for /r DIR_1 %%a in (*) do (
           dime -D CO=Q -L %%~dpa "%%a" "%%a.q"
           make -D CO=X -L %%~dpa "%%a" "%%a.x"
              for %%b in ("%%a.q") do for %%c in ("%%a.x") do if %%~zc LEQ %%~zb (del "%%a.x") else (del "%%a.q")
           )
        pause

        Thanks! Always appreciate the help. :)

        gmgdr11

          Topic Starter


          Rookie

          • Experience: Experienced
          • OS: Windows 7
          Re: Run 2 Commands, Keep Smallest File
          « Reply #5 on: December 10, 2014, 07:02:33 PM »
          Test this on some sample files.


          Code: [Select]
          @echo off
          for /r DIR_1 %%a in (*) do (
             dime -D CO=Q -L %%~dpa "%%a" "%%a.q"
             make -D CO=X -L %%~dpa "%%a" "%%a.x"
                for %%b in ("%%a.q") do for %%c in ("%%a.x") do if %%~zc LEQ %%~zb (del "%%a.x") else (del "%%a.q")
             )
          pause

          Quick follow-up question:

          If I wanted to add a third command that would produce a ".z" file, how would I go about adding it in the comparison portion of the script? In other words, what would I need to add to compare 3 files and keep the smallest and delete the 2 larger files?

          The extra command would be identical to the other two except the file extension would be .z, like so:

          Code: [Select]
             make -D CO=X -L %%~dpa "%%a" "%%a.z"
          I'm hoping it's easy so I can, if need be, add more commands and not need to bother you guys with this one again.

          Any help would be great.

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: Run 2 Commands, Keep Smallest File
          « Reply #6 on: December 10, 2014, 11:33:45 PM »
          It's not totally straight forward to add one, two, or more commands, with tests.


          Batch files are frequently written for a single task and when the task changes, then the batch file can be very different.

          If you have a task to solve and want a script (rather than just a tip on how to use a technique)
          then always describe the full task -  never ask for a simplified version and add things later.

          The reason is that the volunteers find little enjoyment in writing a script, and then having to change it because the task wasn't described accurately.


          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8