i have a textfile with different values in it.
define Domain "http://xyz.co"
define Remote "https:/www.test.co"
define OUTFILE "Folder name"
...
I would like to read the file and search for special tokens like Domain oder OUTFILE and set the value from the double quotes to a variable. The value inside the double quotes can contain spaces.
for /F "eol=; tokens=*" %%z in ("filename.txt") do (
set value=%%z
ECHO %%z | FINDSTR /C:"Domain" >nul & IF NOT ERRORLEVEL 1 (GOTO ITERATE)
)
:ITERATE
for /f tokens^=2^ delims^=^" %%p in ("%value%") do set Domain=%%p
echo %Domain%
This works so far for one value.
Is it possible to parse only once the file?
Is there a better way to get the content inside the double quotes?
Thanks!
:: Q:\Test\2018\11\01\SO_53101679.cmd
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
::clear env vars
For %%A in (Domain OUTFILE) do set "%%A="
for /F "eol=; tokens=1,2*" %%A in ('
findstr /i "^define.Domain ^define.OUTFILE" filename.txt
') do Set %%B=%%C
For %%A in (Domain OUTFILE) do if defined %%A Echo %%A=!%%~A!
Sample output based on your above fragment:
> Q:\Test\2018\11\01\SO_53101679.cmd
Domain=http://xyz.co
OUTFILE=Folder name
Related
I’m trying to parse some information from an XML file with a batch script and I have not been able to figure out how to rename the variable and keep the data intact. The output file is HTML which is what the device that is reading the data needs.
The batch file creates the HTML file just fine. The device is looking for Temperature: 75 and that is what it looks like in the file however if you check the file with a HTML editor the search name is included in the variable and the device can't use the data.
This is what I have been working on;
#echo off
start /b /WAIT %~dp0bitsadmin.exe /transfer "currentstats"
https://w1.weather.gov/xml/current_obs/KMGM.xml %~dp0Weather.xml
for %%a in (%~dp0Weather.xml) do (
for /f "tokens=* " %%b in ( ' type "%%a" ^|findstr /i "temp_f" ' ) do set tem1="%%b"
for /f "tokens=* " %%b in ( ' type "%%a" ^|findstr /i "relative_humidity" ' ) do set
hum1="%%b"
)
for /f "tokens=* delims=" %%P in ("<HTML>") do ( #echo %%P > %~dp0Weather.html)
for /f "tokens=* delims=" %%P in (%tem1%) do ( #echo Temperature: %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in ("<BR />") do ( #echo %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in (%hum1%) do ( #echo Humidity: %%P >> %~dp0Weather.html)
for /f "tokens=* delims=" %%P in ("</HTML>") do ( #echo %%P >> %~dp0Weather.html)
Data sample (from comments) Line breaks assumed.
<HTML> Temperature: <temp_f>75.0</temp_f> <BR />
Humidity: <relative_humidity>62</relative_humidity>
</HTML>
I Can’t figure how to remove the Left & Right pointing single angle quotation marks and what is between them so I end up with this.
<HTML> Temperature: 75.0 <BR />
Humidity: 62
</HTML>
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74092241.txt"
SET "omit="^<temp_f^>" "^</temp_f^>" "^<relative_humidity^>" "^</relative_humidity^>""
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
SET "line=%%e"
FOR %%y IN (%omit%) DO SET "line=!line:%%~y=!"
echo !line!
)
GOTO :EOF
I believe the key is to "escape" (turn off the special meaning) of >|< with a caret ^
It is much simpler to define what data you want to keep, rather than what data you want to remove! Just do this:
#echo off
setlocal EnableDelayedExpansion
rem Define keep values:
set "HTML=<HTML>"
set "BR /=<BR />"
set "/HTML=</HTML>"
(for /F "delims=" %%a in (Weather.xml) do (
set "line=%%a"
set "line=!line:<=%%!"
call set "line=!line:>=%%!"
echo !line!
)) > Weather.html
Change any <name> by %name% and evaluate the result line. All %varNames% that don't exists will be deleted...
I would consider doing this using PowerShell instead. Using PowerShell you should be able to grab the data directly instead of downloading and reading an XML file. It also affords you the opportunity to output directly to HTML.
Example:
WeatherData.ps1
$weather_url = "https://w1.weather.gov/xml/current_obs/KMGM.xml"
$request = [System.Net.HttpWebRequest]::Create($weather_url)
$request.UserAgent = "MyApplication/v1.0 (http://foo.bar.baz; foo#bar.baz)"
$response = $request.GetResponse()
$doc = New-Object System.Xml.XmlDocument
$doc.Load($response.GetResponseStream())
$doc.current_observation | ConvertTo-HTML -As List #{Label = 'Temperature'; Expression = {[Int]$_.temp_f}}, #{Label = 'Humidity'; Expression = {$_.relative_humidity}} | Out-File ($MyInvocation.MyCommand.Path.DirectoryName + 'weather.html')
To run that from a batch file, use this, (obviously changing %UserProfile%\Desktop\ as needed):
WeatherTest.cmd
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File "%UserProfile%\Desktop\WeatherData.ps1"
The output file weather.html should be located in the cmd.exe instance's current directory.
Note: If you want the decimal temperature, as opposed to the whole integer, as I used above, change [Int]$_.temp_f to just $_.temp_f.
If I chose to do this using only a batch file, then I'd use the built-in curl.exe utility, instead of bitsadmin.exe:
WeatherData.cmd
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_=" & For /F "Tokens=3 Delims=<>" %%G In ('%SystemRoot%\System32\curl.exe
"https://w1.weather.gov/xml/current_obs/KMGM.xml" -H "Accept: Application/XML"
2^>NUL ^| %SystemRoot%\System32\findstr.exe /IL "<temp_f> <relative_humidity>"
') Do #(If Not Defined _ (Set /P "=<HTML>Temperature: %%G<BR />" 0<NUL
Set "_=.") Else Set /P "=Humidity: %%G</HTML>" 0<NUL & Echo(
) 1>>"%~dp0weather.html"
Here's a modification which is of no purpose to any future user, and only included for the OP:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "_=" & For /F "Tokens=3 Delims=<>" %%G In ('%SystemRoot%\System32\curl.exe
"https://w1.weather.gov/xml/current_obs/KMGM.xml" -H "Accept: Application/XML"
2^>NUL ^| %SystemRoot%\System32\findstr.exe /IL "<temp_f> <relative_humidity>"
') Do #(If Not Defined _ (Set /P "=<HTML>Temperature: %%G<BR />" 0<NUL
Set "_=.") Else Echo(&Echo Humidity: %%G&Echo ^</HTML^>
) 1>>"%~dp0weather.html"
(for %%f in (%zipfiles%) do (
<nul set /p ="%%f",
certutil -hashfile "%%f" SHA256|find /v ":" || echo empty file
))> "C:\Location\Report\ListOfFiles.csv"
the "%%f" does not work, neither %%"f" nor "/p =%%f",
What I want is the filename in quotes:
"Filename 001.zip",68b17a9d0d98dd64f3c6c5b29e5cd304a6397d21f24e3087723ccad9f6f77c58
#ECHO OFF
SETLOCAL EnableExtensions
set "zipfiles=*.zip"
(for %%f in (%zipfiles%) do (
<nul set /p =""%%f","
certutil -hashfile "%%f" SHA256|find /v ":" || echo empty file
))>".\ListOfFiles.csv"
Here in <nul set /p =""%%f",":
outer double quotes used to escape the comma as well as inner double quotes so that
inner double quotes and the comma are copied to the output stream.
#ECHO OFF
SETLOCAL
set "zipfiles=file*"
(for %%f in (%zipfiles%) do (
if %%~zf==0 (echo "%%~ff",empty file
) else for /f "delims=" %%a in ('certutil -hashfile "%%f" SHA256^|find /v ":"') do echo "%%~ff",%%a
))> "u:\ListOfFiles.csv"
GOTO :EOF
Simple enough - no tricks. I tried with files named filename* on my machine. Simply use %%~zf for zero-length files to produce that name and otherwise select only the non-:-containing lines for certutil output to build the other lines; ^ used to escape the pipe so the certutil output is filtered.
destination filename also changed to suit my system.
I have some files in the form:
filename1 1 extra1.ext
filename1 2.ext
filename1 3 extra2.ext
...
filename2 1.ext
filename2 100 extra3.ext
...
filename20 1.ext
filename20 15 extra100.ext
(etc.)
...where filename1, filename2, etc., can contain spaces, symbol ' but not numbers. And extra1, extra2, etc, can contain anything. The number in the file name enclosed by spaces does not repeat per same filename1, filename2, etc.
What i want is to remove the extra things of the files that contain it. That is, to get from filename20 15 extra100.ext to filename20 15.ext
My first attempt is this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=file name 11 con sosas extras 2.txt"
set "ext=txt"
set "folder=."
for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"
that seems to work, but if i change it to receive parameters, it doesn't:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=%1"
set "ext=%2"
set "folder=%3"
for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"
where %1 is the filename, %2 is the extension and %3 is the folder in which the files are. Probably, the extension can be extracted inside the batch, but i don't know how to do it.
On another hand, i plan to use this batch into another one. There, there will be a for loop in (*.txt) and i don't know how to differentiate between files that have extra things (and then call this batch) from files that doesn't (and then not call this batch).
Regards,
use your method to extract the "extra-portion". In a second step, remove that extra-portion:
#echo off
setlocal enabledelayedexpansion
set "FILE=file name 11 con sosas extras 2.txt"
for /f "tokens=1,* delims=1234567890" %%a in ("%file%") do set new=!file:%%b=!%%~xb
echo %new%
%%~xb gives you the extension.
Here is a batch script that seeks the first purely numeric string portion enclosed within SPACEs, or in case it appears at the end, preceded by a SPACE, that occurs after some other text not consisting of SPACEs only. The part in front of the found number followed by a SPACE followed by the number itself are used for building the new file name.
This approach handles all valid characters for file names properly, even ^, &, %, !, ( and ).
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=.\test"
for /F "eol=| delims=" %%F in ('
dir /B "%_SOURCE%\*.ext" ^| findstr /R /I ^
/C:"^..* [0123456789][0123456789]*\.ext$" ^
/C:"^..* [0123456789][0123456789]* .*\.ext$"
') do (
set "FILE=%%F"
call :SPLIT FIRST NUM REST "%%~nF"
if defined NUM (
setlocal EnableDelayedExpansion
ECHO rename "!_SOURCE!\!FILE!" "!FIRST! !NUM!%%~xF"
endlocal
)
)
endlocal
exit /B
:SPLIT rtn_first rtn_num rtn_rest val_string
setlocal DisableDelayedExpansion
set "RMD=" & set "NUM=" & set "STR=%~4"
:LOOP
for /F "tokens=1,2,* delims= " %%I in ("%STR%") do (
if not "%%J"=="" (
(for /F "delims=0123456789" %%L in ("%%J") do rem/) && (
if not "%%K"=="" (
set "STR=%%J %%K"
goto :LOOP
)
) || (
set "NUM=%%J"
if not "%%K"=="" (
set "RMD=%%K"
)
)
)
)
set "STR=%~4"
if not defined NUM goto :QUIT
set "STR=%STR% "
call set "STR=%%STR: %NUM% =|%%"
for /F "delims=|" %%L in ("%STR:^^=^%") do set "STR=%%L"
:QUIT
(
endlocal
set "%~1=%STR%"
set "%~2=%NUM%"
set "%~3=%RMD%"
)
exit /B
After having tested the script, remove the upper-case ECHO command to actually rename any files.
Example below - 5 files will be located in the same folder.
Sales-fid1000-f100.dat
Revenue-fid1000-f100.dat
Sales-fid2000-f200.dat
Revenue-fid2000-f200.dat
Income-fid2000-f200.dat
I need to read the filename and get the number after "fid", in this case 1000 and 2000 and count the number of files associated with each "fid".
So for fid1000, there are 2 files and for fid2000, there are 3 files.
I need to write the output into a .txt file as below with first field being the fid number and second field being the count.
1000|2
2000|3
How can I generate output text file with fid and count using a Windows batch file?
#echo off
setlocal EnableDelayedExpansion
rem Process all file names
for /F "tokens=2 delims=-" %%a in ('dir /B /A-D *.dat') do (
rem Get FID from second dash-delimited token; format: "xxx-fid####-xxx.dat"
set "fid=%%a"
rem Accumulate it to the corresponding element of "count" array
set /A "count[!fid:~3!]+=1"
)
rem Create the output
(for /F "tokens=2,3 delims=[]=" %%a in ('set count[') do echo %%a^|%%b) > output.txt
For further details on array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script
#ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "outfile=%destdir%\outfile.txt"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*-fid*" '
) DO (
SET "filename=%%a"
CALL :process
)
(
FOR /F "tokens=1,2delims=$=" %%a In ('set $ 2^>Nul') DO ECHO(%%a^|%%b
)>"%outfile%"
GOTO :EOF
:process
SET "filename=%filename:*-fid=%"
FOR /f "delims=-" %%q IN ("%filename%") DO SET /a $%%q+=1
GOTO :eof
You would need to change the settings of sourcedir and destdir to suit your circumstances.
Produces the file defined as %outfile%
After clearing all the $ variables (for safety's sake), perform a directory listing without directorynames and in basic form of files in the source directory matching *-fid*.
For each name found, assign the name to filename and execute the :process routine, which first removes the characters up to and including -fid from filename then uses the delims=- option to assign the part originally between -fid and the following - to %%q.
setthe variable $%%q up by 1 (if $?? is undefined, assign 1)
Finally, when all the names have been processed, list the variables named $... using set which produces a report of the style
$1000=2
$2000=3
Using $ and = as delimiters puts token 1 (eg 2000) into %%a and token 2 (eg 3) into %%b. Write these to the output using echo, remembering to escape the pipe (|) with a caret (^) to suppress the interpretation as a redirector.
The parentheses around the for...$... ensures the output is directed to the destination file specified.
Extract the numbers into a temporary file, then count the occurrences of each number in that file.
#echo off
setlocal EnableDelayedExpansion
>temp.txt type nul
set "unique_num="
for /f "tokens=2 delims=-" %%a in ('dir /b *.dat') do (
set "fid=%%a"
set "num=!fid:~3!"
>>temp.txt echo !num!
echo " !unique_num! " | find " !num! " >nul
if !errorlevel! neq 0 set "unique_num=!unique_num! !num!"
)
for %%n in (%unique_num%) do (
for /f "delims=: tokens=2" %%c in ('find /c "%%n" temp.txt') do (
set "count=%%c"
echo %%n^|!count: =!
)
)
del /f /q temp.txt
Pipe the result into sort if you need the output sorted.
This is my first posting so if the format is not as it supposed to be please excuse me for this. (Suggestions for
improvement are welcome.)
I am trying to create a batchfile that will read last lines from logfiles and copy them to a new file.
Until now I have found here a way to read the last line.
Code would be something like:
for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
set /a startLine=%lines% - 1
more /e +%startLine% someFile.txt > lastLines.txt
The above code works for one file at a time. What I need is to read the last line from all files in a known list and add this line to a new .csv file.
I have been using the following code for getting the 4th entry in the logfiles but it returns every line of every logfile:
for /f %%x in (%list%) do for /f "delims=.txt, tokens=4" %%i in (%%x.txt) do echo %%x, %%i >> output.csv
What I would need is a sort of combination of both but I don't know how to combine them and make the complete last line be copied to the .csv file.
===
#Magoo:
Thanx for your reaction.
In every logfile can be 1 to >100 lines with comma separated information. Something like:
"LOGON,6-1-2015,12:43:39,USERNAME,HOSTNAME,,,,192.168.209.242,00:21:5A:2E:64:5E"
The last code with the 4th entry was used to get a list of all accounts that had logged in to the computers. This code gave me a very large list of all logon/logoff events on all computerlogs I checked in %list%.
In %list$ I had all the names of logfiles I wanted to be checked. This returned all lines.
For a new batchfile I need only the last logon/logoff entry and I want the whole last line.
So I have a .txt file with the hostnames of all computers I need to examine.
This .txt file will be read line by line via the variable %list%.
From every logfile I need only the last line copied to an output file.
===
I just tried the solution offered by JosefZ. Unfortunately this does not work for me yet. No lastlines are copied to the resultfile. In the code I removed the extra entry for possible lastlines for there are no empty lines in the logs, I also added an entry for the hostname I want to be available in the result. JosefZ had the filename there:
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set "host=%%~x"
for /F "tokens=*" %%G in ('type "%%~x"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
set "filename=.\logs\%filename:&=^&%.txt"
echo %host%,%lastline%>>output.csv
goto :eof
The resultfile shows only the hostnames. I'll puzzle some more with this but all tips are welcome!
===
Got it!!!
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set filename= :: *empty previous filename*
set lastline= :: *empty previous lastline*
set "host=%%~x"
set "filename=.\logs\%host%.txt" :: *creating the filename from path+hostname+extention*
for /F "tokens=*" %%G in ('type "%filename%"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
echo %host%,%lastline%>>output.csv
goto :eof
Your approach with line numbering could fail if a file has more trailing empty lines. Fortunately for /F loop ignores (does not iterate) empty lines; let's put to use this feature: in the script used next practices:
disabledelayedexpansion to allow ! in file names
set "list=_listing.txt" where the _listing.txt contains list of file names (full path and extension .txt including), one file name on one line: got by dir /b /s *.txt>_listing.txt
type nul>files\output.csv to empty the output file (optional)
set "lastline=!!!file empty!!!" to initialize variable %lastline%; could be set "lastline=" as well
call :lline to process variables %filename% and %lastline%
set "filename=%filename:&=^&%" to allow & in file names
The script is as follows:
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>files\output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set "filename=%%~x"
set "lastline=!!!file empty!!!"
rem the whole line
for /F "tokens=*" %%G in ('type "%%~x"') do set "lastline=%%G"
rem the fourth token only
rem for /F "tokens=4" %%G in ('type "%%~x"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
set "filename=%filename:&=^&%"
echo %filename% %lastline%
rem >>files\output.csv
goto :eof
Sample _listing.txt file:
d:\bat\files\1exclam!ation.txt
d:\bat\files\2exc!lam!ation.txt
d:\bat\files\11per%cent.txt
d:\bat\files\12per%cent%.txt
d:\bat\files\17per%Gcent.txt
d:\bat\files\18per%%Gcent.txt
d:\bat\files\21ampers&nd.txt
d:\bat\files\22ampers&&nd.txt
Output:
d:\bat>lastlines
d:\bat\files\1exclam!ation.txt 0 15.01.2015 1:52:28.48 -15072 20465
d:\bat\files\2exc!lam!ation.txt 6 15.01.2015 1:52:28.50 3250 16741
d:\bat\files\11per%cent.txt -8 15.01.2015 1:52:28.50 -3692 27910
d:\bat\files\12per%cent%.txt !!!file empty!!!
d:\bat\files\17per%Gcent.txt 0 15.01.2015 1:52:28.56 14508 12374
d:\bat\files\18per%%Gcent.txt 1 15.01.2015 1:52:28.56 30540 26959
d:\bat\files\21ampers&nd.txt 15.01.2015 1:22:50.18
d:\bat\files\22ampers&&nd.txt 15.01.2015 1:22:50.18
Honestly, all that ballast is for (possibly) trailing empty lines in files and for (possibly) ! and & in file names only; all could be done with
for /f %%x in (%list%) do for /f "skip=%startLine% tokens=4" %%i in (%%x) do echo %%x, %%i >> output.csv
You should use a simple FOR to iterate a list of values, not FOR /F.
Something like the following should work:
#echo off
setlocal enableDelayedExpansion
>>output.csv (
for %%F in (
"file1.log"
"file2.log"
"file3.log"
etc.
) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
more +!skip! %%F
)
)
The quotes around the file names are there in case you get a name with spaces.
You could use your LIST variable if it looks something like
set LIST="file1.log" "file2.log" "file3.log" etc.
#echo off
setlocal enableDelayedExpansion
set LIST="file1.log" "file2.log" "file3.log" etc.
>>output.csv (
for %%F in (%LIST%) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
more +!skip! %%F
)
)
If any of your file names contain the ! character, then you must toggle delayed expansion ON and OFF within your loop. Otherwise the delayed expansion will corrupt the names when %%F is expanded.
#echo off
setlocal disableDelayedExpansion
set LIST="file1.log" "file2.log" "file3.log" etc.
>>output.csv (
for %%F in (%LIST%) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
setlocal enableDelayedExpansion
more +!skip! %%F
endlocal
)
)