Solved

String manipulation in DOS (Batch)

Posted on 2006-11-02
3
15,909 Views
Last Modified: 2012-06-22
Sample data I have to work with:

   abcd123, Joe Bloggs logging on to PC25 DOMAIN on <IP> on Thursday, November 2, 2006

What I want the output to look like:

   Joe Bloggs - Thursday, November 2, 2006

This is the code I have at the moment (%* is input data):

   set X=%*
   set X=%X: on =      %
   set X=%X: logging=      %
   for /f "tokens=1,4 delims=      " %%i in ("%X%") do echo %%i - %%j

This is what my output then looks like:

   abcd123, Joe Bloggs - Thursday, November 2, 2006

My question is, I can't work out how to get rid of the substring "abcd123, ".

Thanks,
Steve :)
0
Comment
Question by:sda100
3 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 250 total points
ID: 17857574
:: replace/extract tokens from string
@echo off
setlocal
set X=abcd123, Joe Bloggs logging on to PC25 DOMAIN on 192.168.2.1 on Thursday, November 2, 2006
for /f "tokens=2,3,12,13,14,15 delims= " %%i in ('echo %X%') do echo %%i %%j - %%k %%l %%m, %%n
set X=%X: on =     %
set X=%X: logging=     %
for /f "tokens=2,3,8,9,10,11 delims= " %%i in ('echo %X%') do echo %%i %%j - %%k %%l %%m, %%n
goto :eof
0
 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 250 total points
ID: 17857764
Here's another possible solution:

@echo off

setlocal

call :PROCESS abcd123, Joe Bloggs logging on to PC25 DOMAIN on 0.0.0.0 on Thursday, November 2, 2006

goto :EOF

:PROCESS

set name=%2 %3

for /l %%a in (1, 1, 6) do shift

echo %name% - %6, %7 %8, %9

Good Luck,
Steve
0
 
LVL 9

Author Comment

by:sda100
ID: 17865245
Both the above solutions work on the premise of a fixed number of tokens, which I can't guarantee.  However, you gave me ideas for the solution, so I will share the points - thank you.  If you're interested, here is the part of my code that achieves what I want:

if "%*"=="" goto :eof
set X= %*
set X=%X: on =      %
set X=%X: logging=      %
for /f "tokens=4 delims=      " %%i in ("%X%") do set Y=%%i
set Y=%Y:~0,-12%
for /f "tokens=1 delims=      " %%i in ("%X%") do set X=%%i
:logformat_loop
set X=%X:~1%
echo %X% | find "," > nul
if not errorlevel 1 goto logformat_loop
set X=%X:~1%


Steve :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question