Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > String operations in a CMD/batch file

Reply
Thread Tools Display Modes

String operations in a CMD/batch file

 
 
Michael Leone
Guest
Posts: n/a

 
      08-29-2008
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.


 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      08-29-2008

"Michael Leone" <> wrote in message
news:...
> 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.


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


 
Reply With Quote
Reply

Thread Tools
Display Modes

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
Track file operations Ben Bazian Windows Server 0 07-11-2007 12:39 AM
File operations Johnny Windows Vista General Discussion 1 02-28-2007 01:41 PM
Slow file operations and cannot use ViceVersa krypticide Windows Vista File Management 2 06-20-2006 02:16 PM
Batch file for deleting files by date string in name Ragnar Midtskogen Scripting 0 01-30-2006 11:06 PM
Search a file for string and repolace string?? Paul Scripting 0 07-01-2003 10:32 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59