Home > Software design >  Issue with quotations inside quotations in cmd batch script
Issue with quotations inside quotations in cmd batch script

Time:05-18

This is my first post here. I only have some very basic knowledge of batch scripts.

I have a D:\SOURCE directory containining subdirectories with images. I am using a command line program (Irfanview) to resize and convert images to a different format. I use a FOR /D loop to go down all the folders in D:\SOURCE and store the whole path to the subfolder in %d, then an IF loop to ignore folders which already exist at the destination.

This is my code:

FOR /D %%d IN ("D:\SOURCE\*") DO ( 

if NOT exist D:\DESTINATION\%%~nd (
"%ProgramFiles%\IrfanView\i_view64.exe %%d\*.tif /resize=(50p,50p) /dpi=(150,150) /resample /convert=D:\DESTINATION\%%~nd\*.jpg"
)
)

The issue here is the space in "C:\Program Files". That path doesn't work unless it's enclosed between quotation marks. On the other hand, the whole sentence needs to be enclosed in quotation marks, otherwise %d and %~nd will not work. So I would need to enclose the path to the .exe in quotation marks within the whole quotation-marked string. I have read a lot of posts here addressing similar issues but none of the solutions worked for me. Neither ", nor ^", "", """, nor declaring "C:\Program Files\IrfanView\i_view64.exe" as a variable.

This must be a very common situation so I'm sure there must be a very basic solution that I am overlooking. Or maybe my whole approach is wrong and there's another obvious way to do it. I appreciate all the replies.

CodePudding user response:

You want to partially quote the arguments, a good program should understand quoted arguments.

FOR /D %%d IN ("D:\SOURCE\*") DO ( 

  if NOT exist D:\DESTINATION\%%~nd (
    "%ProgramFiles%\IrfanView\i_view64.exe" "%%d\*.tif" ^
        /resize="(50p,50p)" ^
        /dpi="(150,150)" ^
        /resample ^
        /convert="D:\DESTINATION\%%~nd\*.jpg"
  )
)
  • Related