PDA

View Full Version : Irfanize.cmd



Gownaar
10.07.2014, 05:09 PM
Here is a batch script for processing files with IV.
I did my best to make it versatile and easily configurable.


@Echo off & SetLocal EnableDelayedExpansion

::: Irfanize.cmd
::: A script for processing images with IrfanView
::: Usage: drag&drop a file or folder onto the script
::: or run the script in a folder that you want processed
::: or specify a file/folder on the command line
::: Adjust options below to make script do what you need

:::::::::::::::::::::::::::: Edit this part ::::::::::::::::::::::::::::

Set IrfanExe=
::: Leave blank if IV is installed in default location
::: (64-bit takes precedence if both versions are installed)
::: Otherwise specify full path to executable, without quotes
::: e.g. IrfanExe=D:\My Progs\IrfanView\i_view32.exe

Set IniDir=
::: Leave blank to use the default configuration file
::: Otherwise specify a folder that contains i_view32/i_view64.ini
::: e.g. IniDir=%~dp0 if .ini is in script folder

Set InputExt=
::: Specify one or more input file types separated by commas or spaces
::: e.g. InputExt=jpg,png,gif
::: Leave blank to accept all file types
::: InputExt is only used when processing folders

Set Recursive=No
::: Yes=process subfolders

Set ActionParams=
::: Specify what to do with images; see IV help->Command Line Options
::: e.g. ActionParams=/crop=(0,0,128,128,0) /vflip /invert
::: Leave blank if you are simply looking to convert between formats

Set OutputExt=
::: Specify output format, e.g. OutputExt=png
::: Leave blank to keep the source file type

Set OutputSuffix=
::: You can append a string to output filename
::: e.g. if OutputSuffix=_new, then img.jpg->img_new.jpg

Set OutputAutoRename=No
::: Yes=autorename output if file already exists, e.g. img_new(1).jpg
::: Note: utilizes /makecopy option introduced in IV 4.38

Set OutputDir=
::: If you specify a name, e.g. OutputDir=IVOutput
::: output will be saved to that subfolder (in source folder)
::: You can also specify an absolute path, e.g. OutputDir=D:\Pics
::: Leave blank to save output to the source folder (not recommended)

Set OverwriteInput=No
::: Yes=output will be same as input, i.e. replace source file(s)
::: (overrides OutputExt, OutputSuffix, OutputAutoRename, OutputDir)

Set DeleteInput=No
::: Yes=delete source file(s) after processing
::: Not deleted if OverwriteInput=Yes or if processing failed
::: In folder mode this option is only allowed if InputExt is specified

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

If not %1.==. If not exist "%~f1" Echo Invalid path specified. & Goto End

If not "%IrfanExe%"=="" (If not exist "%IrfanExe%" (Echo Invalid IrfanView executable specified. & Goto End)) Else (
If exist "%ProgramFiles%\IrfanView\i_view64.exe" (Set "IrfanExe=%ProgramFiles%\IrfanView\i_view64.exe") Else (
If exist "%ProgramW6432%\IrfanView\i_view64.exe" (Set "IrfanExe=%ProgramW6432%\IrfanView\i_view64.exe") Else (
If exist "%ProgramFiles%\IrfanView\i_view32.exe" (Set "IrfanExe=%ProgramFiles%\IrfanView\i_view32.exe") Else (
If exist "%ProgramFiles(x86)%\IrfanView\i_view32.exe" (Set "IrfanExe=%ProgramFiles(x86)%\IrfanView\i_view32.ex e") Else (
Echo IrfanView executable could not be located. & Goto End)))))

Set K=!IrfanExe:~-12,8!

If not "%IniDir%"=="" (
If "%K%"=="i_view64" (
If not exist "%IniDir%\i_view64.ini" (Echo i_view64.ini is missing in specified dir. & Goto End)) Else (
If "%K%"=="i_view32" (
If not exist "%IniDir%\i_view32.ini" (Echo i_view32.ini is missing in specified dir. & Goto End)))
Set "IniDir=/ini=^"%IniDir%^""
)

