String manipulation in DOS (Batch)

Posted on 2006-11-02
Medium Priority
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, ".

Steve :)
Question by:sda100
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 30

Accepted Solution

callrs earned 1000 total points
ID: 17857574
:: replace/extract tokens from string
@echo off
set X=abcd123, Joe Bloggs logging on to PC25 DOMAIN on 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
LVL 30

Assisted Solution

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

@echo off


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

goto :EOF


set name=%2 %3

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

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

Good Luck,

Author Comment

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
set X=%X:~1%
echo %X% | find "," > nul
if not errorlevel 1 goto logformat_loop
set X=%X:~1%

Steve :)

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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