CLI - Ignore a line when using the for command - parsing

I'm trying to set a variable which will identify the make of a laptop.
I am doing this by using the command
wmic csproduct get vendor
This gives the following output
Vendor
LENOVO
(blank)
So based on that command, I have used the for command in the way below, to try and set a variable with the value 'LENOVO'
for /f "skip=1 tokens=1 delims=" %i in ('wmic csproduct get vendor')
do set vendor=%i
however, problem is that the output of the wmic command actually produces a blank line, under the word LENOVO, so my variable gets set as a blank value. Is there anyway to stop the for command from parsing this 3rd line, therefore stopping once the variable has been set with the value of 'lenovo'?
The skip function works fine, and bypasses the first line completely. However it doesn't seem to give me the option to say for example, skip lines 1 and 3 but leave 2. I have experimented with the EOL parameter to try and ignore the blank line, but the for command still reads the empty 3rd line each time.
Many Thanks

A little whacky, but try using the following within your line. It will filter out the Vendor line and then sort alphabetically, putting the blank line first. If you really need to do stuff like this regularly, check into a Windows port of some Unix utils like sed and awk.
wmic csproduct get vendor | find /v /i "vendor" | sort

Related

How can I paste multiple line command with Docker interactive mode but not evaluating?

For example I am opening an interactive command line window.
docker exec -it container rails c
When I paste a multiple-line command, it evaluate every line instead of pasting a block. How can I configure it as not to evaluate?
Order.last
.user
.id
Ah, simple trick here: type begin + enter, paste the code, type end:
begin
Order.last
.user
.id
end
It cannot and should not be configurable. The reason is that the console cannot posisible know when you have finished typing the whole command, as the first line is a completely valid ruby statement. Things are slightly different if you adapt different coding style:
Order.last.
user.
id
The code above will work once copied to console (but gosh, I really hate that style!) because it is clear to the interpreter that the statement is not completed yet - similarly to missing closing bracket or quote. Wrapping the code in begin/end have the same effect - it tells the interpreter that the code is not yet completed, so it awaits for the final end.

post-build event with multiple if/copy combinations only execute if first file does not exist

Given the bin\ directory inside the Delphi project contains the files Cert.pem and Key.pem, the below Delphi post-build event only copies both files if C:\Binaries\Cert.pem does not exist:
if not exist $(OUTPUTDIR)Cert.pem (copy bin\Cert.pem $(OUTPUTDIR))
if not exist $(OUTPUTDIR)Key.pem (copy bin\Key.pem $(OUTPUTDIR))
As soon as C:\Binaries\Cert.pem exists, the Key.pem file is never copied.
How can I solve this in the post-build event?
Edit: unlike my 2014 post, this is indeed possible using parentheses. See my answer below.
The problem with Delphi post-build events is that they are not batch files.
It means that statements that look like lines are being concatenated by the Delphi IDE into one big & ampersand separated statement. This ensures the commands are executed in sequence, as per Command Redirection, Pipes - Windows CMD - SS64.com:
commandA & commandB Run commandA and then run commandB
So this is the actual statement that gets executed:
if not exist $(OUTPUTDIR)Cert.pem (copy bin\Cert.pem $(OUTPUTDIR))&if not exist $(OUTPUTDIR)Key.pem (copy bin\Key.pem $(OUTPUTDIR))
The problem here is that now the second if is seen as a continuation of the "then" part of the first if statement: the second if never executes when the $(OUTPUTDIR)Cert.pem exists.
What helps is a little known feature that you can wrap each command inside parentheses. Normally this is to allow one command to span multiple lines (especially for if, and for..do loops), but it also works on one line.
Wrapping each line having an if statement inside parentheses ensures they become standalone statements not affecting the other lines, even if they are being concatenated with & ampersand separators.
In the dialog it looks like this:
(if not exist $(OUTPUTDIR)Cert.pem (copy bin\Cert.pem $(OUTPUTDIR)))
(if not exist $(OUTPUTDIR)Key.pem (copy bin\Key.pem $(OUTPUTDIR)))
That way, the IDE translates it into one statement:
(if not exist $(OUTPUTDIR)Cert.pem (copy bin\Cert.pem $(OUTPUTDIR)))&(if not exist $(OUTPUTDIR)Key.pem (copy bin\Key.pem $(OUTPUTDIR)))
Now it works as intended:
When $(OUTPUTDIR)Cert.pem exists but $(OUTPUTDIR)Key.pem does not, only $(OUTPUTDIR)Cert.pem is copied
When $(OUTPUTDIR)Cert.pem does exists but $(OUTPUTDIR)Key.pem does, only $(OUTPUTDIR)Key.pem is copied
when neither exist, both are copied
when both exist, neither are copied
I did not know this "trick" when writing my 2014 post Delphi prebuild/prelink/postbuild events, so I need to write an update for it.
Searching for batch file parentheses site:microsoft.com -site:social.technet.microsoft.com -site:answers.microsoft.com did not reveal it in the official documentation, but I am not surprised as it grew hysterically, instead of being designed. Or like the Old New Thing attributes h2g2:
Much like the universe, if anyone ever does fully come to understand Batch then the language will instantly be replaced by an infinitely weirder and more complex version of itself. This has obviously happened at least once before ;)
The best documentation I could find was at Parenthesis/Brackets - Windows CMD - SS64.com:
Parenthesis can be used to split commands across multiple lines. This can make code more readable. Variables will be evaluated for the code block just as if the command was a single line.
(command)
(
command
command )
Things that break inside parenthesis
The CMD shell does not use any great intelligence when evaluating parenthesis, so for example the command below will fail:
IF EXIST MyFile.txt (ECHO Some(more)Potatoes)
...
Use multiple build events instead of putting both commands in the same event.
Executing the source lines of a build event.
I can cut this short very easily: build events are not batch files.
What happens is that all lines in your build event are concatenated together using ampersand (&) signs which are used to execute multiple commands on one command line.
This means that all the fancy control structures (if statements, setlocal, for loops) are not possible inside build events.
ref: Pasted from a blog post: Delphi prebuild/prelink/postbuild events written by Jeroen W. Pluimers
Makes me wonder why you asked since it looks like you wrote the answer in 2014. :)

