"Garry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 15 Nov 2005 14:59:46 -0500, "Matt Williamson"
> <(E-Mail Removed)> wrote:
>
>>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 "\\"'
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
|