PC Review


Reply
Thread Tools Rate Thread

How to get a variable substring in a for loop

 
 
ShadowTek
Guest
Posts: n/a
 
      7th Jun 2008
I know how to pull a substring out of a variable, but I don't know how
to get a substring out of a variable in a for loop.

When I try the folluwing command...

for %G in (Input_Files\*) do echo %G:~12%

....I get this:

Input_Files\New Text Document.txt:~12%

But what I am trying to get is this:

New Text Document.txt

What is wrong with this?
 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      7th Jun 2008

"ShadowTek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I know how to pull a substring out of a variable, but I don't know how
> to get a substring out of a variable in a for loop.
>
> When I try the folluwing command...
>
> for %G in (Input_Files\*) do echo %G:~12%
>
> ...I get this:
>
> Input_Files\New Text Document.txt:~12%
>
> But what I am trying to get is this:
>
> New Text Document.txt
>
> What is wrong with this?


Substring manipulation works with environmental variables.
%G is not an environmental variable, hence your problem.
There two ways to resolve this issue. Either like so:
@echo off
setlocal EnableDelayedExpansion
for %%G in (InputFiles\*.*) do (
set Name=%%G
echo !Name:~12!
)

or like so:
@echo off
for %%G in (InputFiles\*.*) do call :Sub %%G
goto :eof
:Sub
set Name=%*
echo %Name:~12%
Note that your choice of coding is fragile because
you do not specify an absolute path for the InputFiles
Folder. To make the batch file robust you must
replace this by "c:\InputFiles" (for example).


 
Reply With Quote
 
 
 
 
ShadowTek
Guest
Posts: n/a
 
      7th Jun 2008
Thanks
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search text for substring and change text colour of substring Carni Microsoft Excel Discussion 5 26th Oct 2011 07:42 PM
String Manipulation - Substring, VB function Left, "length safe" Substring kellygreer1 Microsoft C# .NET 6 29th Oct 2006 09:12 PM
regular expression to match substring xxx and not substring yyy likong@email.com Microsoft C# .NET 6 9th Dec 2005 07:30 PM
RegEx search for a substring within a substring colinhumber@gmail.com Microsoft C# .NET 3 3rd Aug 2005 05:29 PM
Infinite Loop Infinite Loop Infinite Loop Infinite Loop... Donna Windows XP Setup 0 8th Dec 2003 11:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:25 AM.