scanbot90
Goto Top

Mit BatchDatei aus TXT-File lesen und gelesenen Inhalt im nächsten Befehl weiterverwenden (in einer FOR bzw WHILE-Schleife)

Einen schönen guten Abend zusammen,

ich habe mich schon zu dem obigen Thema hier im Forum umgeschaut, aber habe leider bis Dato noch keinen brauchbaren Lösungsansatz bekommen.

Meine Problemstellung:

Ich möchte mit Hilfe einer Batchdatei aus einer Textdatei ab einer gewissen Stelle lesen und den gelesenen Inhalt als Variable abspeichern um ihn für einen weiteren Befehl wiederzuverwenden.

Beispiel für eine Textdatei aus der gelesen werden soll: profiles.txt
Profile auf Schnittstelle WiFi:

Gruppenrichtlinienprofile (schreibgeschützt)
<Kein>

Benutzerprofile* * *
Profil für alle Benutzer : RaspberryPi Nr 1
Profil für alle Benutzer : RaspberryPi Nr 2
Profil für alle Benutzer : RaspberryPi Nr 3
Profil für alle Benutzer : RaspberryPi Nr 4



Anforderung: Ich möchte das aus der Textdatei vorerst nur der Text "RaspberryPi Nr 1" ausgelesen wird. Dieser ausgelesene Text soll dann in einem weiteren Befehl verwendet werden..
Danach soll "RaspberryPi Nr 2" ausgelesen werden und in einem weiteren Befehl verwendet werden.. usw bis die Textdatei Endet.
Natürlich wird bei sowas eine FOR-Schleife verwendet, aber leider habe ich zu wenig Ahnung von der Syntax in Batch-Dateien um mir selbst zu behelfen.

Der ausgelesene String soll dann im folgenden Befehl verwendet werden
 netsh wlan show profiles "RaspberryPi Nr 1"  

Ich hoffe ich konnte das Problem verständlich schildern, ansonsten versuche ich es nochmal.


PSEUDOCODE:

while profiles.txt is not EOF do
{ xyz = "alles nach einem ':' " // in dem Fall -> RaspberryPi Nr x
netsh wlan show profiles xyz
}


Ich wäre sehr dankbar wenn mir jemand weiterhelfen könnte.

Ich wünsche noch einen schönen Abend face-smile
Liebe Grüße

Content-Key: 307739

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

Printed on: May 18, 2024 at 11:05 o'clock

Mitglied: 129813
Solution 129813 Jun 21, 2016 updated at 06:22:22 (UTC)
Goto Top
Hi.
@echo off
for /f "tokens=2 delims=:" %%a in ('findstr /ic:"Profil für alle Benutzer" "c:\textdatei.txt"') do netsh wlan show profiles name="%%a"  
Regards
Member: scanbot90
scanbot90 Jun 21, 2016 at 08:40:14 (UTC)
Goto Top
Hi highload,

ich habe deinen Lösungsansazt ausprobiert, leider gibt es eine Fehlermeldung.
->"%%a" kann syntaktisch an dieser Stelle nicht verarbeitet werden.

Grüße
Mitglied: 129813
129813 Jun 21, 2016 updated at 10:54:18 (UTC)
Goto Top
->"%%a" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
The above code is for usage inside a batch file! If you want to use it directly on the cmd-shell you need to replace all %%a by %a.
Member: scanbot90
scanbot90 Jun 21, 2016 updated at 12:41:38 (UTC)
Goto Top
Hi again,
your Code is working fine but I still have one problem.

The delimitation is ":"
but I want this as a delimitation " : " face-smile

Because your code grabs this from the Textfile " RaspberryPi Nr 1"
but I want this: "RaspberryPi Nr 1"

there is a free space at the beginning..

And this is in profiles.txt:

Profile auf Schnittstelle WiFi:

Gruppenrichtlinienprofile (schreibgeschtzt)
<Kein>

Benutzerprofile
Profil fr alle Benutzer : RaspberryPi Nr 1
Profil fr alle Benutzer : RaspberryPi Nr 2
Profil fr alle Benutzer : Pi_Mini_Robot
Profil fr alle Benutzer : RaspberryPi Nr 3
Profil fr alle Benutzer : hsx
Profil fr alle Benutzer : RaspberryPi Nr 4
Profil fr alle Benutzer : AndroidAP


can you help me?

Greetings
Mitglied: 129813
Solution 129813 Jun 21, 2016 updated at 16:11:17 (UTC)
Goto Top
Sure we can do that ...
@echo off & setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%a in ('findstr /ic:"alle Benutzer" "c:\profiles.txt"') do (  
    set "profilename=%%a"  
    set "profilename=!profilename:~1!"  
    netsh wlan show profiles name="!profilename!"  
)
Regards
Member: scanbot90
scanbot90 Jun 21, 2016 at 22:17:17 (UTC)
Goto Top
Thank you Mr. Highload,
it works great.

here is the final code:

@echo off & setlocal enabledelayedexpansion
title Systeminformationen

:Begruessung
cls
echo Systeminformationen
systeminfo >> log-file.txt
rem pause>nul
goto IPconf1

:IPconf1
cls
ipconfig /all >> log-file.txt
rem pause>nul
goto Wlan

:Wlan
cls
netsh wlan show profiles >> profiles.txt
rem pause>nul
goto NET

:NET
rem for /f "tokens=2 delims=:" %%a in ('findstr /ic:"alle Benutzer" "profiles.txt"') do netsh wlan show profiles name="%%a" >> log-file.txt   
for /f "tokens=2 delims=:" %%a in ('findstr /ic:"alle Benutzer" "profiles.txt"') do (   
    set "profilename=%%a"  
    set "profilename=!profilename:~1!"   
    netsh wlan show profiles name="!profilename!" key=clear >> log-file.txt  
)
rem netsh wlan show all >> log-file.txt
pause>nul
exit


-CLOSED-
Mitglied: 129813
Solution 129813 Jun 22, 2016 updated at 07:22:23 (UTC)
Goto Top
Hi,
you don't need to write the output of the show profiles command into an extra textfile, you can pipe the output to findstr directly in the for-loop:
for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr /ic:"alle Benutzer"') do
Member: scanbot90
scanbot90 Jun 22, 2016 at 12:17:09 (UTC)
Goto Top
Hi again,

ok thats clever! Thank you face-smile))

Greetings
Member: scanbot90
scanbot90 Jun 27, 2016 at 00:14:10 (UTC)
Goto Top
Hi Mr Highload ;)

pre-Final Code:
@echo off & setlocal enabledelayedexpansion
title Systeminformationen

:Begruessung
cls
rem echo Systeminformationen
systeminfo >> log-file.txt
goto IPconf

:IPconf
cls
ipconfig /all >> log-file.txt
goto NET

:NET
for /f "tokens=2 delims=:" %%a in ('netsh wlan show profiles ^| findstr /ic:"alle Benutzer"') do (  
    set "profilename=%%a"  
    set "profilename=!profilename:~1!"   
    netsh wlan show profiles name="!profilename!" key=clear >> log-file.txt  
)
exit

... do you know any other interessting commands that I could use?

Thank you.
Greetings