Jump to content

Null as delimiter?


Recommended Posts


Win XP Home, SP.3 Cmd.exe batch scripting.

Can Null (hex 00) be used as a delimiter in a For loop?

Thanks.

NO. (AFAIK)

Maybe if you post an example of the FOR loop that you have in mind there could be some workaround. .unsure:

jaclaz

Link to comment
Share on other sites

Thanks Jaclaz. I don't have a script yet as couldn't find info on how to use null as a delimiter.

I'm trying to get drive letter assignments into variables using Fsutil as shown:

←C:\>fsutil fsinfo drives

Drives: A:\ C:\ D:\ E:\ F:\ G:\

←C:\>

Seems to me that the delimiter in the above is a space but when tried using a For loop like:

│@echo off
│cls

│setlocal enabledelayedexpansion

│For /f "tokens=1-6 delims= " %%A in ('fsutil fsinfo drives') do (
│ set one=%%B
│ set two=%%C
│ set tre=%%D
│ set for=%%E
│ set fiv=%%F
│)
│echo One = !one! Two = !two! Three = !tre! Four = !for! Five = !fiv!

only A:\ was returned.

So had a look at the Fsutil output in a hex editor with the result:

drivess.jpg

It now seems that the delimiter is null (hex 00) hence my query.

Any advice appreciated.

V.

Link to comment
Share on other sites

I ran into this exact issue! Here is what I came up with to get around this:

@echo off

setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|more') do (
set drive=%%a
set drive=!drive:Drives:=!
set drive=!drive: =!
echo !drive!
fsutil fsinfo volumeinfo !drive!)
endlocal

Link to comment
Share on other sites

A similar solution to the one by Scr1ptW1zard :), possibly simpler :unsure::

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2 delims=: " %%A in ('fsutil fsinfo drives^|more') DO (
IF NOT %%B.==\. (SET drives=%%B:) ELSE (SET drives=!drives!,%%A:)
)
SET drives
ENDLOCAL

:hello:

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Thank you both, lots there for me to ponder.

Another way to ponder:

@ECHO OFF
SET drives=
SET separator=
FOR /F "tokens=1,2 delims=\ " %%A in ('fsutil fsinfo drives^|more') DO CALL :SET_var %%A %%B
SET drives
GOTO :EOF

:SET_var
SET separator=,
IF NOT %2.==. SHIFT&SET separator=
Set drives=%drives%%separator%%1
GOTO :EOF

(doesn't need Delayed Expansion enabled) ;)

jaclaz

Link to comment
Share on other sites

Of course it depends upon what you specifically need to do with each outputted result!

Here is an example without knowing the number of drives

@Echo off&Setlocal enableextensions
For /f "tokens=1* delims= " %%# In ('Fsutil Fsinfo Drives') Do Call :_ %%$
Goto :Eof
:_
If %1' Neq ' (
  Echo/Set "drv=%1"
  Shift)&&Goto _

You would just change Line 6 to suit the task you need to perform on each result.

Link to comment
Share on other sites

I have used in the past part of script written by Yzöwl with a nice workaround if we don't have the more command somewhere in our path.

'Fsutil Fsinfo Drives^|More'

'Fsutil Fsinfo Drives^|find /v ""'

Jaclaz script output all drives letters with comma as separator.

Yzöwl script output only the first found drive letter?

could you explain why you use Call :_ %%$ instead of Call :_ %%#

I would like to learn something i currently don't understand.

Edited by Bilou_Gateux
Link to comment
Share on other sites

My apologies, the example I provided was written and tested only on Vista!

@Echo off&Setlocal enableextensions
For /f "delims=" %%# In ('Fsutil Fsinfo Drives^|More') Do Call :_ %%#
Goto :Eof
:_
If %1' Neq ' (Echo/%1|Find "\">Nul&&(
Echo/Set "drv=%1"
)&Shift)&&Goto _

like the previous example change line 6 to suit the task you need to perform on each result.

@Bilou

in answer to your question, I used %%$ and %%# in the same way as you would use %%b and %%a respectively.

b, (second token), comes after a, (first token)

$, (second token), comes after #, (first token)

In that specific case Drives: was an unwanted first token

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...