Batch reg query key to variable - windows

When I run the command REG Query HKLM /k /F "Command Processor" /s /e /c on cmd, I get this result:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Command Processor
End results: 2 match(s) found(s).
But in batch:
#echo off & setlocal ENABLEEXTENSIONS
for /f "tokens=*" %%a in ('REG Query HKLM /k /F "Command Processor" /s /e /c') do set "MyPath=%%a"
echo The path string value is "%MyPath%"
pause
When I execute this, I only get the last line:
The path string value is "End results: 2 match(s) found(s)."
What is wrong? I would like to get the path keys on variables.

The problem is obvious: you are overwriting the value of MyPath in the for /F loop, then you are printing (echo) the final value/line.
To get all lines (any arbitrary number) you could do the following:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem storing the path strings in `MyPath1`, `MyPath2`, etc.:
set /A count=0
for /F "delims=" %%A in (
'REG Query HKLM /K /F "Command Processor" /S /E /C ^
^| findstr /L /B /V /C:"End of search: "'
) do (
set /A count+=1
set "MyPath!count!=%%A"
)
rem extracting the previously stored path strings:
echo Total number of path strings: %count%
for /L %%B in (1,1,%count%) do (
echo The !count!. path string value is "!MyPath%%B!"
)
pause
endlocal
This constitutes a sort of array MaPath1, MyPath2, and so on, containing all matching path strings.
The findstr command is used to filter out the summary line End of search: (this might be adapted according to your system locale/language).
Note that the array is no longer available after the endlocal command.

Related

Divide by zero error in while executing the batch file

I wrote a batch script to count number file in directory. after that i need to assign the same value to a variable.
#for /f %%a in ('2^>nul dir "C:\Users\robert\Desktop\Resouces" /a-d/b/-o/-p/s^|find /v /c ""') do set n=%%a #echo Totalfiles: %n%.
set /A "{$temp.DataTask1[1]/inout[1]/p_FileName}=%n%"
But this throws divide by zero error. Kindly let me know my mistake here. {$temp.DataTask1[1]/inout[1]/p_FileName} is name of the variable.
you can use the "set" command without /A parameter to resolve problem of "divide by zero"
#for /f %%a in ('2^>nul dir "C:\Users\robert\Desktop\Resouces" /a-d/b/-o/-p/s^|find /v /c ""') do set n=%%a
#echo Totalfiles: %n%.
set "{$temp.DataTask1[1]/inout[1]/p_FileName}=%n%"

How can I find last row that contains string in txt with cmd command

I want to find two different strings in .txt file. I need two scripts, first script to find last row that contains these strings together and second script to find last row that contains these strings seperately. I tried to write somethings but I go off the project.
This what i have tried so far as code :
#echo off
for /f %%i in ('find /v /c "" ^< deneme.txt') do set /a lines=%%i
echo %lines%
set /a startLine=%lines% - 1
more /e +%startLine% deneme.txt > temp.txt
find "ali" temp.txt|find "veli"
del temp.txt
Thanks for help.
#echo off
set "string1=ali"
set "string2=veli"
set "file=deneme.txt"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i /v "\<%string2%\>" ') do set "out1=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string2%\>" %file% ^|findstr /i /v "\<%string1%\>" ') do set "out2=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i "\<%string2%\>" ') do set "out3=%%a"
echo last line with %string1%: "%out1%"
echo last line with %string2%: "%out2%"
echo last line with both: "%out3%"
for explanations, see for /? and findstr /?

Count the number of lines of each text file in a given directory and store it in a variable

I want to count the number of lines of each text file in a given directory and store them in a variable.
Here is my code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "temp\textpipe_tmp\" %%U in (*.txt) DO (
set "cmd=findstr /R /N "^^" "%%U" | find /C ":""
for /f %%a in ('!cmd!') do set number=%%a
echo %number%
)
:eof
pause
I'm not sure why it does not work but if I get rid of SET, it works:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "temp\textpipe_tmp\" %%U in (*.txt) DO (
findstr /R /N "^" "%%U" | find /C ":"
)
:eof
pause
I need the result stored in a variable.
Another version, which does the same thing but is slightly better readable:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "C:\Users\Gebruiker\Documents\ICT" %%U in (*.txt) DO (
set lines=0
for /f %%A in (%%U) do (set /a lines+=1)
echo !lines!
)
pause
As #wOxxOm stated in his comment, find is the perfect choice for this task.
Supposing there is a file test.txt containing 12 lines, find /V /C "" "C:test.txt" will output something like:
---------- C:TEST.TXT: 12
So let us use a for /F loop to capture such an output and string substitution to get the text portion after :SPACE:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R "temp\textpipe_tmp\" %%U in ("*.txt") do (
rem capturing the output of `find` here:
for /F "delims=" %%A in ('find /V /C "" "%%~U"') do (
set "NUMBER=%%~A"
rem substituting substring `: ` and everything before by nothing:
set "NUMBER=!NUMBER:*: =!"
)
rem at this point, variable `NUMBER` is available
rem for the currently processed file in `%%~U`:
echo !NUMBER!
)
endlocal
Note that find /V /C "" will return unexpected reslts if there are empty lines at the end of the file (one of such might not be included in the count). However, empty lines at the beginning or in between non-empty ones will be counted.
Update:
Using redirection like > "C:test.txt" find /V /C "" rather than find /V /C "" "C:test.txt" avoids the prefix ---------- C:TEST.TXT: and just returns the number of lines (like 12). With this modification no string substitution is necessary and so the code looks like this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /R "temp\textpipe_tmp\" %%U in ("*.txt") do (
rem capturing the output of `find` here:
for /F "delims=" %%A in ('^> "%%~U" find /V /C ""') do (
set "NUMBER=%%~A"
)
rem at this point, variable `NUMBER` is available
rem for the currently processed file in `%%~U`:
echo !NUMBER!
)
endlocal
The redirection mark < needs to be escaped like ^< when being used after in in for /F.

