[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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