conditional argument for FOR loop?

M

Matt Williamson

I'm trying to figure out a way to check for the existance of a command
argument that is equal to a single computer name and if there is no command
argument, process a whole list taken from a net view.

This is what I've tried, but it isn't working and I think it's because the
argument for the FOR loop isn't being read properly. I've tried it with and
without the single quotes, with regular quotes and any other combo I could
think of.

IF "%1"=="" (
set arg='NET VIEW ^| FIND "\\"'
) ELSE (
set arg="\\%1"
)

echo %arg%
pause

FOR /F %%A IN (%arg%) DO (
echo %%A
)

Is there a good way to do this?

TIA

Matt
 
G

Garry

I'm trying to figure out a way to check for the existance of a command
argument that is equal to a single computer name and if there is no command
argument, process a whole list taken from a net view.

This is what I've tried, but it isn't working and I think it's because the
argument for the FOR loop isn't being read properly. I've tried it with and
without the single quotes, with regular quotes and any other combo I could
think of.

IF "%1"=="" (
set arg='NET VIEW ^| FIND "\\"'
) ELSE (
set arg="\\%1"
)

echo %arg%
pause

FOR /F %%A IN (%arg%) DO (
echo %%A
)

Is there a good way to do this?

TIA

Matt

Your problem is that the ^ in the line
set arg='NET VIEW ^| FIND "\\"'
is "consumed" when it is used so that when you use %arg% in the FOR
command, the | pipe is no longer escaped by the ^ and causes problems
parsing the FOR command.

There's a couple of ways around this:

1. quote the expression that uses the caret and the pipe so it isn't
stripped and %arg% retains the caret:
set "arg='NET VIEW ^| FIND "\\"'"

2. insert an extra level of ^ (escaped as ^^) so that %var% retains a
single caret:
set arg='NET VIEW ^^^| FIND "\\"'

Garry
 
B

billious

Matt Williamson said:
I'm trying to figure out a way to check for the existance of a command
argument that is equal to a single computer name and if there is no
command argument, process a whole list taken from a net view.

This is what I've tried, but it isn't working and I think it's because the
argument for the FOR loop isn't being read properly. I've tried it with
and without the single quotes, with regular quotes and any other combo I
could think of.

IF "%1"=="" (
set arg='NET VIEW ^| FIND "\\"'
) ELSE (
set arg="\\%1"
)

echo %arg%
pause

FOR /F %%A IN (%arg%) DO (
echo %%A
)

Is there a good way to do this?

TIA

Matt

set arg=%~1
if defined arg set arg=\\%arg%
if not defined arg for /f %%i in ('net view ^|find "\\" ') do set arg=%%i

Noting that arg will be set to the LAST line containing "\\" returned from
NET VIEW
(to make it the first, add "if not defined arg" after the "do" keyword)
- the first of these line strips any encasing double-quotes. Omit the "~" if
this is not suitable

You may find alt.msdos.batch.nt might be a suitable alternative newsgroup
for batch techniques

HTH

....Bill
 
A

Al Dunbar

Garry said:
Your problem is that the ^ in the line
set arg='NET VIEW ^| FIND "\\"'
is "consumed" when it is used so that when you use %arg% in the FOR
command, the | pipe is no longer escaped by the ^ and causes problems
parsing the FOR command.

There's a couple of ways around this:

1. quote the expression that uses the caret and the pipe so it isn't
stripped and %arg% retains the caret:
set "arg='NET VIEW ^| FIND "\\"'"

2. insert an extra level of ^ (escaped as ^^) so that %var% retains a
single caret:
set arg='NET VIEW ^^^| FIND "\\"'

Another way around the problem is to side-step it a bit. Let's say that you
are doing something a little less trivial than echoing computernames, i.e.:

IF "%1"=="" (
set arg='NET VIEW ^| FIND "\\"'
) ELSE (
set arg="\\%1"
)

echo %arg%
pause

FOR /F %%A IN (%arg%) DO (
echo %%A
set zzz=%%A
set zzz=%zzz:\=%
ping %zzz%
dir %%A\c$
)

Instead of bending over backwards to create a variable that will cause a
common chunk of code to behave in various ways depending on input
parameters, make that common chunk a subroutine and call it differently in
those different cases, i.e.:

IF "%1"=="" (
FOR /F %%A IN ('NET VIEW ^| FIND "\\"') DO call:mysub %%A
) ELSE (
call:mysub \\%1
)
pause
goto:eof

:mysub
echo %1
set zzz=%1
set zzz=%zzz:\=%
ping %zzz%
dir %1\c$
goto:eof


/Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top