If /I "%OverwriteInput%"=="Yes" (
Set OutputExt=
Set OutputSuffix=
Set OutputDir=
) Else (
If "%OutputExt%"=="" (If "%OutputSuffix%"=="" (If "%OutputDir%"=="" (
Echo The script isn't set up to output anything. See usage info inside. & Goto End)))
If /I "%OutputAutoRename%"=="Yes" Set "ActionParams=%ActionParams% /makecopy"
If not "%OutputDir%"=="" Set "OutputDir=%OutputDir%\"
)

If "%K%"=="i_view64" (Echo IrfanView ^(64-bit^)) Else (
If "%K%"=="i_view32" (Echo IrfanView ^(32-bit^)))

If %1.==. Goto FolderMode
If exist "%~f1\*.*" Goto FolderMode

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

Echo(
Echo Processing %~nx1...

If "%OutputExt%"=="" (Set OutputExt=%~x1) Else (Set OutputExt=.%OutputExt%)

If /I "%OverwriteInput%"=="Yes" (
"%IrfanExe%" ""%~f1" %IniDir% %ActionParams% /convert=%~f1 /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!)
)
) Else (
If /I not "%~f1"=="%OutputDir%%~n1%OutputSuffix%!OutputExt%!" (
If /I not "%~f1"=="%~dp1%OutputDir%%~n1%OutputSuffix%!OutputExt%!" (
"%IrfanExe%" ""%~f1" %IniDir% %ActionParams% /convert=%OutputDir%%~n1%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!) Else (
If /I "%DeleteInput%"=="Yes" (
CD /D "%~dp1"
If exist "%OutputDir%%~n1%OutputSuffix%!OutputExt%!" (
Del /F /Q "%~f1"
)
)
)
)
)
)
)

Echo(
Echo Finished.
Goto End

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:FolderMode

If %1.==. (Set "SrcDir=%~dp0") Else (Set "SrcDir=%~f1\")

If not "%InputExt%"=="" (
Set I=
Set L=
If /I "%Recursive%"=="Yes" (Echo Scanning for files with specified extension^(s^)...)
For %%G in (%InputExt%) Do (
Set I=!I!*.%%G,
If exist "%SrcDir%*.%%G" (Set L=found) Else (
If /I "%Recursive%"=="Yes" (
CD /D "%SrcDir%"
For /D /R %%J in ("*") Do (If exist "%%J\*.%%G" Set L=found)
)
)
)
If not "!L!"=="found" Echo No files with specified extension^(s^) found. & Goto End
Set InputExt=!I:~0,-1!
) Else (Set InputExt=*.*)

If not "%OutputExt%"=="" Set OutputExt=.%OutputExt%

Echo(
Echo Processing...
Echo(
Echo %SrcDir%

Set K=1
:SrcDirDepth
Set J=%K%
For /F "tokens=%K% delims=\" %%G in ("%SrcDir%") Do (Set /A K+=1)
If not "%J%"=="%K%" Goto SrcDirDepth
Set /A K-=1

Set FileCount=0
Set ErrorCount=0

If /I "%Recursive%"=="Yes" (
Set "L=%SrcDir%"
For /R "%SrcDir%" %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
If /I not "%%~dpG"=="%OutputDir%" (
Set "H=%%~pG"
Set "H=!H:~0,-1!"
For %%I in ("!H!") Do (Set H=%%~nxI)
Set "H=!H!\"
If /I not "!H!"=="%OutputDir%" (
If /I not "!L!"=="%%~dpG" (
For /F "tokens=%K%* delims=\" %%I in ("%%~dpG") Do (
Echo(
Echo %%J
)
)
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG

If /I "%OverwriteInput%"=="Yes" (
"%IrfanExe%" ""%%~fG" %IniDir% %ActionParams% /convert=%%~fG /silent"
If Errorlevel 2 (Echo Error saving output^^! & Set /A ErrorCount+=1) Else (
If Errorlevel 1 (Echo Error loading file^^! & Set /A ErrorCount+=1) Else (
Set /A FileCount+=1
)
)
) Else (
If /I not "%%~fG"=="%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
If /I not "%%~fG"=="%%~dpG%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
"%IrfanExe%" ""%%~fG" %IniDir% %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^! & Set /A ErrorCount+=1) Else (
If Errorlevel 1 (Echo Error loading file^^! & Set /A ErrorCount+=1) Else (
Set /A FileCount+=1
If /I "%DeleteInput%"=="Yes" (
If not "%InputExt%"=="*.*" (
CD /D "%%~dpG"
If exist "%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
Del /F /Q "%%~fG"
)
)
)
)
)
)
)
)
Set "L=%%~dpG"
)
)
)
)
) Else (
CD /D "%SrcDir%"
For %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG

If /I "%OverwriteInput%"=="Yes" (
"%IrfanExe%" ""%%~fG" %IniDir% %ActionParams% /convert=%%~fG /silent"
If Errorlevel 2 (Echo Error saving output^^! & Set /A ErrorCount+=1) Else (
If Errorlevel 1 (Echo Error loading file^^! & Set /A ErrorCount+=1) Else (
Set /A FileCount+=1
)
)
) Else (
If /I not "%%~fG"=="%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
If /I not "%%~fG"=="%%~dpG%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
"%IrfanExe%" ""%%~fG" %IniDir% %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^! & Set /A ErrorCount+=1) Else (
If Errorlevel 1 (Echo Error loading file^^! & Set /A ErrorCount+=1) Else (
Set /A FileCount+=1
If /I "%DeleteInput%"=="Yes" (
If not "%InputExt%"=="*.*" (
CD /D "%%~dpG"
If exist "%OutputDir%%%~nG%OutputSuffix%!OutputExt%!" (
Del /F /Q "%%~fG"
)
)
)
)
)
)
)
)
)
)
)

