PC Review


Reply
Thread Tools Rate Thread

conditional argument for FOR loop?

 
 
Matt Williamson
Guest
Posts: n/a
 
      15th Nov 2005
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


 
Reply With Quote
 
 
 
 
Garry
Guest
Posts: n/a
 
      16th Nov 2005
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 "\\"'

Garry
 
Reply With Quote
 
billious
Guest
Posts: n/a
 
      16th Nov 2005

"Matt Williamson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a
 
      17th Nov 2005

"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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the best way to loop through a 2-column range argument ofnumbers in a UDF? Greg Microsoft Excel Programming 3 8th Apr 2009 06:12 PM
How to get a conditional argument to check all cells in a array? =?Utf-8?B?SnVzdCA0IFRvZGF5?= Microsoft Excel Worksheet Functions 1 9th Dec 2006 05:17 PM
Can I HIDE cells with a conditional argument? =?Utf-8?B?Qm9iIHRoZSBCdWlsZGVy?= Microsoft Excel Worksheet Functions 2 22nd Jul 2005 10:30 AM
Function (array argument, range argument, string argument) vba Witek Microsoft Excel Programming 3 24th Apr 2005 03:12 PM
COUNTIF function with conditional argument Richard Crum Microsoft Excel Worksheet Functions 2 25th Nov 2003 01:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:30 AM.