Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting substring of a token in for loop?

I have this for loop to get a list of directory names:

for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do ( echo %%g ) 

Output:

C:\WINDOWS\Assembly\gac_msil\policy.5.0.A.D C:\WINDOWS\Assembly\gac_msil\policy.5.0.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.20.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.25.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.35.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.55.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.60.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.5.70.A.D.O C:\WINDOWS\Assembly\gac_msil\policy.6.0.A.D.O 

I want to get the folder names starting with "policy" but echo %%g:~29 doesn't work. I also tried to set x=%%g and then echo %x:~29% and still doesn't work.

So, how do I get substring from token in for loop?

like image 675
Ray Cheng Avatar asked Dec 27 '11 18:12

Ray Cheng


People also ask

What do tokens and delims mean in a for loop?

Quite often when writing a batch file, you will come across a FOR loop. It might look something like this: I am constantly hearing people asking “What do tokens and delims mean?”. Well, here you are. Tokens basically tell the batch file where to look to set the variable (%a). Delimiters are what separate each token.

How to substring a string in Java?

Substring in Java 1 String substring () : This method has two variants and returns a new string that is a substring of this string. The... 2 String substring (begIndex, endIndex): This method has two variants and returns a new string that is a substring of... More ...

Which token will return the first word in the line?

Since leading delimiters (before the first word) are ignored, however, it is still the first word in the line, so we need token 1. will return some string (without the leading spaces).

How to get the substring between two instances of the same string?

There is a simplified version of this method in case the substring is nested in between two instances of the same String: The substringAfter method from the same class gets the substring after the first occurrence of a separator. The separator isn't returned:


1 Answers

Of course that set x=%%g and a substring extraction of x should work, but be aware that if the substring is taken inside a FOR loop, it must be done with ! instead of % (Delayed Expansion):

setlocal EnableDelayedExpansion for /d %%g in (%windir%\Assembly\gac_msil\*policy*A.D*) do ( set x=%%g echo !x:~29! ) 
like image 159
Aacini Avatar answered Sep 29 '22 21:09

Aacini