Computer Hope

Microsoft => Microsoft DOS => Topic started by: gmgdr11 on December 08, 2014, 06:00:15 PM

Title: Run 2 Commands, Keep Smallest File
Post by: gmgdr11 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.
Title: Re: Run 2 Commands, Keep Smallest File
Post by: foxidrive on December 09, 2014, 01:36:58 AM
The q and x files could be the same size.  What should happen if that occurs?
Title: Re: Run 2 Commands, Keep Smallest File
Post by: gmgdr11 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.
Title: Re: Run 2 Commands, Keep Smallest File
Post by: foxidrive 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
Title: Re: Run 2 Commands, Keep Smallest File
Post by: gmgdr11 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. :)
Title: Re: Run 2 Commands, Keep Smallest File
Post by: gmgdr11 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.
Title: Re: Run 2 Commands, Keep Smallest File
Post by: foxidrive 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.

Title: Re: Run 2 Commands, Keep Smallest File
Post by: foxidrive on December 11, 2014, 06:08:18 AM
http://stackoverflow.com/questions/27422598/this-batch-script-wont-work-why