<http://www.netikka.net/tsneti/info/tscmd190.php>
Copyright © 2017- by Prof. Timo Salmi  
Last modified Mon 20-Nov-2017 10:38:31
Welcoming the user from 54.145.16.43
On Tue 19-Dec-2017 00:30:33 server time

 
Assorted NT/2000/XP/../Win10 CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page relates to my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.

Windows 10

190} How can I rotate a photo on Windows 10 despite its Explorer problems?

It has been quite a long time since I last added an entirely new item to my Assorted NT/2000/XP/.. CMD.EXE Script Tricks collection http://bit.ly/RMGu5A even though I have made some updating of the already existing items along the way. In November 2017 finally bought a new laptop with a Windows 10 system. Of course some discrepancies and annoying problems surfaced. This item rises from a problem I needed to solve for myself. When one opens the Windows Explorer and right-clicks a photo one gets the option to rotate the photo. In at least in XP and 7 the original photo gets rotated, but on my Windows 10 laptop only the thumbnail appears rotated. The actual photo stays unchanged ("unrotated")! I needed a working solution.

However, even with the earlier Windows versions there can be problems. Some of the resolution (or much of it) might be "lost in translation". The solution below, should avoid also this problem.

My compliments to Irfan Skiljan for his excellent IrfanView. This Rotate90.cmd script is dependent on the command-line extensions of his viewer.

@echo off & setlocal enableextensions
rem enabledelayedexpansion
rem C:\_F\XTOOLS\Rotate90.cmd
rem C:\_H\PHOTO\IrfanViewCmd.pdf

:: On demand, display the full filename of the current script
if /i "%~1"=="/0" (
  call :ProgramTitle
  echo.
  call :CurrentScript "%~f0"
  goto _out)

:: Script's defaults
set ir_=C:\Program Files\IrfanView\i_view32.exe
if defined ProgramW6432 set ir_=C:\Program Files (x86)\IrfanView\i_view32.exe
set jh_=C:\_F\BOX\BOXTOOLS\jhead297.exe
if not exist "%ir_%" (
  call :ProgramTitle
  echo.
  echo Exiting: Needed program "%ir_%" not found^G
  echo For downloading see http://www.irfanview.com/
  goto _out)
if not exist "%jh_%" (
  call :ProgramTitle
  echo.
  call :ProgramHelp
  echo.
  echo Exiting: Needed program "%jh_%" not found^G
  echo For downloading see http://www.sentex.net/~mwandel/jhead/
  echo Or customize the script, since this is not crucial
  goto _out)

:: Help
if "%~1"==""   (call :ProgramTitle & echo. & call :ProgramHelp & goto _out)
if "%~1"=="?"  (call :ProgramTitle & echo. & call :ProgramHelp & goto _out)
if "%~1"=="/?" (call :ProgramTitle & echo. & call :ProgramHelp & goto _out)

:: The caption
call :ProgramTitle

:: Check filename format, no wildcards
call :IsWild "%~1" wild_
if defined wild_ goto _out

:: Get and check the other parameters / switches
set NoQuestions=
set RotateLeft=
set Flip=
set par2=%~2
set par3=%~3
set par4=%~4
for %%p in (%par2% %par3% %par4%) do (if /i "%%p"=="/q" set NoQuestions=true)
for %%p in (%par2% %par3% %par4%) do (if /i "%%p"=="/l" set RotateLeft=true)
for %%p in (%par2% %par3% %par4%) do (if /i "%%p"=="/f" set Flip=true)
set error_=true
if defined NoQuestions set error_=
if defined RotateLeft  set error_=
if defined Flip        set error_=
if "%par2%"==""        set error_=
if defined error_ (
  echo.
  echo Error in parameters, possible causes:^G
  echo An unknown parameter
  echo A file name with blanks has not been enclosed in parentheses
  goto _out)
if defined RotateLeft if defined Flip (
  echo.
  echo Error: Use /F or /L, but not both^G
  goto _out)

:: The actual rotation
echo.
call :RotateImage "%~1"
goto _outNoPause