Echo(
Echo Finished.
Echo(
Echo %FileCount% files successfully processed.
Echo %ErrorCount% errors.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:End
Echo(
Pause
Echo(

tennessee
24.07.2014, 01:15 PM
I may be completely overlooking it, and if so, I apologize. But where do you specify the input/source folder path?

tennessee
24.07.2014, 02:05 PM
This script would be perfect for my needs, but I can't seem to get it working. Here is what I've done.

1. Copied the original code from above and saved as Irfanize.cmd file INSIDE THE SOURCE FOLDER of the directory I want to irfanize.
2. Edited only the settings as follows:

Set IrfanDir=
Set InputExt=jpg
Set Recursive=No
Set ActionParams=/resize_long=2000 /dpi=(300,300) /aspectratio
Set OutputExt=
Set OutputSuffix=
Set OutputAutoRename=No
Set OutputDir=E:\Temp only\Image Sizing Tests\Sized\2500
Set OverwriteInput=No

3. Double clicked the Irfanize.cmd file. [When I do this, the command window pops up and disappears instantly..producing no results]
4. Since step 3 didn't work, I alternatively opened a blank command window from the image source folder and simply pasted the contents of the infanize.cmd file. This produced no results either. I'll paste the resulting code here:



E:\Temp only\Image Sizing Tests\Full Resolution Master>@Echo off & SetLocal Enab
leDelayedExpansion

::: Irfanize.cmd
::: A script for processing images with IrfanView.
::: Can process a single file or all images in a folder.
::: Run from a folder that you want processed or specify a
::: file/folder on the command line / drag&drop onto the script.

:::::::::::::::::::::::::::: Edit this part ::::::::::::::::::::::::::::


Set IrfanDir=
::: Specify IrfanView location.
::: Leave blank if IV is installed in default location.

Set InputExt=jpg
::: Specify one or more input file types separated by commas or spaces.
::: e.g. InputExt=jpg,png,gif
::: Leave blank to accept all file types.
::: InputExt is only used when processing folders.

Set Recursive=No
::: Yes=process subfolders

Set ActionParams=/resize_long=2000 /dpi=(300,300) /aspectratio
::: Specify what to do with images; see IV help->Command Line Options.
::: e.g. ActionParams=/crop=(0,0,128,128,0) /vflip /invert
::: Leave blank if you are simply looking to convert between formats.

Set OutputExt=
::: Specify output format, e.g. OutputExt=png
::: Leave blank to keep the source file type.

Set OutputSuffix=
::: e.g. if OutputSuffix=_new, then img.jpg->img_new.jpg
::: Leave blank to omit the suffix, e.g. img.jpg->img.gif

Set OutputAutoRename=No
::: Yes=auto rename output if file already exists, e.g. img_new(1).jpg
::: Note: utilizes /makecopy option, which was introduced in IV 4.38

Set OutputDir=E:\Temp only\Image Sizing Tests\Sized\2500
::: Leave blank to save output to the same folder.
::: If you specify a name, e.g. OutputDir=ConvertedPics
::: output will be saved to that subfolder (of each folder).
::: You can also specify a fixed path, e.g. OutputDir=D:\Pics

Set OverwriteInput=No
::: Yes=overwrite source file(s)
::: (overrides OutputExt, OutputSuffix, OutputAutoRename, OutputDir)
::: Caution: even with OverwriteInput=No, the wrong combination of
::: above parameters can result in original files being replaced.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

If not %1.==. If not exist "%~f1" Echo Invalid path specified. & Goto End
Invalid path specified.

If not "%IrfanDir%"=="" (If not exist "%IrfanDir%\i_view32.exe" Echo Invalid
More?
More? IrfanView dir specified. & Goto End) Else (
More? If exist "%ProgramFiles%\IrfanView\i_view32.exe" (Set IrfanDir=%ProgramFil
es%
More?
More? \IrfanView) Else (
More? If exist "%ProgramFiles(x86)%\IrfanView\i_view32.exe" (Set IrfanDir=
More?
More? %ProgramFiles(x86)%\IrfanView) Else (
\IrfanView was unexpected at this time.
Echo IrfanView could not be located. & Goto End)))
IrfanView could not be located.

If /I "%OverwriteInput%"=="Yes" (
More? Set OutputExt=
More? Set OutputSuffix=
More? Set OutputDir=
More? ) Else (
More? If /I "%OutputAutoRename%"=="Yes" Set ActionParams=%ActionParams%
/aspectratio was unexpected at this time.

/makecopy
'/makecopy' is not recognized as an internal or external command,
operable program or batch file.
If not "%OutputDir%"=="" Set OutputDir=%OutputDir%\
)

If %1.==. Goto FolderMode
If exist "%~f1\*.*" Goto FolderMode

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

Echo(

Echo Processing %~nx1...
Processing %~nx1...

If "%OutputExt%"=="" (Set OutputExt=%~x1) Else (Set OutputExt=.%OutputExt%)

"%IrfanDir%\i_view32.exe" ""%~f1" %ActionParams% /convert=%OutputDir%
The system cannot find the path specified.

%~n1%OutputSuffix%%OutputExt% /silent"
'%~n1%OutputSuffix%.%OutputExt%' is not recognized as an internal or external co
mmand,
operable program or batch file.
If Errorlevel 2 (Echo Error saving output^^!) Else (
More? If Errorlevel 1 (Echo Error loading file^^!))
Error saving output^!

Echo(

Echo Finished.
Finished.
Goto End

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:FolderMode

If %1.==. (Set SrcDir=%~dp0) Else (Set SrcDir=%~f1\)

If not "%InputExt%"=="" (
More? Set I=
More? Set L=
More? For %%G in (%InputExt%) Do (
%%G was unexpected at this time.
Set I=!I!*.%%G,
If /I "%Recursive%"=="Yes" (
More? CD /D %SrcDir%
More? For /D /R %%J in ("*") Do (If exist "%%J\*.%%G" Set L=found)
%%J was unexpected at this time.
) Else (If exist "%SrcDir%*.%%G" Set L=found)
)
If not "!L!"=="found" Echo No files with specified extension^(s^) found. &
No files with specified extension(s) found.

Goto End
Set InputExt=!I:~0,-1!
) Else (Set InputExt=*.*)

If not "%OutputExt%"=="" Set OutputExt=.%OutputExt%

Echo(

Echo Processing...
Processing...
Echo(

Echo %SrcDir%
%~f1\

Set K=1
:SrcDirDepth
Set J=%K%
For /F "tokens=%K% delims=\" %%G in ("%SrcDir%") Do (Set /A K+=1)
%%G was unexpected at this time.
If not "%J%"=="%K%" Goto SrcDirDepth
Set /A K-=1
0
If /I "%Recursive%"=="Yes" (
More? Set L=%SrcDir%
More? For /R "%SrcDir%" %%G in (%InputExt%) Do (
%%G was unexpected at this time.
If not "%%~fG"=="%~f0" (
More? If not "%%~dpG"=="%OutputDir%" (
More? Set H=%%~pG
More? Set H=!H:~0,-1!
More? For %%I in ("!H!") Do (Set H=%%~nxI)
%%I was unexpected at this time.
Set H=!H!\
If not "!H!"=="%OutputDir%" (
More? If not "!L!"=="%%~dpG" (
More? For /F "tokens=%K%* delims=\" %%I in ("%%~dpG") Do (
%%I was unexpected at this time.
Echo(

Echo %%J
%%J
)
)
Echo(

Echo %%~nxG
%%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG
"%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=
The system cannot find the path specified.

%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
'E:\Temp' is not recognized as an internal or external command,
operable program or batch file.
If Errorlevel 2 (Echo Error saving output^^!) Else (
More? If Errorlevel 1 (Echo Error loading file^^!))
Error saving output^!
Set L=%%~dpG
)
)
)
)
) Else (
CD /D %SrcDir%
The system cannot find the path specified.
For %%G in (%InputExt%) Do (
%%G was unexpected at this time.
If not "%%~fG"=="%~f0" (
More? Echo(
More? Echo %%~nxG
More? If "%OutputExt%"=="" Set OutputExt=%%~xG
More? "%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=
More?
More? %OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
More? If Errorlevel 2 (Echo Error saving output^^!) Else (
More? If Errorlevel 1 (Echo Error loading file^^!))
More? )

%%~nxG
The system cannot find the path specified.
'E:\Temp' is not recognized as an internal or external command,
operable program or batch file.
Error saving output^!
)
)

Echo(

Echo Finished.
Finished.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:End
Echo(

Pause
Press any key to continue . . .

Gownaar
24.07.2014, 07:41 PM
You can't do that (4.) Can you please attach your .cmd?

tennessee
24.07.2014, 09:20 PM
Certainly. Thanks for taking a look...here it is


@Echo off & SetLocal EnableDelayedExpansion

::: Irfanize.cmd
::: A script for processing images with IrfanView.
::: Can process a single file or all images in a folder.
::: Run from a folder that you want processed or specify a
::: file/folder on the command line / drag&drop onto the script.

:::::::::::::::::::::::::::: Edit this part ::::::::::::::::::::::::::::


Set IrfanDir=
::: Specify IrfanView location.
::: Leave blank if IV is installed in default location.

Set InputExt=jpg
::: Specify one or more input file types separated by commas or spaces.
::: e.g. InputExt=jpg,png,gif
::: Leave blank to accept all file types.
::: InputExt is only used when processing folders.

Set Recursive=No
::: Yes=process subfolders

Set ActionParams=/resize_long=2000 /dpi=(300,300) /aspectratio
::: Specify what to do with images; see IV help->Command Line Options.
::: e.g. ActionParams=/crop=(0,0,128,128,0) /vflip /invert
::: Leave blank if you are simply looking to convert between formats.

Set OutputExt=
::: Specify output format, e.g. OutputExt=png
::: Leave blank to keep the source file type.

Set OutputSuffix=
::: e.g. if OutputSuffix=_new, then img.jpg->img_new.jpg
::: Leave blank to omit the suffix, e.g. img.jpg->img.gif

Set OutputAutoRename=No
::: Yes=auto rename output if file already exists, e.g. img_new(1).jpg
::: Note: utilizes /makecopy option, which was introduced in IV 4.38

Set OutputDir="E:\Temp only\Image Sizing Tests\Sized\2500"
::: Leave blank to save output to the same folder.
::: If you specify a name, e.g. OutputDir=ConvertedPics
::: output will be saved to that subfolder (of each folder).
::: You can also specify a fixed path, e.g. OutputDir=D:\Pics

Set OverwriteInput=No
::: Yes=overwrite source file(s)
::: (overrides OutputExt, OutputSuffix, OutputAutoRename, OutputDir)
::: Caution: even with OverwriteInput=No, the wrong combination of
::: above parameters can result in original files being replaced.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

If not %1.==. If not exist "%~f1" Echo Invalid path specified. & Goto End

If not "%IrfanDir%"=="" (If not exist "%IrfanDir%\i_view32.exe" Echo Invalid IrfanView dir specified. & Goto End) Else (
If exist "%ProgramFiles%\IrfanView\i_view32.exe" (Set IrfanDir=%ProgramFiles%\IrfanView) Else (
If exist "%ProgramFiles(x86)%\IrfanView\i_view32.exe" (Set IrfanDir=%ProgramFiles(x86)%\IrfanView) Else (
Echo IrfanView could not be located. & Goto End)))

If /I "%OverwriteInput%"=="Yes" (
Set OutputExt=
Set OutputSuffix=
Set OutputDir=
) Else (
If /I "%OutputAutoRename%"=="Yes" Set ActionParams=%ActionParams% /makecopy
If not "%OutputDir%"=="" Set OutputDir=%OutputDir%\
)

If %1.==. Goto FolderMode
If exist "%~f1\*.*" Goto FolderMode

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

Echo(
Echo Processing %~nx1...

If "%OutputExt%"=="" (Set OutputExt=%~x1) Else (Set OutputExt=.%OutputExt%)

"%IrfanDir%\i_view32.exe" ""%~f1" %ActionParams% /convert=%OutputDir%%~n1%OutputSuffix%%OutputExt% /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))

Echo(
Echo Finished.
Goto End

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:FolderMode

If %1.==. (Set SrcDir=%~dp0) Else (Set SrcDir=%~f1\)

If not "%InputExt%"=="" (
Set I=
Set L=
For %%G in (%InputExt%) Do (
Set I=!I!*.%%G,
If /I "%Recursive%"=="Yes" (
CD /D %SrcDir%
For /D /R %%J in ("*") Do (If exist "%%J\*.%%G" Set L=found)
) Else (If exist "%SrcDir%*.%%G" Set L=found)
)
If not "!L!"=="found" Echo No files with specified extension^(s^) found. & Goto End
Set InputExt=!I:~0,-1!
) Else (Set InputExt=*.*)

If not "%OutputExt%"=="" Set OutputExt=.%OutputExt%

Echo(
Echo Processing...
Echo(
Echo %SrcDir%

Set K=1
:SrcDirDepth
Set J=%K%
For /F "tokens=%K% delims=\" %%G in ("%SrcDir%") Do (Set /A K+=1)
If not "%J%"=="%K%" Goto SrcDirDepth
Set /A K-=1

If /I "%Recursive%"=="Yes" (
Set L=%SrcDir%
For /R "%SrcDir%" %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
If not "%%~dpG"=="%OutputDir%" (
Set H=%%~pG
Set H=!H:~0,-1!
For %%I in ("!H!") Do (Set H=%%~nxI)
Set H=!H!\
If not "!H!"=="%OutputDir%" (
If not "!L!"=="%%~dpG" (
For /F "tokens=%K%* delims=\" %%I in ("%%~dpG") Do (
Echo(
Echo %%J
)
)
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG
"%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))
Set L=%%~dpG
)
)
)
)
) Else (
CD /D %SrcDir%
For %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG
"%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))
)
)
)

Echo(
Echo Finished.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:End
Echo(
Pause
Echo(

Gownaar
24.07.2014, 11:16 PM
Figured it out, the problem occurs when using parentheses inside an If/Else block; it's a limitation of the command processor, you triggered it with /dpi=(300,300). It should be fixed now. There's also one problem with your editing: do not quote paths (OutputDir).
You've made me realize that the script will also fail if folder names contain parentheses. I'll fix that some other time, too busy now, and I don't consider it a very critical problem anyway. Us old-school pre-NTFS raised dudes know better than to use parentheses in pathnames, it's asking for trouble. :)

tennessee
25.07.2014, 01:14 PM
Hmm. I must still be doing something incorrectly. I used the revised code (from the original post), edited it as noted above, and when I open the .cmd file from the jpg source folder, the command window appears and disappears instantly producing no results. Since the command window disappears so quickly, I don't have anything to evaluate for errors. What do you recommend?

Here's my new cmd file:

@Echo off & SetLocal EnableDelayedExpansion

::: Irfanize.cmd
::: A script for processing images with IrfanView.
::: Can process a single file or all images in a folder.
::: Run from a folder that you want processed or specify a
::: file/folder on the command line / drag&drop onto the script.

:::::::::::::::::::::::::::: Edit this part ::::::::::::::::::::::::::::

Set IrfanDir=
::: Specify IrfanView location.
::: Leave blank if IV is installed in default location.

Set InputExt=jpg
::: Specify one or more input file types separated by commas or spaces.
::: e.g. InputExt=jpg,png,gif
::: Leave blank to accept all file types.
::: InputExt is only used when processing folders.

Set Recursive=No
::: Yes=process subfolders

Set ActionParams=/resize_long=2000 /aspectratio
::: Specify what to do with images; see IV help->Command Line Options.
::: e.g. ActionParams=/crop=(0,0,128,128,0) /vflip /invert
::: Leave blank if you are simply looking to convert between formats.

Set OutputExt=
::: Specify output format, e.g. OutputExt=png
::: Leave blank to keep the source file type.

Set OutputSuffix=
::: e.g. if OutputSuffix=_new, then img.jpg->img_new.jpg
::: Leave blank to omit the suffix, e.g. img.jpg->img.gif

Set OutputAutoRename=No
::: Yes=auto rename output if file already exists, e.g. img_new(1).jpg
::: Note: utilizes /makecopy option, which was introduced in IV 4.38

Set OutputDir=E:\Temp only\Image Sizing Tests\Sized\2500
::: Leave blank to save output to the same folder.
::: If you specify a name, e.g. OutputDir=ConvertedPics
::: output will be saved to that subfolder (of each folder).
::: You can also specify a fixed path, e.g. OutputDir=D:\Pics

Set OverwriteInput=No
::: Yes=overwrite source file(s)
::: (overrides OutputExt, OutputSuffix, OutputAutoRename, OutputDir)
::: Caution: even with OverwriteInput=No, the wrong combination of
::: above parameters can result in original files being replaced.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

If not %1.==. If not exist "%~f1" Echo Invalid path specified. & Goto End

If not "%IrfanDir%"=="" (If not exist "%IrfanDir%\i_view32.exe" Echo Invalid IrfanView dir specified. & Goto End) Else (
If exist "%ProgramFiles%\IrfanView\i_view32.exe" (Set IrfanDir=%ProgramFiles%\IrfanView) Else (
If exist "%ProgramFiles(x86)%\IrfanView\i_view32.exe" (Set IrfanDir=%ProgramFiles(x86)%\IrfanView) Else (
Echo IrfanView could not be located. & Goto End)))

If /I "%OverwriteInput%"=="Yes" (
Set OutputExt=
Set OutputSuffix=
Set OutputDir=
) Else (
If /I "%OutputAutoRename%"=="Yes" Set "ActionParams=%ActionParams% /makecopy"
If not "%OutputDir%"=="" Set "OutputDir=%OutputDir%\"
)

If %1.==. Goto FolderMode
If exist "%~f1\*.*" Goto FolderMode

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

Echo(
Echo Processing %~nx1...

If "%OutputExt%"=="" (Set OutputExt=%~x1) Else (Set OutputExt=.%OutputExt%)

"%IrfanDir%\i_view32.exe" ""%~f1" %ActionParams% /convert=%OutputDir%%~n1%OutputSuffix%%OutputExt% /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))

Echo(
Echo Finished.
Goto End

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:FolderMode

If %1.==. (Set SrcDir=%~dp0) Else (Set SrcDir=%~f1\)

If not "%InputExt%"=="" (
Set I=
Set L=
For %%G in (%InputExt%) Do (
Set I=!I!*.%%G,
If /I "%Recursive%"=="Yes" (
CD /D %SrcDir%
For /D /R %%J in ("*") Do (If exist "%%J\*.%%G" Set L=found)
) Else (If exist "%SrcDir%*.%%G" Set L=found)
)
If not "!L!"=="found" Echo No files with specified extension^(s^) found. & Goto End
Set InputExt=!I:~0,-1!
) Else (Set InputExt=*.*)

If not "%OutputExt%"=="" Set OutputExt=.%OutputExt%

Echo(
Echo Processing...
Echo(
Echo %SrcDir%

Set K=1
:SrcDirDepth
Set J=%K%
For /F "tokens=%K% delims=\" %%G in ("%SrcDir%") Do (Set /A K+=1)
If not "%J%"=="%K%" Goto SrcDirDepth
Set /A K-=1

If /I "%Recursive%"=="Yes" (
Set L=%SrcDir%
For /R "%SrcDir%" %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
If not "%%~dpG"=="%OutputDir%" (
Set H=%%~pG
Set H=!H:~0,-1!
For %%I in ("!H!") Do (Set H=%%~nxI)
Set H=!H!\
If not "!H!"=="%OutputDir%" (
If not "!L!"=="%%~dpG" (
For /F "tokens=%K%* delims=\" %%I in ("%%~dpG") Do (
Echo(
Echo %%J
)
)
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG
"%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))
Set L=%%~dpG
)
)
)
)
) Else (
CD /D %SrcDir%
For %%G in (%InputExt%) Do (
If not "%%~fG"=="%~f0" (
Echo(
Echo %%~nxG
If "%OutputExt%"=="" Set OutputExt=%%~xG
"%IrfanDir%\i_view32.exe" ""%%~fG" %ActionParams% /convert=%OutputDir%%%~nG%OutputSuffix%!OutputExt! /silent"
If Errorlevel 2 (Echo Error saving output^^!) Else (
If Errorlevel 1 (Echo Error loading file^^!))
)
)
)

Echo(
Echo Finished.

:::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::

:End
Echo(
Pause
Echo(

Gownaar
25.07.2014, 01:44 PM
My fault again. Edited; how about now?

Gownaar
25.07.2014, 02:32 PM
Edited again: fixed that problem with parentheses in pathnames.

tennessee
25.07.2014, 06:31 PM
Thank you very much. This is working correctly now.

I'm only now discovering there is an issue with Irfanview itself, as the colors of the images are being shifted in a major way when simply resizing the photos. I have LCMS installed and enabled, but not sure what I need to do in order to keep the same color profile the original image had. Regardless, I need to post this as a new thread. Thanks so much for the script...I hope Irfanview will for me in the end, because your script is perfect.

Thanks

Gownaar
25.07.2014, 06:56 PM
Your feedback is very much appreciated, you helped me realize and fix script issues.
As for this color shift, I wouldn't know about that, hopefully someone else can help you.

Gownaar
30.03.2015, 10:39 PM
I added the option to delete source files; it can be useful for bulk-converting image folders without having to clean up afterwards. Also fixed some bugs.

Gownaar
27.12.2016, 10:36 AM
- added support for 64-bit IV

- added IniDir option to specify custom location of i_view32/i_view64.ini

- added protection against inadvertent overwrite of source files through wrong options combo

- DeleteInput now requires InputExt to be set (as a safety measure, primarily against deleting text files)

- script now performs no action by default

- added readme.txt with some notes and usage examples


Get it from post #1 (https://irfanview-forum.de/showthread.php?t=10087&p=42762&viewfull=1#post42762)