Computing.Net > Forums > Programming > Editing file names in a batchscript

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Editing file names in a batchscript

Reply to Message Icon

Name: pball
Date: August 20, 2008 at 16:34:32 Pacific
OS: XP Pro
CPU/Ram: 2.8 ghz / 2 gig
Product: home made
Comment:

I'd like to be able to edit the file name inside a batch script

example of what i start with
"E:\music.mp3" ( i set this as a variable)

I would like to have it look like
E:\music

How can i have it remove the first character and last 5 characters?



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 21, 2008 at 03:00:06 Pacific
Reply:

Please see the post below as I have realsied I'm complicating things for no reason.

---------- The rest of this post is obselete ----------

The script below requires you set a counter to zero and a temporary variable to the value of your variable (your filename), but will still change your variable later on.


set count=0
set tempvariable=%yourvariable%

:length
if not defined tempvariable goto nextthing
set /a count+=1
set tempvariable=%tempvariable:~1%
goto length

:nextthing
set /a takeoff=%count%-6
set result=yourvariable:~1,%takeoff%
call set yourvariable=%%%result%%%
rem yourvariable should now contain
rem your edited filename.


The length label is to find out how many characters yourvariable is by stripping out a character of the tempvariable and setting it to to the remaining value until it is no longer defined. The counter will have the number of characters stored in it after this section is finished. The ":~1" at the end of the tempvariable variable, but inside the percentage signs make the variable display, set ect. without it's first character.

The takeoff variable will be the number of characters you want after removing the six you don't want. The result variable is an intermediate step to setting yourvariable from position two to %takeoff% number of places to(:~1,%takeoff%) the right(In this case number one is position two as it start counting from zero).


0

Response Number 2
Name: Judago
Date: August 21, 2008 at 03:37:52 Pacific
Reply:

After all that I just realised all you will need to do is.

set yourvariable="E:\music.mp3"
echo %yourvariable:~1,-5%

This would echo: E:\music


%yourvariable:~1,-5% - All this basically means is "the string inside %yourvariable% starting from the second character, excluding the last five characters". (The first character is zero not one)

This will be all you want. You can set a variable from it or just use it as is. Go to the command line and type "set /?" for more information.


I hope you learn't something from my last post anyway :-)


0

Response Number 3
Name: pball
Date: August 21, 2008 at 14:07:11 Pacific
Reply:

Thank you, I didn't read the first one but the second one helps me out greatly. This is something I should be able to use for many things.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Editing file names in a batchscript

Edit batch file while in a cmd window www.computing.net/answers/programming/edit-batch-file-while-in-a-cmd-window/19639.html

batch file name(s) in memory? www.computing.net/answers/programming/batch-file-names-in-memory/15656.html

multi part file name in C++ www.computing.net/answers/programming/multi-part-file-name-in-c/14174.html