0

I have a batchfile script that I wrote as an interface for connecting to my network shares. At this point it’s very simple. It has a list of my shares that I would like to compare to network shares in use and disable that share as option if it’s found. The following code snippet is part of a for loop that iterates over the number of shares in the list and lists them.

setlocal enabledelayedexpansion
set list[0]="\\xxx.xxx.x.xx\photo"
set list[1]="\\xxx.xxx.x.xx\photo 2"
for /l %%n in (0,1,2) do (
rem wmic netuse get remotename |findstr /C:!list[%%n]!
rem if %errorlevel% neq 0 do(command 1) else (command 2)
echo  %%n   !list[%%n]! 
)

The rem above is removed for testing the problem. The thought here is to use if %errorlevel% condition to catch the match. The shares are echoed with double quotes. If for example photo is mounted, both photo and photo 2 will be matched which is undesirable. Since the shares have similar names and added number with space I need to compare the strings exactly so I have tried with findstr /x switch but this doesn’t work at all. Not sure if the entry with double quotes interferes. Removing the double quotes in the list yields an error that number after the space cannot be opened. Am I approaching this in a correct way?

Enigma86
  • 11
  • 4
  • 2
    Delayed Expansion must also be used the with the `%errorlevel%` variable. – Squashman Dec 28 '20 at 17:17
  • 3
    Does this answer your question? [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – T3RR0R Dec 28 '20 at 17:17
  • 3
    Within a parenthesised series of instructions (aka "code block") any `%var%`, including `%errorlevel%`, is replaced by the then-current ("parse time") value of that variable when the block syntax is being validated, hence `delayedexpansion` and `!var!` needs to be used to access the *current* value of the variable, including *errorlevel*. The syntax `if [not] errorlevel n` may also be used, meaning `if the errorlevel is [not] "n" OR GREATER THAN "n"`. Also, assigning quoted strings to variables make the variables hard to combine logically. Inserting quotes as needed is far simpler. – Magoo Dec 28 '20 at 19:08
  • 2
    BTW: Delayed expansion would not be needed at all if you change the for loop to `for /F "tokens=2 delims==" %%i in ('set list[') do %SystemRoot%\System32\wbem\wmic.exe netuse get remotename | %SystemRoot%\System32\findstr.exe /C:%%i && command 2 || command 1`. See [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). I suggest to run in command prompt window also `findstr /?` and read the output help, especially about usage of the options `/G`, `/V` and `/L`. The task could be done most likely even more efficient. – Mofi Dec 28 '20 at 19:23

1 Answers1

1

May I suppose a slightly different approach?

@echo off 
setlocal enabledelayedexpansion
set "list[0]=\\xxx.xxx.x.xx\photo"
set "list[1]=\\xxx.xxx.x.xx\photo 2"
for /l %%n in (0,1,2) do (
  net use |findstr ilc:"  !list[%%n]!  " >nul && (
    echo found "!list[%%n]!", do command1
  ) || (
    echo no match for "!list[%%n]!" do command2
  )
)

Changes to your code:

  • using net use instead of wmic, as wmic outputs Unicode, which we would have to handle. And net use is a lot faster.
  • using conditional operators && and || instead of errorlevel
  • made findstr more secure and
  • discarded findstr's output.
  • corrected the set syntax to not include the quotes to the values.
Stephan
  • 53,940
  • 10
  • 58
  • 91