How to execute MSBuild from Delphi?

I'm working on automating our software's build / deploy process. One major piece of this is executing msbuild to compile multiple Delphi projects.
Following numerous resources such as this one, I can do it successfully from the RAD Studio Command Line (which is simply calling rsvars.bat to set some environment variables). However, when trying to automate both this batch file and the msbuild command from Delphi, I cannot figure out how to proceed.
The key of the issue is that the batch file and the actual msbuild command are two entirely separate commands - although they need to be run together under the same environment. I found this question somewhat related, but I don't see a clear answer for my scenario.
How can I execute msbuild from Delphi while first preparing it with the environment variables as found in rsvars.bat?
As this answer showed, you can run cmd.exe itself with command-line parameters to execute commands.
Per the cmd.exe documentation:
Syntax
cmd [/c|/k] [/s] [/q] [/d] [/a|/u] [/t:{<B><F>|<F>}] [/e:{on|off}] [/f:{on|off}] [/v:{on|off}] [<String>]
Parameters
/c
Carries out the command specified by String and then stops.
...
<String>
Specifies the command you want to carry out.
...
Remarks
Using multiple commands
To use multiple commands for <String>, separate them by the command separator && and enclose them in quotation marks. For example:
"<Command>&&<Command>&&<Command>"
Processing quotation marks
If you specify /c or /k, cmd processes the remainder of String, and quotation marks are preserved only if all of the following conditions are met:
You do not use /s.
You use exactly one set of quotation marks.
You do not use any special characters within the quotation marks (for example: & < > ( ) # ^ | ).
You use one or more white-space characters within the quotation marks.
The String within quotation marks is the name of an executable file.
If the previous conditions are not met, String is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.
So try using CreateProcess() or ShellExecute/Ex() to run an instance of cmd.exe with these parameters:
cmd.exe /C ""<path>\rsvars.bat" && msbuild "<path>\project" <msbuild parameters> ..."
Alternatively, you can have your app load rsvars.bat and parse out the values it defines (or just define the values in your own code), and then execute msbuild using CreateProcess(), passing the desired environment variables to its lpEnvironment parameter. This way, you do not need to actually execute rsvars.bat at all.
Another way which works is to put both commands together into another new batch file. This also allows you to build multiple projects using the same environment. For example DoBuild.bat with the following contents:
call "C:\Program Files (x86)\Embarcadero\Studio\17.0\bin\rsvars.bat"
msbuild "<path>\MyProject.dproj"
msbuild "<path>\MyOtherProject.dproj"
msbuild "<path>\YetAnotherProject.dproj"
Then, just execute your batch file:
Cmd.exe /K "<path>\DoBuild.bat"

How to specify $* in a parameter for a Jenkins job?

I have a Jenkins job that has a test.excludes parameter that I would like to default to **/*$* (ie exclude all inner classes). Normally I would specify this value in a file, but in this case, I don't want to submit any files since this is investigative work (as I see tests that are failing, I will add them to test.excludes).
The problem is that the $* in **/*$* is being expanded to the command line variables. Using **/*$$* only changes the problem to $$ being expanded to the pid. Escaping * using \ doesn't work (inner classes are still run). Escaping by wrapping the entire value in ' does nothing to prevent the $* from being replaced.
Is there a way to get the behavior I want?
The following really ugly expression works: $(echo '**/*$*').

ZSH `rvm-prompt` on RHS prompt not refreshing

I have rvm-prompt feeding into my RPrompt, however it is not refreshing between commands: (Larger image)
For example, when I cd from one ruby project to another with an .rvmrc file pointing to a new gemset, the rprompt simply will not refresh. It appears that it must be caching the rprompt for performance purposes, so I am curious as to how I can force a refresh for zsh at each command?
How do you generate the prompt? I do it like this:
local rvm_ruby=' %{$fg[red]%}[$(~/.rvm/bin/rvm-prompt i v g s)]%{$reset_color%}'
And then use the rvm_ruby variable in my prompt:
PROMPT="${user_host}${directory}${git_branch}${rvm_ruby}%B
→%b "
EDIT: Note that the place where you create the contents for the variable needs single quotes, otherwise the command will get substituted right away and not updated anymore. This initially took me a bit to figure out. You may have the same problem defining your RHS prompt.

Resources