Solved

String manipulation in DOS (Batch)

Posted on 2006-11-02
3
15,888 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now