Reputation:
I am having problems trying to get the string after the : in the following two lines into two separate variables from the bottom of a text file called file.txt for example
Number of files to delete: 27 Total
Total size of files to delete: 1,427 KB
I just want the 27 in one variable, and the 1,427 in another. These are randomly generated from a scan, and could include more digits.
Batch file please, I am using windows
Upvotes: 2
Views: 5585
Reputation: 1655
jimbob, here is what I came up with:
delfiles.cmd
@echo off
set file=clean.txt
call :grep "Number of files to delete:"
set files=%grep%
call :grep "Total size of files to delete:"
set size=%grep%
echo %Files% files of a total size of %size% are to be deleted.
exit /b 0
:grep
setlocal
for /F "tokens=2 delims=:" %%i in ('findstr /i /c:"%~1" "%file%"') do (
for /F "tokens=1 delims= " %%j in ("%%i") do set _find=%%j
)
endlocal& set grep=%_find%
exit /b 0
This is a re-usable script, as the file and strings can be changed with the same results. I copied/pasted your example and it worked good.
@Patrick Cuff: you might run into problems finding a whole string without the /c:".." part. Might have been a part of the problem, but then maybe my script will give the same results/problems... Live and learn I guess.
______Notes__________
set file= : Set this to the file (and path, if needed) of the file to be scanned.
call :grep "string" : Call the :grep function with the string to find.
set var=%grep% : Set a variable (here files and size) to the answer from :grep.
:grep function: It first looks within the %file% for the "%string%", then parse it at the ':' character, keeping the right-hand part. It then parse it again with 'spaces' and keep the first word. The function returns the variable %grep% which contain the found string.
As with my usual answers, I hope this helps.
Upvotes: 1
Reputation: 29806
It looks like you're having some problem with reading through the whole file to get the number of lines. This solution will find just the strings you're interested in and get the values:
@echo off
setlocal enableextensions enabledelayedexpansion
:: Get number of files to delete.
for /f "usebackq tokens=1-7" %%A in (`findstr /b "Number of files to delete:" file.txt`) do (
set NUM_FILES=%%F
)
:: Get total size of files.
for /f "usebackq tokens=1-8" %%A in (`findstr /b "Total size of files to delete:" file.txt`) do (
set TOTAL_SIZE=%%G
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
If the strings with the number of files and total size ever change, you'll have to change the script accordingly.
Upvotes: 0
Reputation:
This is my full batch file;
@echo on
setlocal enableextensions enabledelayedexpansion
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (D:\clean.txt) do (
set /a LINES=LINES+1
echo %LINES%
)
:: Parse the last 2 lines and get the numbers into variable NUMS
set /a LINES=LINES-2
echo %LINES%
for /f "skip=%LINES% delims=" %%L in (D:\clean.txt) do (
for /f "tokens=1-2* delims=:" %%A in ("%%L") do (
for /f "tokens=1-2*" %%N in ("%%B") do set NUMS=!NUMS!%%N;
)
)
:: Parse variable NUMS
for /f "tokens=1-2* delims=;" %%A in ("%NUMS%") do (
set NUM_FILES=%%A
set TOTAL_SIZE=%%B
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
You can see where i put the extra echoes in, too see the value of the variable and i get the previous screenshot. Why does it work for you and not me? thats not fair lol I'm running Vista Home Basic SP1 32-bit
Upvotes: 0
Reputation: 29806
Something like this should work:
@echo off
setlocal enableextensions enabledelayedexpansion
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (file.txt) do (
set /a LINES=LINES+1
)
:: Parse the last 2 lines and get the numbers into variable NUMS
set /a LINES=LINES-2
for /f "skip=%LINES% delims=" %%L in (file.txt) do (
for /f "tokens=1-2* delims=:" %%A in ("%%L") do (
for /f "tokens=1-2*" %%N in ("%%B") do set NUMS=!NUMS!%%N;
)
)
:: Parse variable NUMS
for /f "tokens=1-2* delims=;" %%A in ("%NUMS%") do (
set NUM_FILES=%%A
set TOTAL_SIZE=%%B
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
Upvotes: 4
Reputation: 3852
I assume You need this to be done in pure bash.
#!/bin/bash
S="$(<testfile.txt)" # load the data
A="${S#*:}" # remove everything before the first : (including it)
A="${A%%Total*}" # remove everything after the first "Total" (including it)
B="${S##*:}" # remove everything before the last : (including it)
B="${B% *}" # remove everything after the last space (including it)
A="${A// }" # remove all spaces
B="${B// }"
echo "-$A-" # print results (showing that there are no spaces in it)
echo "-$B-"
I encourage You to explore Parameter Expansion section of man bash
. You will find numerous usefull tricks there.
Upvotes: -1