Context
I’m trying to write myself a script that would toggle my WLAN adapter (enable/disable it) depending on a condition. The script should disable the adapter if it’s currently enabled or, conversely, enable it if currently disabled. This is what I’ve come up with so far:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%comspec% /C devcon status PCI\VEN_168C > C:\devstat.txt", 0, false
Return = WshShell.Run("findstr disabled C:\devstat.txt", 0, true)
WScript.Echo Return
If Return = 0 Then
WshShell.Run "devcon " & "enable PCI\VEN_168C", 0, false
Else
WshShell.Run "devcon " & "disable PCI\VEN_168C", 0, false
End If
Explanation of the script
Line 1 is quite obvious, so I’m skipping that part. Line 2 executes devcon to check the status of my WLAN adapter (hardware ID PCI\VEN_168C) and dump the output to C:\devstat.txt. Line 3 runs findstr to check whether C:\devstat.txt contains “disabled“. If “disabled” is not found, findstr should return errorlevel > 0, otherwise errorlevel == 0 (zero). The rest of the script is just if statements based on the value of Return (which is supposed to hold the value of errorlevel).
Problem
Value of Return is always 1 whether C:\devstat.txt contains “disabled” or not.
What am I missing here?
Final Edit
Thanks to barlop’s hint, I managed to figure out a workaround. It turns out that the culprit is Windows Scripting Host. The script has to be paused for several miliseconds after executing Line 2, so here is what the final script should look like:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%comspec% /C devcon status PCI\VEN_168C > C:\devstat.txt", 0, false
WScript.sleep 400
Return = WshShell.Run("findstr disabled C:\devstat.txt", 0, true)
Rem WScript.Echo Return
If Return = 0 Then
WshShell.Run "devcon " & "enable PCI\VEN_168C", 0, false
Else
WshShell.Run "devcon " & "disable PCI\VEN_168C", 0, false
End If
There is no problem with findstr..
Run this code, which is the same code as yours but with wscript.echo “WAIT” just before the findstr line. Now when that wscript.echo “WAIT” line executed, open the file, you will probably see it doesn’t contain disabled, now write disabled in it, and save it. Then click OK to the message that says wait. And the program continues.
I get the correct result, from the errorlevel returned by the findstr command. That is
1 for when it doesn’t contain disabled
0 for when it does
You can also try for the sake of troubleshooting, to make the program simpler to find the error. So you could try removing that line, to test findstr, then you may have found findstr was fine. Looking at the file just before findstr, and amending it manually, shows it too without having to remove that line.
I tried also changing Return to Retur ‘cos I thought maybe Return was a keyword and thus won’t quite work, but it works fine with the variable name of Return which you used.
So the issue is
but findstr is fine. And I think you were right to use True as the 3rd parameter in
WshShell.Run(“findstr xbc C:\getmail\a.a”, 0, true)
since from script56.chm the documentation ” If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).”