0

I'm creating a batch script, which will allow me to change any type of files to another. But i have problems with getting the file names from folder. %%~nxi contains file name, but i cant write it to a variable, so i cant use it. And also I have problems with how to add an extension to the file name:

magick convert "file1.jpg" "folder\file2.png"

How i can make "file2.png" using variable and .png extension? My ideas:

"folder\{file_name}.png" 
"folder\!file_name!.png" 
"folder\%file_name%.png" 

Batch script:

set png_folder=PNG
set jpg_folder=JPG

for /f "delims=" %%i in ('dir /b "%jpg_folder%"') do (
  set file_name="%%~nxi"
  :: file_name is always empty
  magick convert "%%f" "%png_folder%\{file_name}.png" 
)
pause
  • Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) Specifically, add `setlocal enabledelayedexpansion` to the top of your script and use `"%png_folder%\!file_name!.png"`. Or just use `"%png_folder%\%%~ni.png"` directly since there's no need (in the code that you posted) to save the name to a variable at all. – SomethingDark Aug 18 '23 at 10:01
  • Apart from the fact you have no reference for `%%f` and your `set` commands are using the wrong syntax, you are going to have issues with variable expansion. Of course, you could just do this instead: ```For /F "EOL=? Delims=" %%G In ('Dir "%jpg_folder%" /A:-D /B') Do magick.exe convert "%jpg_folder%\%%G" "%png_folder%\%%~nG.png"```. – Compo Aug 18 '23 at 10:04
  • Do not use `::` comments in `code blocks` (command sequences in parentheses) - use `rem` as `::` is an unreachable label and labels break code blocks. – Magoo Aug 18 '23 at 10:52

2 Answers2

0

Solved a problem:

set png_folder=PNG
set jpg_folder=JPG

for %%f in (%jpg_folder%\*.jpg) do (
   set "file_name=%%~nf"
   magick convert "%%f" "%png_folder%\!file_name!.png"
)
pause
0

You can do it more efficiently by having IM itself iterate the files:

convert *.jpg -set filename:base "%[basename]" "png_directory/%[filename:base].png"
xenoid
  • 8,396
  • 3
  • 23
  • 49