2

I am using a command:

find /c "abc" "C:\Users\abc\Desktop\project\string.txt"

Output:

---------- C:\Users\abc\Desktop\project\string.txt: 4

I want to assign this value 4 to a variable so that I can use it for an if statement.

1

4 Answers 4

2

I would use:

For /F %%A In ('Find /C "abc"^<"C:\Users\abc\Desktop\project\string.txt"') Do (
    Set "mlc=%%A")

Your %mlc% varaiable would hold the matched line count.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if this is the best method to do this, but it works:

find /c "abc" "C:\Users\abc\Desktop\project\string.txt" > tmpFile 
set /p myvar= < tmpFile 
del tmpFile 

Comments

0

with your snytax the output is ---------- C:\Users\abc\Desktop\project\string.txt: 4
There is another syntax: type file.txt|find /c "abc", which gives you a beautiful output of just:

15

To get it into a variable, use a for /f loop:

for /f %%a in ('type file.txt^|find /c "abc"') do set count=%%a
echo %count%

(for use directly on commandline (not in a batchfile) use %a instead of %%a)

2 Comments

by using "type file.txt|find /c "abc"" this command the text file gets open and also the count displays 0.
i want to get the count value and compare with the actual count so that i get the output as pass or fail.
0

I am not a batch script / Windows command line pro, but I got this to work with the following, without a temporary file:

for /f "delims=" %%a in ('dir "%~dpSomeFolder\*.suffix" /b ^|find /c "suffix"') do set "fileCount=%%a"

Explanation:

  1. I found it very confusing, why assigning a variable with a batch script, is so weird, considering it's "Windows", the most used operating system. Anyways this answer here is helpful. Even if it is a duplicate, I like the formatting more: Assign command output to variable in batch file

  2. %~dp0: basically translates to "path of this script". You can find info about this online.

  3. SomeFolder\*.suffix: In my case this I was looking to count the number of files ending with a certain suffix. I had problems using the dir command with \s as this listed all the matches in subfolders I did not expect him too look. As if this was referenced to the path from which I executed this script from. Therefore, the path name with the asterisk "\*.suffix" solved that issue for me.

  4. ^|: When using the pipe sign "|" in "for command", specified inside the single quotation marks, you need to use a circumflex "^|", instead of just the "|", which you would normally use when just typing in the command in cmd (f.e. like dir "%~dp0Folder*.suffix" /b | find /c "suffix"

  5. %%a: You have to use the "double percentage", as this is just a locale variable when writing this in a batch script.

FYI: you can have a look at the command help/ manual ("man" as I am used to Linux), with the command /? (f.e. dir /? or find /?)

I thought I would also mention how I then used this variable, as this might save some time for you ;) (batch code coloring somehow did not work here...).

IF %fileCount% NEQ 1 (ECHO Number of SUFFIX files does not equal 1! Found %fileCount% SUFFIX files inside the SomeFolder. Aborting script! & PAUSE & EXIT)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.