1

I am collecting all my family albums in a main one with hundreds of subfolders that inside have hundreds of images. How I can convert all this galleries without to copy in each subfolder the bat file and run it in every folder?

I have more than 1k of galleries, please help!

This is the command what I tried to write:

@ECHO OFF  FOR /f "delims=*" %a IN ('dir *.jpg /b /s') do REN "%a" *.png

But insn't work and I don't know what I'm doing wrong..

PS: The folder and subfolders are still in generic location, I don't have a fixed one because I always move the files from a disk to others. I'm using Windows 8.1.

Warmuser
  • 21
  • 4
  • 1
    renaming them doesn't actually convert them from one image type to the other, I think you should look at that first. – Dennis van Gils Jan 28 '16 at 22:00
  • Batch files require %%A instead of %A when typing at command prompt. –  Jan 28 '16 at 22:03
  • @Dennis van Gils: how can I convert them all with the same command? – Warmuser Jan 28 '16 at 22:07
  • 1
    Why would you want to convert JPEG photos to PNG format? That is not a good idea generally as JPEG offers better compression (smaller filesize) for photographs whereas PNG is generally better for computer graphics. – Mark Setchell Jan 28 '16 at 22:26
  • @MarkSetchell although it is [questionably](http://stackoverflow.com/a/7752936/5022761) good advice to use JPEG over PNG, this doesn't add to the question. There's a good chance a png is required by some external source, in which case it is necessary to convert the files. – Dennis van Gils Jan 28 '16 at 22:34
  • @Mark Setchell I know, but JPEG lose much details much rapidly.. Anyway I can use the TIFF or RAW format, what I think are much better. – Warmuser Jan 28 '16 at 22:38
  • @DennisvanGils Sorry, I disagree. There is no indication in the question that an external source is involved so I fail to see how the *"chances can be good"* that there are any. I also fail to see how PNG can be better than JPEG for photographic material. And finally, my reply was merely a simple comment, not a full-blown answer, so it doesn't need to *"add anything to the question"*. – Mark Setchell Jan 28 '16 at 22:45
  • Possible duplicate of [Batch command for ImageMagick to convert all files in a directory and sub-direcotries on windows](http://stackoverflow.com/questions/30414346/batch-command-for-imagemagick-to-convert-all-files-in-a-directory-and-sub-direco) –  Jan 29 '16 at 00:04
  • Updated my answer with GraphicsMagick example. – Dennis van Gils Jan 29 '16 at 14:16

1 Answers1

2

The problem with your code is, as @bgalea pointed out, that batch uses %%a instead of %a.

However, while this fixes the syntax of your code, there is still an error. Renaming a file to a different extension doesn't actually convert the file, for example you can't rename a .txt file to .mp4 and expect it to play a video.

Unfortunately, windows has no native way to convert images. This means that you'll need a 3rd party tool, for example ImageMagick (free). If you have ImageMagick installed, you can use the convert command, like this:

@ECHO OFF  
FOR /f "delims=*" %%a IN ('dir *.jpg /b /s') do convert "%%a" "%%~dpna.png"

To delete the files after conversion:

@ECHO OFF  
FOR /f "delims=*" %%a IN ('dir *.jpg /b /s') do (
convert "%%a" "%%~dpna.png"
del "%%a"
)

EDIT

Ok, so after trying to get ImageMagick to work I looked for an alternative, and found GraphicsMagick, which is available here. After Installing this, I updated the script, and this time it worked.

@ECHO OFF  
FOR /f "delims=*" %%a IN ('dir *.jpg /b /s') do (
"C:\Program Files (x86)\GraphicsMagick-1.3.23-Q16\gm.exe" convert "%%a" "%%~dpna.png"
del "%%a"
)

Please note that you should make sure your gm.exe is located at the same path as the one given above, if not you should edit the path in the script.

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • Thanks, I have photoshop. But is possible to convert by using the existing file, without to generate a converted copy ? I don't have enough space. – Warmuser Jan 28 '16 at 22:34
  • @Warmuser As far as I'm aware, photoshop [doesn't really work from the commandline](http://superuser.com/a/78667). Also, I edited the answer to delete the files after conversion. – Dennis van Gils Jan 28 '16 at 22:40
  • Works with Paint.NET? PS: I have an SSD, so is much better for me If don't make many copies of the files (my pics archive is around 55GB of reflex photos) there's no way to convert without to clone the file? thanks – Warmuser Jan 28 '16 at 22:43
  • Photoshop has a built-in Batch Processor that can do this for you... https://helpx.adobe.com/photoshop/using/processing-batch-files.html – Mark Setchell Jan 28 '16 at 22:47
  • @Warmuser no, paint.net also [doesn't work from commandline](http://forums.getpaint.net/index.php?/topic/18804-applying-paintnet-function-using-a-command-line/). It seems that ImageMagick is one of the few free programs that does this. – Dennis van Gils Jan 28 '16 at 22:47
  • @MarkSetchell it seems the photoshop built-in batch processor is the best option in this case, however I can't help with that any further as I don't have photoshop nor have any knowledge on it. – Dennis van Gils Jan 28 '16 at 22:50
  • @Mark Setchell the problem of the Adobe Batch Processor is that I have to select every subfolder manually. So it becomes an eternal work.. – Warmuser Jan 28 '16 at 22:53
  • If you are processing tens of thousands of files with ImageMagick, by the way, you would be well advised to consider `mogrify` over `convert` since it is expressly designed for that very purpose and avoids the overhead of having to create a new process and and read all ImageMagick's settings and font configuration files for every single one of tens of thousands of images. – Mark Setchell Jan 28 '16 at 22:55
  • Search for the word *"nested"* on the page I linked - you can recurse down into subdirectories. – Mark Setchell Jan 28 '16 at 22:57
  • @MarkSetchell as far as I know, both `mogrify` and `convert` are part of ImageMagick. They are seperate programs installed as one big program, called ImageMagick. – Dennis van Gils Jan 28 '16 at 23:00
  • @Dennis van Gils: I have downloaded the ImageMagick program. So It works automatically when I open the bat file or I have to set something? – Warmuser Jan 28 '16 at 23:08
  • The batch-file should be in the folder where all the other directories with images are. If that's the case it should work as soon as you open the batch-file. Please be carefull and try it on a test-folder first to see if it works, as batch is quite a dangerous language. – Dennis van Gils Jan 28 '16 at 23:09
  • @Dennis van Gils: the first commant doesn't convert the pics, the second only delete the test photos. – Warmuser Jan 28 '16 at 23:15
  • @Warmuser Could you find the folder of ImageMagick and locate the `convert.exe` file (this can also be any other executable file called convert) and comment the path to that file? – Dennis van Gils Jan 28 '16 at 23:19
  • @Warmuser also try to execute `convert -version`. If this gives you "Invalid drive specification" as output you didn't install ImageMagick correctly, or it can't be found – Dennis van Gils Jan 28 '16 at 23:22
  • why do you delete the source file? That's rather dumb - isn't it? – DATEx2 Jun 07 '23 at 18:51