Reputation: 13
I am trying to write to batch files one that will pull the computer info and export it to a text file ie output.txt I have that one correct.
wmic CSProduct get vendor >> c:\output.txt
wmic CSProduct get name >> c:\output.txt
That gives me an output.txt that contains
Vendor
Gigabyte Technology Co., Ltd.
Name
H61N-USB3
Now I need a batch file that will turn lines 2 and 4 into variables. I have tried
set "Name="
for /F "skip=1" %%a in (C:\output.txt) do if not defined Name set "Vend=%%a"
set "Name="
for /F "skip=3" %%b in (C:\output.txt) do if not defined Name set "Name=%%b"
REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation /V Model /T REG_SZ /D "Vend, Name" /F
But I cannot get it to work. I am completely new to batch scripts and have no clue what I am doing.
Upvotes: 1
Views: 166
Reputation: 14340
Stephan is the best solution if you ask me but here is another option.
for /F "tokens=1,2 delims==" %%G IN ('wmic CSProduct get vendor /value ^|find /i "vendor"') do SET %%G=%%H
for /F "tokens=1,2 delims==" %%G IN ('wmic CSProduct get name /value ^|find /i "name"') do SET %%G=%%H
echo %Vendor% %Name%
Upvotes: 0
Reputation: 56238
without the need of a temporary file:
for /f "tokens=1,2 delims=," %%a in ('wmic CSProduct get vendor^,name^,version /format:csv') do (
set vend=%%a
set name=%%b
)
echo -%vend%-%name%-
wmic
has ugly line endings. There are several ways to trick them out. Here I choosed to use an additional token (that "contains" the line ending), that is not used (Version)
Upvotes: 3
Reputation: 24329
You're actually pretty close.
Set "Name="
should have been Set "Vend="
.for
line should likewise have referred to not defined Vend
instead of not defined Name
.delims=
to read the entire input line. (You can see more details on "for" options by just typing for /?
at your command line.)reg add
line, the %%a
and %%b
should have been the environment variables you created, %Vend%
and %Name%
respectively.So, this should work:
set Vend=
for /F "skip=1 delims=" %%a in (C:\output.txt) do if not defined Vend set "Vend=%%a"
set Name=
for /F "skip=3 delims=" %%b in (C:\output.txt) do if not defined Name set "Name=%%b"
REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation /V Model /T REG_SZ /D "%Vend%, %Name%" /F
Upvotes: 0
Reputation: 80211
set "Vend="
for /F "skip=1" %%a in (C:\output.txt) do if not defined Vend set "Vend=%%a"
set "Name="
for /F "skip=3" %%b in (C:\output.txt) do if not defined Name set "Name=%%b"
echo(%Vend% %Name%
If you want the values found for vend
and name
in your reg
line, then use %vend%
and %name%
Essentially, skin #lines, assign the vaue to the correct variable. For each line thereafter, since the variable is then set
, don't make any assignment.
Ah - you'd probably want the entire line, including spaces.
change "skip=1"
to "skip=1delims="
(change the #skipped to suit)
This assigns the entire line read to the metavariable %%a
by disabling the delimiters. The default delimiters ar (SpaceTab,;) so only the first "token" (up to the first delimiter found) would be assigned.
Upvotes: 0