I am coding a batch script to turn off Green-Ethernet in network adapter properties.
I've written following code:
#echo off
cls
for /F "skip=3 tokens=1,2,3* delims= " %%G in ('netsh interface show interface') DO (
IF "%%H"=="Disconnected" netsh interface set interface "%%J" enabled
IF "%%H"=="Connected" netsh interface set interface "%%J" disabled
echo %%J
powershell.exe Set-NetAdapterAdvancedProperty -Name '%%J' -DisplayName 'Green-Ethernet' -DisplayValue 'Disabled'
)
My windows language is set to german, it raises this error:
Set-NetAdapterAdvancedProperty : No matching display value found. The following are valid display values: Deaktiviert,
Aktiviert
In Zeile:1 Zeichen:1
+ Set-NetAdapterAdvancedProperty -Name 'Ethernet' -DisplayName 'Green-E ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MSFT_NetAdapter...CC2156B75}:...):ROOT/StandardCi...ertySettingData) [
Set-NetAdapterAdvancedProperty], CimException
+ FullyQualifiedErrorId : Windows System Error 87,Set-NetAdapterAdvancedProperty
So if I replace "Disabled" with "Deaktiviert" it works, but why does "Disabled" not work? Doesn't powershell accept english arguments on windows whose language is set to german?
Because the executable (in this case, netsh) understands English and only English but the messages are translated to the system language for human interpretation.
Suppose the executables were to be able to accept any possible language. There would need to be possibly hundreds of different ways of expressing each and every possible parameter, like show in your example, for instance. The example instruction could be expressed in millions of different ways - interface for instance is used twice. Why couldn't you use Dutch for the first and Estonian for the second?
Even batch itself - or any programming language, would need to be able to accept any of hundreds of translations of if, not, echo, for and so on. It's simply impractical. One common string meaning one common thing, even if it's gobbledegook in any particular language - like cls is in English for instance.
Users, on the other hand, have the unreasonable attitude that they want to be able to understand the responses, so the response is tailored for the user's language. This is not simply a matter of word-for-word translation - different languages use different word-orders.
Consequently, the output that is intended to be interpreted by humans becomes a mixture of the human's language and an English-ish language, such as In Zeile:1 Zeichen:1 and CategoryInfo InvalidArgument in your example.
There's another aspect to this issue - word-order. We have a problem with dates and times. Different representations depending on language and other user-settings, like d(d)/m(m)/(cc)yy against m(m)/d(d)/(cc)yy against ccyy/mm/dd + all the variations in separators. Time may have different separators, may be in 12 or 24-hour format, may have the am/pm not only as different strings, but also as string that may be separated from the numeric portion by a space, or not and with or without dots.
So, in all, your code needs to customise its interpretation of the responses that it is receiving according to the language in which it is receiving the response.
What would be a really good idea would be to allow a switch to have the response from a command to be generated in a standard way, regardless of user-language like
:: Responds in user-language
dir
set babel on
:: Responds in standard language (Probably English)
dir
set babel off
:: Back to user-language
dir
But the chances of having this are zero. Microsoft has asked for user-suggestions about enhancing the batch language years ago - so long that I've lost any links I had - but nothing has ever been converted from user-suggestion to language-change AFAIAA.
Related
I am trying to make a if statement for a CLI i am making in batch. this is the code
if "%command%" == "browser"(
echo Warning, this will only change the default broswer for this session
echo What would you like the default browser to be changed to?
set /p browserdefault=
)
I want it to check "browserdefault" and see if it is chrome, edge or a diffrent broswer
if it is none of those it should say "error, invalid broswer."
is there any way for there to be a if statement inside of the already existing if statement? like if %browserdefault% is "chrome" start C:%username%\filepath\tochrome?
I tried many diffrent ways but none of them seems to work
As pointed out in the other answer this is Windows batch. I do not know a way of nested if statements.
I would use call statements to get rid of the nested if statements like this:
:getCommand
set /P command=Enter command:
call :%command%
goto :getCommand
:browser
set /P browserdefault=Enter browser:
if not "%browserdefault%"=="chrome" if not "%browserdefault%"=="edge" echo error, invalid broswer.
goto :eof
What you are trying to do is right, noble and good. However, this is Windows batch. The ordinary rules of style & good taste do not apply.
You should not try to nest other statements inside a batch command. There are times this will appear to work, but (based on a number of factors, such as a poorly considered logical condition, or even different input strings!) will occasionally cause the block to fail miserably. This will set you up for a world of painful debugging.
The suggestion in the comments to search for menu is a good one. This should lead you to code that will have (what looks like) many unnecessary & unrelated blocks, all sprinkled with horrible gotos. Just... it's fine. This is Windows batch. It will work, it will be reliable. Try not to think about the style too much.
There are some example menus in this excellent Q&A:
Menus in Batch File
(as noted in at least one answer there, when using the naive if ERRORLEVEL 1, this will actually check if the number is equal OR greater than which requires starting with a higher ERRORLEVEL and working down to IF ERRORLEVEL 0, with a GOTO jump for each check -- using a statement with a % variable such as if "1" == "%ERRORLEVEL%" or similar avoids this pitfall).
Note: this question is not about dart but the windows registry.
I've implemented a library and tooling (called dcli) to write cli apps in the dart language.
When a user types the name of a dart script on the command line I need windows to start dcli and pass the dart script and any command line arguments.
e.g.
hellow.dart a b c
Will result in the following command being run
dcli.bat hellow.dart a b c
I need to do this via the 'C' registry api (I'm calling the registry api from dart using its foreign function interface (ffi) which allows it to call C entry points). This part of the problem is already solved and I can successfully add registry keys from dart.
My problem is knowing what registry settings to create because, particularly with the advent of windows 10, the documentation is a mess.
This is my rather poor attempt so far.
// create a ProgID for dcli 'noojee.dcli'
regSetString(HKEY_CURRENT_USER,
r'\Software\Classes\noojee.dcli',
defaultRegistryValueName, 'dcli');
// associate the .dart extension with dcli's prog id
regSetString(HKEY_CURRENT_USER, r'\Software\Classes\.dart',
defaultRegistryValueName, 'noojee.dcli');
regSetString(HKEY_CURRENT_USER,
r'SOFTWARE\Classes\.dart\OpenWithProgids',
'noojee.dcli.dart', '');
// this path doesn't look correct
regSetExpandString(
HKEY_CURRENT_USER,
r'dcli\shell\open\command',
defaultRegistryValueName,
'c:\path\to\dcli.bat %1 %2 %3 %4 %5 %6 %7 %8 %9');
regSetString(
HKEY_CURRENT_USER,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.dart\OpenWithList',
'a',
'dcli.bat');
regSetString(
HKEY_CURRENT_USER,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.dart',
'MRUList',
'a');
regSetNone(
HKEY_CURRENT_USER,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.dart\OpenWithProgids',
'noojee.dcli');
One thing that confuses me is the use of HKEY_CLASSES_ROOT.
The doco appears to say that it is read only but then goes on to show examples creating keys under it.
I have the added complication that vscode with the dart-code extension likes to add its own association (which makes no sense) and I need to override this if it exists but ideally make it an alternative on the explorer menu.
HKEY_CLASSES_ROOT is a virtual merged view of HKEY_CURRENT_USER\Software\Classes and HKEY_LOCAL_MACHINE\Software\Classes (on Win2000 and later). It is not read-only but writing to it is not the best idea because writes usually go to the machine part except the times it does not.
The FileExts key is undocumented, don't write to it.
In your code, r'dcli\shell\open\command' is incorrect, the first component in the classes key should be the progid the extension points to, try r'SOFTWARE\Classes\noojee.dcli\shell\open\command'.
It basically goes like this:
HKEY_CURRENT_USER\Software\Classes\.myext = myprogid
HKEY_CURRENT_USER\Software\Classes\myprogid\shell\verb\command = "c:\path\app.exe" "%1" (where %1 is replaced by the path of the file, %2 is only used for printers).
Thanks to #Anders I arrived at the following:
Note: defaultRegistryValueName is just an empty string ''.
const progIdPath = r'Software\Classes\.dart\OpenWithProgids';
if (regKeyExists(HKEY_CURRENT_USER, progIdPath)) {
regDeleteKey(HKEY_CURRENT_USER, progIdPath);
}
regCreateKey(HKEY_CURRENT_USER, progIdPath);
regSetString(HKEY_CURRENT_USER, progIdPath, 'noojee.dcli', '');
const commandPath = r'Software\Classes\noojee.dcli\shell\open\command';
if (regKeyExists(HKEY_CURRENT_USER, commandPath)) {
regDeleteKey(HKEY_CURRENT_USER, commandPath);
}
regCreateKey(HKEY_CURRENT_USER, commandPath);
regSetString(
HKEY_CURRENT_USER,
commandPath,
defaultRegistryValueName,
'"${DCliPaths().pathToDCli}" '
'"%1"%*');
The only issues is that the %* doesn't seem to be doing anything useful.
From the doco I've found it should expand any no. of args (e.g. more than 9) however I only ever get 8 (as %1 is the command itself) and "%1" yeilds the same result as "%1"%*
Using Actuate eReport Designer Professional 9 SErvice Pack 3 Fix 2
I am attempting to set a text control's ValueExp property to display a string consisting of a division result concatenated with some static text. I want the division result to display as an integer if there is no remainder. Otherwise, I want only 1 decimal place.
There will be conditional logic involved, but I will be able to handle that. What I am really looking for is, using the Expression Builder only, can I format numbers. For example, how would I get the expression, 5/3 & " text" to display 1.7 text? This guess,
round(5/3, 1) & " text"
threw errors for "illegal variable use (round)" and "operator not found for these types"
From Dominique's answer, this effort:
BirtMath.round(5/3, 1)
resulted in an illegal variable use on BirtMath.
Try this:
BirtMath.round(5/3, 1) + " text"
(tested on BIRT Eclipse designer, this should be the same with actuate professional designer)
What finally got the job done was:
Format(5/3, "##.#") & " text"
Dominique's answer refers to BIRT, which is a completely different technology/product.
If you want to display a numeric value with special formatting, a Text Control is not the best choice here. Instead, you should use a numeric control of the appropriate type (for example, a Double Control) and either override the GetText() method of that control to handle the display formatting or use Conditional Formatting. The reason this is a better solution is that data search and export will not work properly with a Text Control.
Note that you can use a format pattern like this: "#,##0 \T\e\x\t" instead of concatenating the string; this technique is needed when you are using Conditional Formatting.
Personally, I would prefer you to use Conditional Formatting, because I put a lot of effort into designing that feature of e.Reports, and I'd like to see more people using it. :) But overriding GetText() is probably easier in your specific situation, due to the need to do more complex string manipulation to eliminate the trailing decimals.
I want to add that with BIRT, you can also format the text in a more 'graphic' way, without the need for SQL ... in case it is useful to someone ...
Select the Object - Properties - Format Number - You choose the Format
there is variety for each case and in addition to adding the custom format.
Of course, it is everyone's decision to how format the text, and it is always good to know how it is done in different ways!
Hello I have 2 batch files, it works perfectly on one machine but not so perfect on another.
Here is the code.
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "%ln:&="&set "%"
set realuser=%user:"=%
echo %realuser%
So on my machine it shows like this:
echo Liam
Liam
On the other machine it shows like this:
echo "=
"=
It's the exact same machines only difference is one is running Windows 8 (working) and the other windows 7 ("=)
EDIT:
Thank you all for the answers, I managed to solve this by editing the way the userl.txt file is generated to make it display just the name, e.g "Liam" without quotes. Then use this
set /p user=
That seems to work for what I need s there is only 1 value ever going to be in that file.
Thank you all!
A simple echo %thisvariabledoesnoexist:"=% will show the same result.
The reason for the observed output is that the variable %user%, that seems that have to been assigned a value in the set "%ln:&="&set "%" line, did not get any value.
The problem is probably that the input line does not contain the required value or the format of the input line is different from the expected one.
As MC ND has pointed out, the critical point is that variable user must be defined to work properly. If it is not defined, then you get your problem result.
I don't see how identical user1.txt files being processed on two machines with identical batch scripts can possibly give different results as you describe.
Strike that, I can think of one way, but it is a long shot. The assumption is the first two lines are supposed to define the user variable, perhaps along with other variables. But suppose the first two lines do not define the user variable on either machine. Perhaps user is already defined on the machine that "works" before the script is even run, and it is not defined on the other machine. That is the only thing I can come up with that would yield the result you describe.
I do see one thing that concerns me in your code. The following line of code implies that sometimes you get quotes in your input.
set realuser=%user:"=%
You state that quotes in your value interfere, so you remove them. But you are removing them too late! The prior line may not set all the values properly if there are quotes in the value of ln.
Try the following:
set /p "ln=" <"C:\LoginSystem\userl.txt"
set "ln=%ln:"=%"
set "%ln:&="&set "%"
echo %user%
The docs for MAKELANGID specify that MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) Means 'Language neutral'.
This seems to be English on my machine (tried it with FormatMessage), but what does it mean in general? Is it guarenteed to be English?
Thanks!
I would expect that this means that the strings associated with the lang id are not specific to any language - which could be useful to know for a localisation team. "%1 + %2 = %3" would be an example of one such string.
with sublanguage = SUBLANG_DEFAULT this would be the user's default language.
https://web.archive.org/web/20100704043524/http://msdn.microsoft.com/en-us/library/ms534732(VS.85).aspx
Here's a note on the sublanguage identifier - https://web.archive.org/web/20100728153356/http://wiki.winehq.org/SublangNeutral.
Note that MAKELANGID creates a language identifier for you from the primary language and sublanguage identifier - it does "not" get the default language, or anything like that.
No, it is not "gauranteed to be English." It "is" whatever you place into it at that point (English, in your case). But it means that it should not serve as a (language) satellite assembly (except maybe as a fallback).