Path over registry (windows)

I try to get a path over the regestry of windows. My Problem now is how do I get the Path out of MATLAB_ROOT_32?
for /F "tokens=* delims='C'" %%i IN ('reg query HKLM\SOFTWARE\WOW6432NODE\Mathworks\Matlab\7.9.1 /v MATLABROOT') do (set MATLAB_ROOT_32=%%i)
echo %MATLAB_ROOT_32%
set i=
rem GOTO Split1
rem :Split1
REM -- Split the result into MATLABROOT, REG_SZ and Folder using space as delimiter
for /f "tokens=1,2,3 delims='C'" %%a in ("%MATLAB_ROOT_32%") do set useless1=%%a&set useless2=%%b&set MATLAB_x32=%%c
echo %Matlab_x32%
The plan is to get the MATLAB Path in the Matlab_x32 variable.
This works for me:
#echo off
setlocal ENABLEEXTENSIONS
set MATLAB_VERSION=8.3
set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\MathWorks\MATLAB\%MATLAB_VERSION%
set VALUE_NAME=MATLABROOT
for /F "usebackq tokens=2*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
set MATLABROOT=%%B
)
echo %MATLABROOT%
Just change the Matlab version to whatever you're using and it should be fine. There are variations in the output from reg depending on the OS version, but this should cope (I think!)

