Translation of a DOS batch file in PowerShell

I am trying to translate a .bat file into PowerShell and don't understand what a few code snippets do:

set MY_VARIABLE = "some\path\here" "!MY_VARIABLE:\=/!" 

What makes line 2 higher? In particular, I don’t understand what does: \ = /, since I saw the else variable, where the reference code has MY_VARIABLE !.

Another point of confusion is the code below.

 set SOME_VARIABLE=!SOME_ARGUMENTS:\=\\! set SOME_VARIABLE=!SOME_ARGUMENTS:"=\"! 

Also can you tell me what happens on lines 3 and 4 above?

What would the variables below change in PowerShell?

 set TN0=%~n0 set TDP0=%~dp0 set STAR=%* 

Any help on this is greatly appreciated. Thanks.

+4
source share
3 answers

!var:find=replace! is string substitution for an extended delay variable.

http://www.robvanderwoude.com/ntset.php#StrSubst

When you use ! instead of % for a variable, you want DOS to do the replacement of variables at runtime (which you probably think it does with % , but it doesn't). With the help of % variable is replaced with the fact that the command is analyzed (before its launch) - therefore, if the variable changes as part of the command, it will not be visible. I think some are switching to use ! all the time because it gives "normal" behavior.

Here you can find out more about the delayed extension.

http://www.robvanderwoude.com/ntset.php#DelayedExpansion

+4
source

The first two commands set variableName= use modifiers to expand the name of the batch file, represented as %0 .

%~n0 extends it to the file name and %~dp0 extends it to include the letter and path to the disk.

The last one, %* , represents all the arguments passed to the batch file.

Further information can be found here .

+2
source

Exclamation marks (!) I n DOS batch files reference an intermediate value, useful if you are in a for loop. If you used% instead of (in a loop), it would return the same value over and over.

Lines 3 and 4 set "SOME_VARIABLE" to the intermediate value "SOME_ARGUMENTS: \ = \" and SOME_ARGUMENTS: "= \" respectively. Again, I assume these lines are taken from a loop.

Regarding variable assignments, Powershell variable assignments work as follows:

  $myVariable = "my string" 

~ dp0 (in the DOS batch) is converted to the path (with drive letter) of the current bat file. You can get this in Powershell by doing get-location.

Why someone had to set the variable for STAR (*) is outside of me, so I assume there is some encoding problem or for some other reason that they cannot just use an asterisk.

~ n0 I'm not sure; maybe someone else knows what it is.

+1
source

Source: https://habr.com/ru/post/1384140/


All Articles