subtility with FOR

  • Thread starter Thread starter Gustavio
  • Start date Start date
G

Gustavio

Hello,

The line a want to process with the following for is like this :

TS020139,0000399AF902,1990/01/01 00:12:56

for /f "Tokens=3 Delims=," %%d in ...

It works good.

But if I have a line like this :

TS020139,,1990/01/01 00:12:56

It doesn't work anymore.

why ?
what should I do ?

Thanks for your suggestions.

Matthieu/Gustavio
 
Gustavio said:
Hello,

The line a want to process with the following for is like this :

TS020139,0000399AF902,1990/01/01 00:12:56

for /f "Tokens=3 Delims=," %%d in ...

It works good.

But if I have a line like this :

TS020139,,1990/01/01 00:12:56

It doesn't work anymore.

why ?
what should I do ?

It does not work because the ,, does not have a token.

Assuming the "..." in your example is a variable with a string,

@echo off
set str=TS020139,,1990/01/01 00:12:56
set str=%str:,,=,_,%
for /f "Tokens=3 Delims=," %%d in ("%str%") do echo %%d


But if the "..." is actually parsing a file, you might try,

@echo off
for /f "Tokens=*" %%d in (file) do call :GetDT "%%d"
goto :eof

:GetDT
set str=%~1
set str=%str:,,=,_,%
for /f "Tokens=3 Delims=," %%d in ("%str%") do echo %%d
 
Thanks for your answer Todd.

The thing is I don't when it will be ,, or not...

So I can't really do :
:GetDT
set str=%~1
set str=%str:,,=,_,%

( i'm analysing a log file...)

If you have any idea, it's welcome
Thanks for you help again
Matthieu/Gustavio
 
Todd Vargo said:
It does not work because the ,, does not have a token.

Assuming the "..." in your example is a variable with a string,

@echo off
set str=TS020139,,1990/01/01 00:12:56
set str=%str:,,=,_,%
for /f "Tokens=3 Delims=," %%d in ("%str%") do echo %%d


But if the "..." is actually parsing a file, you might try,

@echo off
for /f "Tokens=*" %%d in (file) do call :GetDT "%%d"
goto :eof

:GetDT
set str=%~1
set str=%str:,,=,_,%
for /f "Tokens=3 Delims=," %%d in ("%str%") do echo %%d
Gustavio said:
Thanks for your answer Todd.

The thing is I don't when it will be ,, or not...

So I can't really do :

( i'm analysing a log file...)

If you have any idea, it's welcome
Thanks for you help again
Matthieu/Gustavio

for /f "tokens=2,3 delims=," %%i in (file.txt) do if "%%j"=="" (echo %%i)
else (echo %%j)

(all as one line)

HTH

....Bill
 
The line a want to process with the following for is like this :

TS020139,0000399AF902,1990/01/01 00:12:56

for /f "Tokens=3 Delims=," %%d in ...

It works good.

But if I have a line like this :

TS020139,,1990/01/01 00:12:56

It doesn't work anymore.

why ?
what should I do ?

4NT has the function @FIELD[] which observes consecutive separators:
SET text=TS020139,0000399AF902,1990/01/01 00:12:56
SET /A nF=%@FIELDS[",",%text] - 1
GOSUB dF
SET text=TS020139,,1990/01/01 00:12:56
SET /A nF=%@FIELDS[",",%text] - 1
GOSUB dF
QUIT
:dF
ECHO Text is: %text
DO n = 0 TO %nF
SET F=%@FIELD[",",%n,%text]
IFF "%F" NE "" THEN
ECHO Field %n: %F
ELSE
ECHO Field %n is empty.
ENDIFF
ENDDO
RETURN

which gives:
Text is: TS020139,0000399AF902,1990/01/01 00:12:56
Field 0: TS020139
Field 1: 0000399AF902
Field 2: 1990/01/01 00:12:56
Text is: TS020139,,1990/01/01 00:12:56
Field 0: TS020139
Field 1 is empty.
Field 2: 1990/01/01 00:12:56

@FIELD[] is documented at <http://jpsoft.com/help/f_field.htm>.
 
Content-Class: urn:content-classes:message
From: "Nimit Mehta" <[email protected]>
Sender: "Nimit Mehta" <[email protected]>
Subject: RUNAS PASSWORD
Date: Mon, 28 Jun 2004 14:13:15 -0700
Lines: 7
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcRdVLpM3giN1gF1S7mpXvRN6rr4gQ==
Newsgroups: microsoft.public.win2000.cmdprompt.admin
NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
Path:
g2news1.google.com!news2.google.com!proxad.net!teaser.fr!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTNGP08.phx.gbl!TK2MSFTFEED02.phx.gbl!cpmsftngxa10.phx.gbl
I have a batch file, when run, would RUNAS administrator
another batch file. At prompt i want to automatically
insert my password for administrator. Anyway i can have
the password in the batch file itself? so when run it
would RUNAS Admin automatically? I can then convert that
batch file to a .com file using bat2exec for security
reasons, so people cant see the password.



Hello,

a simple tool to use is runasspc on robotronic.de/runasspcEn.html

bye
 
Back
Top