1.

Solve : Modifying file name by adding a suffix?

Answer»

Hi All, DOS Noob here. I have been working on a problem that unfortunately has me stumped. I have a jpg file name I WANT to convert the file to a png file which I can do with xcopy however I am having a lot of trouble in adding _small to the filename. The resultant should be *_small.png. My code gets me to *.png_small.png I have tried many different THINGS (variations) and I cannot find an answer for it. Please help.


@Echo
c:
CD \test\

xcopy *.jpg *.png

FOR /f "delims=" %%F IN ('dir /b/s *.png') do Echo REN "%%F" "%%F"_small.png

Pause

Thankyou in advance



Quote

I have a jpg file name I want to convert the file to a png file which I can do with xcopy

XCopy cannot and will not change a file's internal organization. Changing a file extension does not give you a new type of file. For instance, changing a file extension to XLS does not convert the file to an Excel workbook. Best practices indicate reading the contents of the file into a variable, then saving the contents with the new file type. Powershell can do this (see below)

This LITTLE blurb shows how to write your for statement (execution at command prompt): The code syntax will rename the file correctly but will not convert PNG to JPG.

Code: [Select]for /f "delims=" %F in ('dir /b/s *.png') do echo %~dpnxF %~dpnF_small.jpg

Powershell script to change PNG file to JPG file:
Code: [Select]#Requires -Version 2.0

Add-Type -AssemblyName System.Drawing
$imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]

$sourceFile = "C:\temp\razr_product_red.jpg"
$saveFile = "C:\temp\razr_product_red_small.png"

$image = [drawing.image]::FromFile($sourceFile)
$image.Save($saveFile, $imageFormat::png)

Happy Days, just a clarification, if the file names you are working with you in the for loop contain spaces it could error. i would recomend putting quotations around the %~pdnxF as well as the %~pdnF_small.jpg.thanks for your input guys. Even if I did not change the file type I still cannot add the SUFFIX, even after your suggestions. My knowledge of DOS is extremely limited and I am not sure where to go from here. Anymore ideas?Give this a shot. The free graphics viewer/editor Irfanview is able to convert from JPG to PNG using a command line interface and batch file.

Code: [Select]FOR /f "delims=" %%F IN ('dir /b /s /a-d *.png') do REN "%%F" "%%~nF_small.png"Is the OP WANTING to crate a photo album for a web site?
There are programs that do all the work for you. It is a feature in Dreamweaver as well as other WYSIWYG editors.
http://www.youtube.com/watch?v=YlB_IZqKg_o

Thankyou everybody for their efforts. Foxidrives' solution did the trick. I will look into irfanview.


Discussion

No Comment Found