:_out
:: A conditional pause on my own system. Ignore.
if not defined cmdbox if defined PauseIfFromDesktop pause
:_outNoPause
endlocal & goto :EOF

rem Parameter value information from IrfanView Command Line Options documentation
rem Transformation rotate 90
rem Optimize
rem Set EXIF date
rem Keep current date
rem Set DPI
rem DPI value2
rem Marker option : Keep all (0), Clean all (1), Custom (2)

:: Perform a lossless rotation of the photo
:RotateImage
setlocal
set fullname_=%~f1
set basename_=%~n1
set ext_=%~x1
set CopyToFolder=C:\_M\Photo Rotation Backup
set trans_=3
if defined Flip set trans_=1
if defined RotateLeft set trans_=5
if not exist "%fullname_%" (
  echo File "%fullname_%" not found^G
  goto _eofRotateImage)
echo start "" /wait "%ir_%" "%fullname_%" /jpg_rotate=(%trans_%,0,1,0,0,0,0,0) /cmdexit
if defined NoQuestions goto _startRotateImage
call :AskQuestionWithYdefault "Do [n,Y?]" reply_
if "%reply_%"=="n" goto _eofRotateImage
:_startRotateImage
rem If you do not want backups comment away the next three lines below
if not exist "%TargetFolder%\" mkdir "%TargetFolder%"
echo xcopy /v/f/d "%fullname_%" "%CopyToFolder%\"
xcopy /v/f/d "%fullname_%" "%CopyToFolder%\"
start "" /wait "%ir_%" "%fullname_%" /jpg_rotate=(%trans_%,0,1,0,0,0,0,0) /cmdexit
dir "%fullname_%"|find " Directory of "
dir "%fullname_%"|find /i " %basename_%%ext_%"
:_eofRotateImage
endlocal & goto :EOF

:ProgramTitle
echo +----------------------------------------------------+
echo Rotate90.cmd Rotate a .jpg image to the right      
echo By Prof. Timo Salmi, Last modified Sun 19-Nov-2017
echo +----------------------------------------------------+
goto :EOF

:ProgramHelp
echo Usages:
echo  For a single file Rotate90 "filename" [/F] [/L] [/Q]
echo                                    flip -+        
echo                                  rotate left -+    
echo                               no questions asked  -+
echo.
echo  For multiple files e.g.
echo   for %%f in ("C:\_M\*.JPG") do call Rotate90 "%%~ff" /Q
set itsme_=
if /i "%USERNAME%"=="ts" set itsme_=true
if /i "%USERNAME%"=="es" set itsme_=true
if /i "%USERNAME%"=="tsadmin" set itsme_=true
if not defined itsme_ goto _eofProgramHelp
echo.
echo Closely related:
echo   JPGSCALE7.CMD /M (From: "C:\_M\*.JPG" resize to C:\_M\Resized1024)
echo.
call :AskQuestionWithNdefault "Display related programs' common help [N/y]?" reply_
if "%reply_%"=="y" (call :HelpText "C:\_F\XTOOLS\JPGSCALE.TXT" & goto _out)
:_eofProgramHelp
goto :EOF

:AskQuestionWithNdefault
  setlocal enableextensions
  :_askthenquestionagain
  set return_=
  set ask_=
  set /p ask_="%~1"
  if "%ask_%"=="" set return_=n
  if /i "%ask_%"=="n" set return_=n
  if /i "%ask_%"=="y" set return_=y
  if not defined return_ goto _askthenquestionagain
endlocal & set "%2=%return_%" & goto :EOF

:AskQuestionWithYdefault
  setlocal enableextensions
  :_asktheyquestionagain
  set return_=
  set ask_=
  set /p ask_="%~1"
  if "%ask_%"=="" set return_=y
  if /i "%ask_%"=="y" set return_=y
  if /i "%ask_%"=="n" set return_=n
  if not defined return_ goto _asktheyquestionagain
endlocal & set "%2=%return_%" & goto :EOF

