Matt Williamson
11-15-2005, 07:59 PM
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
Garry
11-16-2005, 12:27 AM
On Tue, 15 Nov 2005 14:59:46 -0500, "Matt Williamson"
<ih8spam@spamsux.org> 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 "\\"'
Garry
billious
11-16-2005, 01:42 AM
"Matt Williamson" <ih8spam@spamsux.org> wrote in message
news:uPe8x7h6FHA.3544@TK2MSFTNGP09.phx.gbl...
> 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
Al Dunbar
11-17-2005, 02:36 AM
"Garry" <garry@nospam.com> wrote in message
news:mjukn19d6ce1dnv1fq8bh39svpluidt64u@4ax.com...
> On Tue, 15 Nov 2005 14:59:46 -0500, "Matt Williamson"
> <ih8spam@spamsux.org> 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