1

I have a directory with 50 subdirectories containing a total of 300 shapefiles which I would like to query for their feature count and output to a text file.

Thus far I am able to get the feature count for all shapefiles in 1 subdirectory using ogrinfo:

for %f in (*.shp) do ogrinfo -al -so %f

This outputs a lot of additional metadata and tags which need to be stripped out manually.

Introducing the following to this script results in the feature counts all equaling 1:

-dialect sqlite -sql "SELECT COUNT(DISTINCT('ID'))"

I'd love to learn a command line solution to efficiently parse the ogrinfo output. I recognize that this could require some awk/grep type solutions which I can learn to do, but I am working with a standard Windows 10 install of OSGEO /GDAL.

2 Answers 2

2

You can use findstr with multiple search terms.

FOR /R %f IN (*.shp) do ( 
    echo %f  >> shape_list.txt
    ogrinfo -al -so %f| findstr /I /c:"layer name" /c:"feature count" >> shape_list.txt
)
2

Windows has a built in find command you could use. Do help find to see the options.

for %f in (*.shp) do ogrinfo -al -so %f | find /I "feature count"

If you need to reduce it down to just the count without the label you might have to find a different utility.

4
  • Thanks @mikewatt! The label is helpful. To add an additional tag like layer name, is the find command still useful?
    – mbela
    Commented Feb 26, 2019 at 22:47
  • Hmm, I'm not aware of any way to "or" the find string. Sounds like you might be better off writing some Python, Batch, or Powershell to get the job done
    – mikewatt
    Commented Feb 26, 2019 at 23:50
  • Thank you! Given user2856's response using findstr with two string searches, I am able to pipe these two metadata tags into a text file: FOR /R "dir" %f in (*.shp) do ogrinfo -al -so %f | findstr /I /c:"layer name" /c:"feature count" >> layer_feature_count.txt I have one more issue to resolve: each of fifty folders contains identically named shapefiles so I need to append the subdirectory name to layer name and feature count. Any thoughts?
    – mbela
    Commented Feb 27, 2019 at 1:03
  • Thank you user2856! That did the trick.
    – mbela
    Commented Feb 27, 2019 at 2:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.