Solved

String manipulation in DOS (Batch)

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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