Batch Rename file using Dos Left Function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kris.Mitchell
Posts: 3
Joined: 07 Dec 2009 12:41

Batch Rename file using Dos Left Function

#1 Post by Kris.Mitchell » 07 Dec 2009 12:50

I am using a batch file with ImageMagick to convert pictures from one format to another.

I only need to run the program for the files in the directory.

I can't get the variable b to be a:0,-4

It ends up printing to the screen as b:0,-4.

Code: Select all

for %%a in (*.ps) DO (
SET b=%%a:~0,-4!
convert -density 288 %%a +matte -resize 50%% %b.jpg
echo %b%
)


Thanks for the help.

-Kris

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 07 Dec 2009 20:21

Kris.Mitchell,

To get the name of a file without extension simply use "substitution of FOR variable references" like this:

Code: Select all

@echo off
for %%A in (*.*) DO (
    echo.convert -density 288 "%%A" +matte -resize 50%% "%%~nA.jpg"
)

Type "FOR /?" on a command line to read more, or go here: http://www.dostips.com/DosCommandIndex.htm#FOR


If however you want to use the "left string" then you could do it like this:

Code: Select all

@echo off
Setlocal enabledelayedexpansion
for %%a in (*.*) DO (
    set b=%%a
    set b=!b:~0,-4!
   
    echo.convert -density 288 "%%a" +matte -resize 50%% "!b!.jpg"
)

DosItHelp? :wink:

Kris.Mitchell
Posts: 3
Joined: 07 Dec 2009 12:41

#3 Post by Kris.Mitchell » 08 Dec 2009 07:49

it DOSe help! :)

I have one last issue though.

I have a space between b and the .jpg extention.

So I tried :idea:

Code: Select all

FOR /L !b! IN (1,1,10)

and

FOR /L %%b IN (1,1,10)


just in case there were more than one space

as well as

Code: Select all

SET b=!b:~0,-1! 

and

Set b=%b:~0,-1%


I ended up with funky filenames each go around.

This should be the last piece to the puzzle. Thank you again for the help.

-Kris

Kris.Mitchell
Posts: 3
Joined: 07 Dec 2009 12:41

#4 Post by Kris.Mitchell » 10 Dec 2009 07:31

I don't know if this was really the solution, but I've tested it (and for me at least) I found the error.

When running the script from above,

using filename 1234.A.1.ai.ps

and running the script I got something like 1234.A.1 .jpg

I found that I actually had spaces between commands in the file. I, again am not sure, but it's possible that they were interpeted as carage returns and/or spaces.

I butted everything up to each other and then gave it spacing and it seems to work without adding extra spaces.

I hope this helps someone in the future.

-Kris

Post Reply