Substrings in Windows Batch Files

A relatively unknown feature of the windows shell (cmd.exe) is that it has a built in substring function. You can use the :~ operator in conjunction with any variable to obtain a substring of that variable. For example:

set foo=foobar
echo %foo:~3%

The above will output bar. You can also use two comma separated parameters if you want. The first one is the start index, and the second one is length:

echo %foo:~1,2%

This will display oo. You can also have a negative offset, but not the length:

echo %foo:~-3,3%

This will also produce bar. What would be a practical application for this? You tell me - there are hundreds of ways you could exploit this feature to your advantage. I personally use it in my scripts to create dated directories. Sometimes you want to create a folder labeled with the current date. The easy way to do this of course is:

mkdir "%date%"

The only problem is that this method doesn’t work. Because by default dates contain slashes (”/”) which are interpreted as path delimiters by the mkdir command. What you end up with is a directory tree:

Fri 10\27\2006\

This might be useful, but not exactly what we have wanted. What I really meant to do was this:

mkdir "%date:~0,3% %date:~4,2%-%date:~7,2%-%date:~-4,4%"

Yes, the line is much more verbose, but the end effect is well worth it - you end up with a neatly named folder Fri 10-27-2006. This is really useful trick for quick backup scripts and etc…

Related Posts:

  • Command Line SCP for Windows
  • Windows HTTP Server under 600KB
  • Batch Upload Images to ImageShack using Perl
  • Remove Stuck Jobs from the Printer Queue
  • Using CPAN version of WWW::Mechanize with ActiveState Perl on Windows
  • Disk Cleanup
  • Windows Administration Commands
  • Convert a large Access table into Excel files
  • Windows Task Scheduler: Kill Running Tasks
  • Game Design: Where to put Saved Game Files

  • 2 Responses to “Substrings in Windows Batch Files”

    1. Gravatar Simon Watts UNITED KINGDOM Says: Reply to this comment

      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?

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.14 on Windows Windows XP
    2. Gravatar Schmalls UNITED STATES Says: Reply to this comment

      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.

      Posted using Mozilla Firefox Mozilla Firefox 3.0.1 on Windows Windows XP

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]