Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: pball
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:\musicHow can i have it remove the first character and last 5 characters?

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).

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 :-)

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.

![]() |
![]() |
![]() |

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