Comments on: Substrings in Windows Batch Files http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: Gunko http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-48648 Sat, 17 Aug 2013 18:23:18 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-48648

@ Schmalls:
Thank you so much for this!

Reply  |  Quote
]]>
By: vamsi http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-21230 Fri, 20 Jan 2012 10:24:14 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-21230

@ Harish:
set test=BBB2100BBB
echo %test:~3,4%

Reply  |  Quote
]]>
By: Harish http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-20912 Thu, 01 Dec 2011 10:43:39 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-20912

Hi all,

I need ur help. Actually I m trying to read 1 text file and trying to get the substring from that.

my test.txt file c0ntatins
BBB2100BBB

my requirement to get the value 2100. Means the value between the BBB and BBB.
Please give me some solution asap.

Thanks in advance.

Reply  |  Quote
]]>
By: deoren http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-19835 Mon, 01 Aug 2011 20:59:12 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-19835

Awesome posts, thanks! Found it via Google after looking up “batch file sub string”. I didn’t realize that the windows shell had a built in substring function until I stumbled onto this page.

Thanks again.

Reply  |  Quote
]]>
By: FrodoNL http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-17950 Mon, 06 Dec 2010 13:14:37 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-17950

And yet another thank-you – EnableDelayedExpansion just saved me hours of work!

Reply  |  Quote
]]>
By: Eugene http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-17564 Wed, 27 Oct 2010 08:58:40 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-17564

Thank you very much for the post!

Reply  |  Quote
]]>
By: cire zerep http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-14825 Thu, 01 Apr 2010 21:19:47 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-14825

This is great stuff and exactly what I was looking for. I’ve been writing batch files for many years and never knew about this functionality. Thanks so must for posting it.

Reply  |  Quote
]]>
By: JBC http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-14468 Wed, 03 Mar 2010 20:46:00 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-14468

Thank you Schmalls. That EnableDelayedExpansion was just what I needed to trim 17 leading characters (of file path) out of a string.

Reply  |  Quote
]]>
By: Schmalls http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-10600 Wed, 05 Nov 2008 16:05:46 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-10600

Here is what I did to get the FOR variable:

SETLOCAL EnableDelayedExpansion
FOR /F %%F IN ("%FILELIST%") DO (
SET G=%%F
IF "!G:~-10,-3!" == "%KEYWORD%" (
:: Process one way...
) ELSE (
:: Process another way...
)
)

The EnableDelayedExpansion switch allows us to use variables in a loop with the ! syntax instead of the % syntax.

Reply  |  Quote
]]>
By: Simon Watts http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-9249 Wed, 04 Jun 2008 17:11:34 +0000 http://www.terminally-incoherent.com/blog/2006/10/27/substrings-in-windows-batch-files/#comment-9249

Substrings all very well on %VARIABLES%, but how do you take a substring of a FOR variable in a batch file? For example, the following code fails because the substring notation on “%%F” is not correct:

FOR /F %%F IN ("%FILELIST%") DO (
IF "%%F:~-10,-3%" == "%KEYWORD%" (
:: Process one way...
) ELSE (
:: Process another way...
)
)

How do you perform a substring operation on a FOR loop variable?

Reply  |  Quote
]]>