How to count no of lines in text file and store the value into a variable using batch script?

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is
findstr /R /N "^" file.txt | find /C ":"
I refered the question How to store the result of a command expression in a variable using bat scripts?
Then I tried,
set cmd="findstr /R /N "^" file.txt | find /C ":" "
I am getting the error message,
FIND: Parameter format not correct
How could i get rid of this error.
There is a much simpler way than all of these other methods.
find /v /c "" filename.ext
Holdover from the legacy MS-DOS days, apparently. More info here: https://devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803
Example use:
adb shell pm list packages | find /v /c ""
If your android device is connected to your PC and you have the android SDK on your path, this prints out the number of apps installed on your device.
You could use the FOR /F loop, to assign the output to a variable.
I use the cmd-variable, so it's not neccessary to escape the pipe or other characters in the cmd-string, as the delayed expansion passes the string "unchanged" to the FOR-Loop.
#echo off
cls
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" file.txt | find /C ":""
for /f %%a in ('!cmd!') do set number=%%a
echo %number%
Inspired by the previous posts,
a shorter way of doing so:
CMD.exe
C:\>FINDSTR /R /N "^.*$" file.txt | FIND /C ":"
The number of lines
Try it. It works in my console.
EDITED:
(the "$" sign removed)
FINDSTR /R /N "^.*" file.txt | FIND /C ":"
$ reduces the number by 1 because it is accepting the first row as Field name and then counting the number of rows.
Try this:
#Echo off
Set _File=file.txt
Set /a _Lines=0
For /f %%j in ('Find "" /v /c ^< %_File%') Do Set /a _Lines=%%j
Echo %_File% has %_Lines% lines.
It eliminates the extra FindStr and doesn't need expansion.
- edited to use ChrisJJ's redirect suggestion. Removal of the TYPE command makes it three times faster.
#Tony: You can even get rid of the type %file% command.
for /f "tokens=2 delims=:" %%a in ('find /c /v "" %_file%') do set /a _Lines=%%a
For long files this should be even quicker.
I usually use something more like this
for /f %%a in (%_file%) do (set /a Lines+=1)
for /f "usebackq" %A in (`TYPE c:\temp\file.txt ^| find /v /c "" `) do set numlines=%A
in a batch file, use %%A instead of %A
The perfect solution is:
FOR /F %%i IN ('TYPE "Text file.txt" ^| FIND /C /V ""') DO SET Lines=%%i
I found this solution to work best for creating a log file that maintains itself:
setlocal enabledelayedexpansion
SET /A maxlines= 10
set "cmd=findstr /R /N "^^" "filename.txt" | find /C ":""
for /f %%a in ('!cmd!') do set linecount=%%a
GOTO NEXT
:NEXT
FOR /F %%A IN ("filename.txt") DO (
IF %linecount% GEQ %maxlines% GOTO ExitLoop
echo %clientname% %Date% %Time% >> "filename.txt")
EXIT
:ExitLoop
echo %clientname% %Date% %Time% > "filename.txt"
EXIT
Environmental variables included are %clientname% the computername of the remote client %Date% is the current date and %Time% the current time. :NEXT is called after getting the number of lines in the file. If the file line count is greater than the %maxlines% variable it goes to the :EXITLOOP where it overwrites the file, creating a new one with the first line of information. if it is less than the %maxlines% variable it simply adds the line to the current file.
You don't need to use find.
#echo off
set /a counter=0
for /f %%a in (filename) do set /a counter+=1
echo Number of lines: %counter%
This iterates all lines in the file and increases the counter variable by 1 for each line.
The :countLines subroutine below accepts two parameters: a variable name; and a filename. The number of lines in the file are counted, the result is stored in the variable, and the result is passed back to the main program.
The code has the following features:
Reads files with Windows or Unix line endings.
Handles Unicode as well as ANSI/ASCII text files.
Copes with extremely long lines.
Isn’t fazed by the null character.
Raises an error on reading an empty file.
Counts beyond the Batch max int limit of (31^2)-1.
#echo off & setLocal enableExtensions disableDelayedExpansion
call :countLines noOfLines "%~1" || (
>&2 echo(file "%~nx1" is empty & goto end
) %= cond exec =%
echo(file "%~nx1" has %noOfLines% line(s)
:end - exit program with appropriate errorLevel
endLocal & goto :EOF
:countLines result= "%file%"
:: counts the number of lines in a file
setLocal disableDelayedExpansion
(set "lc=0" & call)
for /f "delims=:" %%N in ('
cmd /d /a /c type "%~2" ^^^& ^<nul set /p "=#" ^| (^
2^>nul findStr /n "^" ^&^& echo(^) ^| ^
findStr /blv 1: ^| 2^>nul findStr /lnxc:" "
') do (set "lc=%%N" & call;) %= for /f =%
endlocal & set "%1=%lc%"
exit /b %errorLevel% %= countLines =%
I know it looks hideous, but it covers most edge-cases and is surprisingly fast.
Just:
c:\>(for /r %f in (*.java) do #type %f ) | find /c /v ""
Font: https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l
One nice surprise is for one who has git bash on his windows: just plain old linux wc -l <filename> will works for you there
In the below code, the variable name are SalaryCount and TaxCount
#ECHO OFF
echo Process started, please wait...
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set SalaryCount=%%C
echo Salary,%SalaryCount%
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set TaxCount=%%C
echo Tax,%TaxCount%
Now if you need to output these values to a csv file, you could use the below code.
#ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv
The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath
You can pipe the output of type into find inside the in(…) clause of a for /f loop:
for /f %%A in ('
type "%~dpf1" ^| find /c /v ""
') do set "lineCount=%%A"
But the pipe starts a subshell, which slows things down.
Or, you could redirect input from the file into find like so:
for /f %%A in ('
find /c /v "" ^< "%~dpf1"
') do set "lineCount=%%A"
But this approach will give you an answer 1 less than the actual number of lines if the file ends with one or more blank lines, as teased out by the late foxidrive in counting lines in a file.
And then again, you could always try:
find /c /v "" example.txt
The trouble is, the output from the above command looks like this:
---------- EXAMPLE.TXT: 511
You could split the string on the colon to get the count, but there might be more than one colon if the filename had a full path.
Here’s my take on that problem:
for /f "delims=" %%A in ('
find /c /v "" "%~1"
') do for %%B in (%%A) do set "lineCount=%%B"
This will always store the count in the variable.
Just one last little problem… find treats null characters as newlines. So if sneaky nulls crept into your text file, or if you want to count the lines in a Unicode file, this answer isn’t for you.
You can also try
set n=0 & for /f "tokens=*" %a in (text.txt) do set/a n=!n!+1
echo !n!
You can also mark with a wildcard symbol * to facilitate group files to count.
Z:\SQLData>find /c /v "" FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_*.txt
Result
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS01_V1.TXT: 2041
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIOST00_V1.TXT: 315938
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS00_V1.TXT: 0
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_CNTPTF00_V1.TXT: 277

Resources