# Solved: batch file help



## lennep (Jul 19, 2007)

Hi,

We require some help refining a batch file to compress large numbers of medical image files using a utility called "dcmcjpeg.exe".

We have created a batch file into which we drag and drop the images files which are then compressed and overwrite the originals.At present, the images are stored in multiple sudirectories and must be located with a manual search. The "+ee" parameter shown below is the type of compression used.

To overcome the batch file's limitation to only handle parameters up to %9, we use the SHIFT command and repeat the original command many times over, making for a long and cumbersome batch file with limitations on the number of images than can be processed due to command line length limitations.

The batch file and dcmcjpeg.exe reside in a C: drive folder called "compress":

_[cd\compress
dcmcjpeg +ee %1 %1
SHIFT
dcmcjpeg +ee %1 %1
SHIFT
dcmcjpeg +ee %1 %1
SHIFT
dcmcjpeg +ee %1 %1
SHIFT/I] etc.etc.

Is there a more elegant way of looping the original command and allowing drag and drop of the parent folder with processing of images in all subdirectories?

Thanks in advance._


----------



## Squashman (Apr 4, 2003)

You would be better of using a For Loop to parse the directory and sub directories instead of dragging and dropping the files on there. I am kind of surprised that it will let you overwrite a file in use. That doesn't make a whole lot of sense.

I will whip up a batch file tonight when I get some free time. I will direct a few other guys over here as well. TheOutCaste is pretty darn good at this stuff.


----------



## lennep (Jul 19, 2007)

Thanks for the prompt response Squashman. 

We are medicos, not programmers and hence the crude nature of this effort. We initially renamed all compressed files but when I tried "%1 %1" in the command line, yes, it does overwrite the file. This is handy because we then do have to delete the original image files.

We would appreciate any rewrite of this batch file that allows for more automated importing and processing, but drag and drop of the parent folder rather than the contained subdirectory files into the batch file would still work OK for us. We are aware of For Loop but are not clear on its correct implementation.


----------



## TheOutcaste (Aug 8, 2007)

This is a better way of writing your original as you don't have to guess at how many files are dropped. You are still limited to the 8191 character limit, which includes the batch file name.

```
@Echo Off
:_Process
If [%1]==[] Goto :_Done
dcmcjpeg +ee %1 %1
SHIFT
Goto _Process
:_Done
PopD
[COLOR=Blue]Pause[/COLOR]
```
You can remove the *Pause* statement to have it automatically close when finished
This file will let you drop a folder onto the batch file icon (or a shortcut to the icon you can place on the desktop) and will process all files in that folder and it's subfolders:
You can adjust the width and height of the Command Prompt window by adjusting the numbers in the 6th line, or remove it to use the defaults. I set it to widen the window a bit so longer paths will be visible in the title bar.
You can remove the *Pause* statement in line 11 to have it automatically close when finished, but leave the last Pause statement in.

```
@Echo Off
If [%1]==[] Goto _Usage
If Not Exist "%~1" Goto _Usage2
If Not Exist %~s1\nul Goto _Usage1
Set _Source=%1
Mode CON: Cols=100 Lines=30
Title Processing %_Source% and it's subfolders
PushD "%~dp0"
For /F "Tokens=1 Delims=" %%I In ('Dir /A-D /B /S %_Source%') Do dcmcjpeg +ee "%%I" "%%I"
PopD
Pause
Goto :EOF
:_Usage2
Echo Error: %1 does not exist.
Goto _Usage
:_Usage1
Echo Parameter Error: %1 is a file name.
Echo                : Please specify a folder name
:_Usage
Echo.
Echo %~n0 [drive:][path]foldername
Echo.
Echo.  foldername  : Name of the folder to process.
Echo.              : It must be quoted if it contains spaces and is entered
Echo.              : on the Command Line. If dropped on the batch file or it's
Echo.              : shortcut, it will be quoted automatically
Echo.
Pause
```
HTH

Jerry


----------



## Squashman (Apr 4, 2003)

Keeping it simple. That is awesome Jerry.


----------



## lennep (Jul 19, 2007)

Hi TheOutcaste,

Thanks for tthis. Clearly your solution is way beyond anything we could have conceived. I am away for a couple of days but will test the new files as soon a possible upon my return.

Once again, thanks for the incredibly fast responses from yourself and Squashman..


----------



## TheOutcaste (Aug 8, 2007)

You're welcome, let us know how it goes.


----------



## lennep (Jul 19, 2007)

Thanks guys, the file posted by Outcaste works at treat for both folders and subfolders.

lennep


----------



## lennep (Jul 19, 2007)

Hi Outcaste,

Wondering if I could get a little more help? We now have a very functional system in operation based on your batch file, and again, thanks for your help. 

We do, however, now have a problem with some vendor images rendered unreadable with compression type "+ee". These are readable with a less efficicient compression type "+e1". 

As the files affected are less than 1.5mb, can we do a modification that requests that files less than 1.5mb are compressed by "dcmcjpeg +e1" and those larger than 1.5mb are compressed by "dcmcjpeg +e1" ?

Regards,

lennep


----------



## TheOutcaste (Aug 8, 2007)

lennep said:


> files less than 1.5mb are compressed by "dcmcjpeg *+e1*" and those larger than 1.5mb are compressed by "dcmcjpeg *+e1*"


I'm assuming the *+e1* should be *+ee*
So files 1.5 MiB (1,572,864 bytes) or larger will use *+ee* and files less than 1.5 MiB will use *+e1*.
Just need to add a check for file size in the *For* loop in line 9.
It will now cover lines 9 through 14, pushing the *PopD* statement that was on line 10 to line 15:

```
For /F "Tokens=1 Delims=" %%I In ('Dir /A-D /B /S %_Source%') Do (
If %%~zI GEQ 1572864 (
dcmcjpeg +ee "%%I" "%%I"
) Else (
dcmcjpeg +e1 "%%I" "%%I"
))
```
Give that a try.

Jerry


----------



## lennep (Jul 19, 2007)

Hi Jerry,

Worked perfectly!! 

Just to fill you in on what we have achieved in improving xray image transfer from remote rural locations in the Australian outback for reporting. 

1.We start with approx. 500 MB of dicom images files and a bunch of request forms in .pdf format

2.With a single click, a series of commands in batch files compress the dicom images, deletes junk files, compresses the lot to .rar file (final size 80MB with +ee, +e1) and deletes the original folder of images

3..rar file transfered by YouSendIt in about 40 minutes

4.Downloaded from our homes for reporting in 15 minutes. 

5.A single click on a batch file then decompresses the .rar file, copies .pdf's to a defined folder, imports the dicom and opens an image viewing program and deletes the.rar and all decompressed files, leaving an empty directory for the next run.

We couldn't be more pleased with the results and thank you for your assitance in this project.

Cheers,

Ruben


----------



## TheOutcaste (Aug 8, 2007)

You're welcome Ruben.

Glad to hear it's working so well.

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