:IsWild
setlocal
set return_=
echo %~1|findstr "[*?]">nul
if %errorlevel% GTR 0 goto _endIsWild
echo.
echo Parameter: %~1
echo Wildcards are not allowed in the file name^G
echo For multiple files use e.g.:
echo for %%f in ("%~1") do call Rotate90 "%%~ff"
set return_=true
:_endIsWild
endlocal & set "%2=%return_%" & goto :EOF

:HelpText
setlocal
set less_=C:\_F\FTOOLS\unxless.exe
if not exist "%less_%" (
  echo Exiting: Pager %less_% not found^G
  goto _eofHelpText)
set helptext_=%~1
if not exist "%helptext_%" (
  echo Exiting: File %helptext_% not found^G
  goto _eofHelpText)
"%less_%" -i -S "%helptext_%"
:_eofHelpText
endlocal & goto :EOF

:CurrentScript
setlocal
set d_=%~d1
if "%d_%"=="c:" set d_=C:
if "%d_%"=="d:" set d_=D:
if "%d_%"=="e:" set d_=E:
if "%d_%"=="f:" set d_=F:
echo "%d_%%~pnx1"
echo.
dir "%d_%%~pnx1"|findstr /i "%~nx1"
endlocal & goto :EOF

:: Only used during my debugging
:SmallDelay
setlocal
ping -n %~1 127.0.0.1>nul
endlocal & goto :EOF

:: Just for debugging, show verbally the value of an environment variable
:ShowVarValue [TheNameOfTheVariable]
setlocal
set par1_=%~1
call set value_=%%%par1_%%%
echo The value of %~1 is %value_%
endlocal & goto :EOF

An example of the output:
  C:\_M>rotate90 Torig2017102606.JPG
  +----------------------------------------------------+
  Rotate90.cmd Rotate a .jpg image to the right      
  By Prof. Timo Salmi, Last modified Sun 19-Nov-2017 
  +----------------------------------------------------+
 
  start "" /wait "...\i_view32.exe" "C:\_M\Torig2017102606.JPG" /jpg_rotate=(3,0,1,0,0,0,0,0) /cmdexit
  Do [n,Y?]y
  C:\_M\Torig2017102606.JPG -> C:\_M\Photo Rotation Backup\Torig2017102606.JPG
  1 File(s) copied
   Directory of C:\_M
  26.10.2017  14:51         1,715,376 Torig2017102606.JPG
 
  C:\_M>

And voilà, the photo (and the thumbnail) has been rotated 90°. Be aware, however, that it is not guaranteed that the orientation of the thumbnail and the actual photo will always tally with each other.


If you found this useful, it is convenient to put a shortcut pointing to Rotate90.cmd into your user profile's SendTo Folder. Then you can right-click any photo easily to rotate it. Your SendTo folder is located at "%APPDATA%\Microsoft\Windows\SendTo".



In this SendTo context Rotate90.cmd can only be used for "Rotate Right" i.e. clockwise rotation. For a similar right-clickable SendTo item you just need the following simple Rotate270.cmd script. And, naturally, a shortcut to it to put in the SendTo folder.
@echo off & setlocal enableextensions
rem C:\_F\XTOOLS\Rotate270.cmd
rem Fri 17-Nov-2017 16:36:04
echo.
echo +---------------------------------------------+
echo Calling Rotate90 "%%~1" /L                   
echo for rotating to the left (counterclockwise)
echo +---------------------------------------------+
echo "%~1"
echo.
call Rotate90 "%~1" /L
endlocal & goto :EOF

For flipping:
@echo off & setlocal enableextensions
rem C:\_F\XTOOLS\Rotate180.cmd
rem Sun 19-Nov-2017
echo.
echo +-------------------------------------------------+
echo Calling Rotate90 "%%~1" /F                       
echo for flipping the image i.e rotating 180 degrees
echo +-------------------------------------------------+
echo "%~1"
echo.
call Rotate90 "%~1" /F
endlocal & goto :EOF

References/Comments:
  A ShellExtension for IrfanView, including lossless rotation of JPGs
  alt.msdos.batch.nt Fri, 17 Nov 2017 15:19:24 -0800 (PST)