pollekowski
Goto Top

Zusammengesetzte Variable in FINDSTR

Versuche vergeblich eine zusammengesetzte Variable an ein Findstr zu übergeben.

Hallo Ihr Götter face-smile

Ich versuche für ne unattented Install die Rechnertypen zu definieren und will dafür eine Variable "zusammensetzen".
	findstr /C:"%Modell%%count%"" %LogFile% >NUL  
Es soll dann folgendes ausgeführt werden:
findstr /C:"HP Compaq dc5700"" %LogFile%  
findstr /C:"HP Compaq dx2300"" %LogFile%  
.
.

Nach ewigen versuchen die Variablen irgendwie zu übergeben bin ich nun recht ratlos face-sad
Ich bekomme %Modell%%count% einfach nicht zusammengesetzt und wieder als Variable in den "findstr".

@echo ON
md c:\temp

set LogFile=C:\temp\SystemInfo.log
del %LogFile%

set count=1
set AnzahlModelle=11
set Modell1=HP Compaq dc5700
set Modell2=HP Compaq dx2300
set Modell3=HP Compaq dx2400
set Modell4=HP Compaq dx2420
set Modell5=HP Compaq dx2450
set Modell6=HP Compaq dx5150
set Modell7=HP Compaq xw4300
set Modell8=HP Compaq xw4400
set Modell9=HP Compaq xw4600
set Modell10=HP Compaq 6730b
set Modell11=VMware Virtual Platform

Systeminfo.exe > %LogFile%	

	:HWCHECK
	If %count%==%AnzahlModelle% GOTO EXIT
	set Modell=Modell
	findstr /C:"%%Modell%%count%%"" %LogFile% >NUL  
	if errorlevel == 1 GOTO HWCHECKFAIL

	echo Hardware - %%Modell%%count%% wurde gefunden
	GOTO EXIT

	:HWCHECKFAIL
	echo Hardware - %%Modell%%count%% wurde NICHT gefunden

	set /a count=count+1
	GOTO HWCHECK
:EXIT
Pause
exit

Content-Key: 123544

Url: https://administrator.de/contentid/123544

Printed on: April 16, 2024 at 07:04 o'clock

Member: Biber
Biber Aug 26, 2009 at 18:40:05 (UTC)
Goto Top
Moin Pollekowski,

willkommen im Forum.

Ich habe Deinen Schnipsel mal für Demozwecke teilweise ausgeREMt.

Zum Testen.
@echo Off & setlocal EnableDelayedExpansion
REM md c:\temp

set LogFile=C:\temp\SystemInfo.log
REM del %LogFile%

set count=1
set AnzahlModelle=11
set "Modell1=HP Compaq dc5700"  
set "Modell2=HP Compaq dx2300"  
set "Modell3=HP Compaq dx2400"  
set "Modell4=HP Compaq dx2420"  
set "Modell5=HP Compaq dx2450"  
set "Modell6=HP Compaq dx5150"  
set "Modell7=HP Compaq xw4300"  
set "Modell8=HP Compaq xw4400  
set "Modell9=HP Compaq xw4600"  
set "Modell10=HP Compaq 6730b"  
set "Modell11=VMware Virtual Platform"  

REM Systeminfo.exe > %LogFile%	

	:HWCHECK
	If %count% GTR %AnzahlModelle% GOTO EXIT
	set "Modell=Modell%Count%"  
	ECHO [variante a] findstr /C:"!%Modell%!" %LogFile% ^>NUL  
	ECHO [variante b] findstr /C:"!Modell%count%!" %LogFile% ^>NUL  
	set /a "count=count+1" & GOTO HWCHECK   
	REM vorzeitiges Go Bäck /demomode
	REM if errorlevel == 1 GOTO HWCHECKFAIL

	echo Hardware - !Modell%count%! wurde gefunden
	GOTO EXIT

	:HWCHECKFAIL
	echo Hardware - !%Modell%count%! wurde NICHT gefunden

	set /a count=count+1
	GOTO HWCHECK
:EXIT
Pause
REM exit

Bei dieser Strategie über "DelayedExpansion", das über die Anweisung "Setlocal EnableDelayedExpansion" ermöglicht wird lässt sich Deine Anforderung umsetzen.

Ausgabe des Schnipsels wäre:
<code type=plain">>modellcnt.cmd
[variante a] findstr /C:"HP Compaq dc5700" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dc5700" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq dx2300" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dx2300" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq dx2400" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dx2400" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq dx2420" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dx2420" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq dx2450" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dx2450" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq dx5150" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq dx5150" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq xw4300" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq xw4300" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq xw4400" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq xw4400" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq xw4600" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq xw4600" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"HP Compaq 6730b" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"HP Compaq 6730b" C:\temp\SystemInfo.log >NUL
[variante a] findstr /C:"VMware Virtual Platform" C:\temp\SystemInfo.log >NUL
[variante b] findstr /C:"VMware Virtual Platform" C:\temp\SystemInfo.log >NUL
Drücken Sie eine beliebige Taste . . .


Allerdings nur, weil ich Deine Abbruchbedingung
If %count% == %AnzahlModelle% GOTO EXIT
...geändert habe in...
If %count% GTR %AnzahlModelle% GOTO EXIT
.
Sonst wäre die Ausgabe kürzer. face-wink

Statt über DelayedExpansion wäre auch ein Setzen der Variablen über ein "CALL SET...", d.h. eine kurz geöffnete Unterinstanz des CMD-Interpreters möglich,
aber das sieht noch unappetitlicher aus.

Und da wir ja zeitig essen wollen beschränke ich mich auf die obere Variante.

Grüße
Biber
Member: Pollekowski
Pollekowski Aug 27, 2009 at 06:31:20 (UTC)
Goto Top
Danke Dir Biber, dass Du dich meiner angenommen hast.
Noch ein paar kleine Verbesserungen und nun funktioniert es... Danke face-smile

Hier nochmal komplett und funktionstüchtig:
@echo OFF & setlocal EnableDelayedExpansion
set LogFile=%temp%\SystemInfo.log
del %LogFile%
set count=1
set AnzahlModelle=11
set "Modell1=HP Compaq dc5700"  
set "Modell2=HP Compaq dx2300"  
set "Modell3=HP Compaq dx2400"  
set "Modell4=HP Compaq dx2420"  
set "Modell5=HP Compaq dx2450"  
set "Modell6=HP Compaq dx5150"  
set "Modell7=HP Compaq xw4300"  
set "Modell8=HP Compaq xw4400"  
set "Modell9=HP Compaq xw4600"  
set "Modell10=HP Compaq 6730b"  
set "Modell11=VMware Virtual Platform"  

Systeminfo.exe > %LogFile%

:HWCHECK
	If %count% GTR %AnzahlModelle% GOTO ModellUnknown
	set Modell=Modell%Count%
	findstr /C:"!%Modell%!" %LogFile% >NUL  
	if errorlevel == 1 GOTO HWCHECKFAIL
	
	echo.
	echo Hardware - !%Modell%! wurde gefunden
	echo.
	set /a "count=count+1"  
	GOTO BeginInstall

:HWCHECKFAIL
	echo Hardware - !%Modell%! wurde NICHT gefunden
	set /a count=count+1
	GOTO HWCHECK

:BeginInstall

:EXIT
	del %LogFile%
	Pause
	exit

:ModellUnknown
	echo.
	echo Dieses Modell ist unbekannt!
	echo.
	findstr /C:"Systemmodell:" %LogFile%  
	findstr /C:"System Model:" %LogFile%  
	echo.
	GOTO EXIT