Vineeth Pillai
Vineeth Pillai

Reputation: 27

How to get the FIND command output count value into a variable in batch scripting?

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.

Upvotes: 2

Views: 3128

Answers (4)

Stack it up
Stack it up

Reputation: 1

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)

Upvotes: 0

Compo
Compo

Reputation: 38642

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.

Upvotes: 2

Stephan
Stephan

Reputation: 56189

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)

Upvotes: 0

Sujeet Sinha
Sujeet Sinha

Reputation: 2433

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 

Upvotes: 0

Related Questions