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%
Related
I have two .txt files. One contains numbers, and the other one contains filepaths. I want to combine these two files to a .csv. The combination is based on wether the number (from nrs.txt) is in the string of the filepath (nodups.txt).
Now I have the following code for this:
#setlocal enableextensions enabledelayedexpansion
for /F %a IN (Output\nrs.txt) DO (
SET "nrs=%a"
for /F %b IN (Output\nodups.txt) DO (
SET "pathstring=%b"
SET csvdelim=,
IF NOT x!pathstring:%nrs%=""!==x%pathstring% %nrs%,%pathstring%>>new2017.txt
)
)
#endlocal
However, I keep having the following issues with the code:
The pathstring never seems to get set. (when I run the code without the if statement, The nrs variable gets set but the pathstring is set to %b). I've seen a lot of possible solutions on here already but none seem to work for me (setting variables like !var! and using usebackq).
The IF statement in the second for loop gets the following error message =""!==x%pathstring% was unexpected at this time. The ="" should remove the nr. from the path (if its there). When I replace "" with something else it still does not work.
The file contents are:
File nrs.txt:
12345
12245
16532
nodubs.txt:
C:\tmp\PDF_16532_20170405.pdf
C:\tmp\PDF_1234AB_20170405.pdf
C:\tmp\PDF_12345_20170506.pdf
Desired output:
12345, C:\tmp\PDF_12345_20170506.pdf
16532, C:\tmp\PDF_16532_20170405.pdf
I really hope someone can help me out with this !
This solution use a different approach, based on arrays:
#echo off
setlocal EnableDelayedExpansion
rem Load array from nodubs.txt file
pushd "Output"
for /F "tokens=1,2* delims=_" %%a in (nodubs.txt) do set "nodubs[%%b]=%%a_%%b_%%c"
rem Process nrs.txt file and show output
(for /F %%a in (nrs.txt) do (
if defined nodubs[%%a] echo %%a, !nodubs[%%a]!
)) > new2017.txt
In a batch file for variables need two percent signs.
There is no need to put %%A into a variable, use it directly.
#setlocal enableextensions enabledelayedexpansion
for /F %%a IN (Output\nrs.txt) DO (
findstr /i "_%%a_" Output\nodups.txt >NUL 2>&1 || >>new2017.txt Echo %%a
)
#endlocal
Instead of a second for, I'd use findstr to search for the entry of nrs.txt enclosed in underscores.
if no find use condiotonal execution on failure || to write to the new file.
According to changed preliminaries another answer.
#Echo on
Pushd Output
for /F "tokens=1-3 delims=_" %%A IN (
' findstr /G:nrs.txt nodubs.txt'
) DO >>"..\new2017.txt" Echo %%B, %%A_%%B_%%C
Popd
sample output:
> type ..\new2017.txt
16532, C:\tmp\PDF_16532_20170405.pdf
12345, C:\tmp\PDF_12345_20170506.pdf
Best Users,
I want to made a batch script that deletes a specific word in a txt file.
I used the following but it didn't work
(Script that I used) :
SET /p word=device
FOR /f "delims=" %%a IN ('dir /a-d /b "Input_%date%.txt"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"'
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
(Input_%date%.txt):
1015faf2da091b02 device
1115fbd4e0dd3b03 device
I want to delete the word "device" in every line.
Can anyone help me out!
Kind Regards,
A.V.R
I guess you want sth like this:
SETLOCAL EnableDelayedExpansion
set x=device
FOR /f "delims=" %%a IN (Input_%date%.txt) DO (
set b=%%a
echo !b:%x%=!
)
But once you install sed (from for example unxutils) you can do this with simple one liner
type input_%date%.txt | sed "s/device//"
I assumed you can forward output to another file afterwards.
I have the following batch script to read an xml file and find a word (in this case, "factory"):
#echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in ('findstr /C:"factory" xcsconfig.xml') do set content=%%a
set content=%content:*"=%
set content=%content:~0,-1%
echo %content%
exit /b
Here's part of the xml file:
<loggers>
<recorder1>
<add name="factory" value="xlog"/>
<add name="alias" value="WSEnterprise.log"/>
</recorder1>
<recorder2>
<add name="factory" value="weblog"/>
</recorder2>
</loggers>
The code works fine and will always return the "first" founding - value="weblog"/. My question is, is there a way to return the founding under a specific tab? (i.e. I want to search specific under recorder1 instead of record2 tab, and return answer value="xlog"/). Thanks in advance.
EDIT: I changed my expected answer, it was incorrect
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sectionstart=recorder1"
SET "insection="
for /f "delims=" %%a in ('type q25062317.txt') do (
IF DEFINED insection (
ECHO "%%a"|FINDSTR /c:"factory" >NUL
IF NOT errorlevel 1 SET "content=%%a"
)
ECHO "%%a"|FINDSTR /i /L /c:"<%sectionstart%>" > NUL
IF NOT ERRORLEVEL 1 SET insection=Y
ECHO "%%a"|FINDSTR /i /L /c:"</%sectionstart%>" > NUL
IF NOT ERRORLEVEL 1 SET "insection="
)
set content=!content:*"=!
set content=!content:*"=!
set content=!content:~1,-1!
echo %content%
GOTO :EOF
I used a file named q25062317.txt containing your data for my testing.
You can incorporate also the next statements inside the do block of the for cycle. I mean:
setlocal EnableDelayedExpansion for /f "delims=" %%a in ('findstr /C:"factory" xcsconfig.xml') do (
set content=%%a
set content=!content:*"=!
set content=!content:~0,-1!
echo !content!
)
In this way the output is not only the last XML code line but all the code lines that contain the "factory" string in the file considered. Of course this example doesn't echo only a single desired string but this is possible setting a condition to the output of the loop.
Here is a robust tool that can help you - the command looks like this to get the line you need and it can be wrapped in a for /f loop.
type file.xml |findrepl "<recorder1>" /e:"</recorder1>" /b:"factory"
This uses a helper batch file called findrepl.bat (by aacini) - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
If I had a text file that was a list of things in the following format:
Thing1 "Description of Thing1"
Thing2 "Description of Thing2"
etc.
How would I use a batch file to echo only the information contained in the quotes for any given thing?
Edit: Per the answer, I've tried the following:
for /f "tokens=1*" %%a in (G:\Games\Console\Emulators\MAME\listfull.txt) do if "%%a"=="%%~nf" set gamename=%%~b
echo !gamename!
if !count!==1 echo|set /P=", "!gamename!": [#name: "%%~nf", #path: "%%~dpf"]" >> "G:\Games\Console\Utilities\Lists\MAME_%%d.txt"
It almost totally works; if the description contains an &, symbol, however, "echo !gamename!" works while the next line doesn't and spits out that "&' isn't a command. (I have to use the echo|set to produce lines that don't have a carriage return.)
Edit2: The only solution I've found is to add an ^ before every & in whatever text file I'm reading the descriptions from. Not an ideal workaround, but...
set "search=Thing2"
for /f "tokens=1*" %%a in (textfile.txt) do if "%%a"=="%search%" echo %%~b
If all this script does is outputs the information for any given thing, then you could change search to %~1 and just input the value when calling the script.
for /f "tokens=1*" %%a in (textfile.txt) do if "%%a"=="%~1" echo %%~b
for /f "tokens=1*" %%a in (file.txt) do echo(%%~b
I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)