String operations in a CMD/batch file

Discussion in 'Scripting' started by Michael Leone, Aug 29, 2008.

  1. Pardon the OT question; if there's a better place to ask, please direct me.

    So here's my problem .. I have this CMD shell script (not VBScript or
    PowerShell), and what I need to do is some string manipulation.
    Specifically:

    I need to strip off the leading 2 characters from a string, and then I need
    to locate the position of a special character in the string. So I have this
    value:

    \\servername\users\username

    What I want to do is to isolate "servername".

    Rewriting the script as VBSript or PowerShell isn't really an option yet, as
    I am not proficient enough in those to do this.

    So what I need to do is parse the string, and return everything from
    position 3 to the location of the first "\". I can then use that value
    later in an IF statement,

    I have this:

    _Server="ThatServer"

    SET _Offset=3
    SET _Chars=11
    FOR /F "DELIMS=, SKIP=1 TOKENS=1-6" %%A IN (HomeFolders.txt) DO (

    SET _HomeFolder=%%E

    IF /I "!_HomeFolder:~%_Offset%,%_Chars%!" NEQ "!_Server!" (

    I then do stuff here. This works, but only because the IF statement is
    comparing "servername\users\username" to "ThatServer", and of course
    failing. Ideally, what I want is just the servername. (I get these values by
    extracting them from my Win2000 AD, using the ADFIND program, and
    redirecting output to a file. I then read that file back in.

    So any clues, pointers, semi-vitrolic abuse appreciated. Better ways, other
    utilities, other examples, etc.
     
    Michael Leone, Aug 29, 2008
    #1
    1. Advertisements

  2. In your case you don't need string manipulation in order to extract
    "ServerName" from "\\servername\users\username". This two-line batch file
    will do it by using a parsing technique:
    @echo off
    for /F "tokens=1 delims=\" %%a in ('echo \\servername\users\username') do
    set server=%%a
     
    Pegasus \(MVP\), Aug 29, 2008
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.