I need some help to adjust a simple text.
I have generated a file from the command getmac.
The file looks like this:
1
2 F0-79-59-62-20-EB \Device\Tcpip_{F4744941-2A87-4DFA-A61B-393B8830B0C3}
3
My goal is to get to this point:
1 F0-79-59-62-20-EB
2
3
I.e. delete every string after the MAC and move it to the first line
or just read the mac and insert it to a variable without editing it at all
Thanks
Best regards,
Mike
C:\Users\Test\Desktop>SETLOCAL EnableDelayedExpansion
C:\Users\Test\Desktop>FOR /F "tokens=*" %a IN (c:\mymac.txt) DO (
SET line=%a
IF "!line:~0,2!" == "2 " SET macline=%a
)
C:\Users\Test\Desktop>(
SET line=F0-79-59-62-20-EB \Device\Tcpip_{F4744941-2A87-4DFA-A61B-
}
IF "!line:~0,2!" == "2 " SET macline=F0-79-59-62-20-EB \Device\Tcpip_{F474494
1-2A87-4DFA-A61B-393B8830B0C3}
)
C:\Users\Test\Desktop>FOR /F "tokens=2 delims= " %m IN ("") DO (
SET mac=%m
ECHO
)
C:\Users\Test\Desktop>pause
Press any key to continue . . .
The format of your generated file seems to be totally different from the output of the getmac command - at least, it is on my machine.
You should be able to use (from the prompt - but most responses on SO are of a batch file)
FOR /F %a IN ('getmac') DO SET mac=%a
echo %mac%
If I've understood the question right, this should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%a IN (input.txt) DO (
SET line=%%a
IF "!line:~0,2!"=="2 " SET macline=%%a
)
FOR /F "tokens=2 delims= " %%m IN ("!macline!") DO (
SET mac=%%m
)
At this point the mac ist stored in the %mac% variable. If you want to output it into your file, append this to the code:
ECHO 1 %mac% > output.txt
ECHO. >> output.txt
ECHO 2 >> output.txt
ECHO 3 >> output.txt
If you want to reuse the mac, it's stored in %mac%.
Related
How to remove the character from the variable in a batch script.
I have to create a CSV file for which i am using a batch script. I have a last field where the value comes as "0," or "1,".
I have to remove the , and just take the value 0 or 1.
I have written a code like :
<nul set /p test=%%S >> %fileDate%
how do i remove the , from the variable
for %%f in (*.sent) do (
for /f "tokens=3" %%S IN ('FIND "Status" "%%f"') DO (
<nul set /p test=%%S >> %fileDate%
set test=%%S
set test=%test:,= %
echo %test% >>%fileDate%
goto tests2
)
:tests2
)
this is my code , i have also tried to use the sub string and then used the echo to send the output to the file . But as i have used #ECHO OFF it is sending echo is off in the CSV file
I have a text file, that has some text with this syntax:
mytextfile.txt:
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
And so on....
And I'd like to be able to find username and password strings by pointing the websiteurl, so let's say I tell the batch file,
find websiteurl3, it should print the username3 and password3
and I was able to write a "FOR LOOP" but I am not sure how to use the syntax of the loop, as my code finds only the last line always, here is what I have:
FOR /F "tokens=2,3 delims= " %%A IN (URL.txt) DO IF EXIST URL.txt (set WEBUserName1=%%A) && (SET WEBUserPass1=%%B)
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
I know the loop is looking on the "URL.txt" for the tokens 2 and 3, and then sets the variables accordingly, what I'd like to know, is how I can use the loop, or if needed, any other command to be able to:
Find the "URL.txt file
Then find the specific string, in this case, the first word of the specified string line
Then find tokens 2 and 3 of that string line.
And I'd like to be able to find username and password strings
Use the following batch file.
test.cmd:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3" %%a in ('type mytextfile.txt ^| findstr "%1"') do (
echo Username:"%%a"
echo Password:"%%b"
pause
exit
)
endlocal
Notes:
Pass the website URL string as a parameter to the batch file.
findstr is used to find the matching line from the file, so we only need to parse a single line using for /f.
Example usage and output:
F:\test>type mytextfile.txt
websiteurl1 username1 password1
websiteurl2 username2 password2
websiteurl3 username3 password3
F:\test>test websiteurl3
Username:"username3"
Password:"password3"
Press any key to continue . . .
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
findstr - Search for strings in files.
for /f - Loop command against the results of another command.
type - Display the contents of one or more text files.
bad way, but this too work:
#echo off
setlocal
set srch_site=websiteurl2
FOR /F "tokens=1,2,3 delims= " %%A IN (URL.txt) DO (
IF EXIST URL.txt (
if "%%A"=="%srch_site%" (
(set WEBUserName1=%%B) && (SET WEBUserPass1=%%C)&goto:stdout
)
)
)
:stdout
pause
echo Username:"%WEBUserName1%"
echo Password:"%WEBUserPass1%"
pause
exit
May I offer you a different approach?
The code below get all usernames and passwords and store they in two vectors:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-3" %%A in (URL.txt) do set "User[%%A]=%%B" & set "Pass[%%A]=%%C"
After that, you may test if the username and password of a certain site was given this way:
set "site=websiteurl3"
if defined User[%site%] echo The site "%site%" have username and password
... or display their values this way:
echo Username:"!User[%site%]!"
echo Password:"!Pass[%site%]!"
You may review a detailed explanation of array management in Batch files at this post.
You get the last line, because you process the whole file. Filter your textfile and process only that one line:
set "site=websiteurl2"
FOR /F "tokens=2,3 delims= " %%A IN ('find "%site%" URL.txt') DO (
set "usr=%%A"
set "pwd=%%B"
)
echo %usr%, %pwd%
I have a utility (myexefile.exe) which outputs a list of information like this:
Line1=[Information in line1]
Line2=[Information in line2]
Line3=[Information in line3]
Line4=[Information in line4]
, etc.
I use a .bat file to write this information to a text file like this:
set myexefile="c:\myexefile.exe"
set outputfile="c:\outputfile.txt"
%myexefile% >> %outputfile%
However, I would like to write all of the lines except for the line containing "Line3=".
So I want the output to the outputfile.txt to be:
Line1=[Information in line1]
Line2=[Information in line2]
Line4=[Information in line4]
, etc.
I could probably create the file as it is and then use an existing sample which shows how to remove a line from a text file, but I would rather skip the line in the first place, rather than writing it to a text file and then removing it.
Can someone help me with this?
%myexefile% | find /v "Product ID">> %outputfile%
should filter out any line containing Product ID
setLocal enableDelayedExpansion
set outputfile="c:\outputfile.txt"
for /f "delims=" %%a in ('c:\myexefile.exe') do (
set out=%%a
if "!out:~0,4!" NEQ "Line3" echo %%a>>%outputfile%
)
or alternatively -
set outputfile="c:\outputfile.txt"
for /f "delims=" %%a in ('c:\myexefile.exe') do for /f %%b "delims=:" in ("%%a") do if "%%b" NEQ "Line3" echo %%a>>%outputfile%
i searched & tried for hours to get a newline when using a Batchfile with output -> file.
I am new to batchprograming and need some help. So here is my problem:
What i want :
I have a Batch that ping a spezific Server and writes the result in a "xml" format. This format is not real xml - its a kind of XML that a second app (LCDHost) understand.
Finaly the outfile should contain :
<host>myhost2ping.com</host>
<date>22.12.2013</date>
<time>17:03:25,55</time>
<ping>36</ping>
When getting this LCDHost can read this File and show the result in a external LCD ( im my case a G19 Keyboard).
What i actually get :
<host>myhost2ping.com</host> <date>22.12.2013</date> <time>17:03:25,55</time> <ping> Minimum = 36ms</ping>
My Batch:
:begin
for /f "delims=," %%a in ('ping -n 1 myhost2ping.com') do #echo ^<host^>myhost2ping.com^</host^> ^<date^>%DATE%^</date^> ^<time^>%TIME%^</time^> ^<ping^>%%a^</ping^> > ping.txt
TIMEOUT /T 60
goto :begin
What i already tried :
all versions of & echo.
set ^ as Var
/n, /c/n , /r/n
What i need :
each node ... in his own line
last entry should be cutted to only ms :
From Minimum = 36ms to 36.
thanky you for your help.
Here's another method using your code:
:begin
(for /f "delims=," %%a in ('ping -n 1 myhost2ping.com') do (
echo ^<host^>myhost2ping.com^</host^>
echo ^<date^>%DATE%^</date^>
echo ^<time^>%TIME%^</time^>
echo ^<ping^>%%a^</ping^>
)) > ping.txt
TIMEOUT /T 60
goto :begin
try this:
#echo off
setlocal EnableDelayedExpansion
set NL=^
REM the two empty lines above are required!
:begin
for /f "delims=," %%a in ('ping -n 1 www.google.de^|find "Minimum"') do ( set t=%%a )
rem it's easier to work with the variable outside the for-block:
set "t=%t: =%" :: remove spaces
set "t=%t:Minimum=%" :: remove Minimum
set "t=%t:ms=%" :: remove ms
set "t=%t:~1%" :: remove first char (equal sign)
#echo ^<host^>myhost2ping.com^</host^>!NL!^<date^>%DATE%^</date^>!NL!^<time^>%TIME
%^</time^>!NL!^<ping^>%t%^</ping^> > ping.txt
timeout /t 6
goto :begin
ok, I have 2 .txt files. "server.txt" and "local.txt", each have a number,
e.g
2 = server.txt
and
1 = local.txt.
I want so bat script reads both files and compares the numbers, another e.g
set local=1
set server=2
if %local% EQU %server% (
ECHO VERSION UP TO DATE
) else (
ECHO OUT OF DATE
)
pause
how would i set local to the number in the local.txt same for server.txt?
I assume the contents of the file local.txt as follows and that server.txt is similar.
1 = local.txt
Then, you can use something like the following to set local and server.
FOR /F "tokens=1 delims==" %%n IN (server.txt) DO SET server=%%n
FOR /F "tokens=1 delims==" %%n IN (local.txt) DO SET local=%%n