Count the number of various types of files in a specific directory (shell&c + +)

Keywords: C++ Windows

  • Shell language

This problem uses command line + bat (windows batch file) to realize query. There are two main methods. The first is to use dir command and find command to count the number of various types of files in a specific directory; The second is to use the for statement to traverse and count the files in a specific directory, as follows:

Compilation environment:

  1. Implementation of dir statement plus find statement
@echo off

set /p p1=echo.where do you want to find?  

set /p p2=echo.which type do you want to find?  

dir /s /b /a-d  %p1% | find /c  ".%p2%"

pause

Idea: the set/p function realizes the external device write operation, inputs the address of the directory to be searched and the file suffix to be searched, then uses the dir/s/b statement to traverse the target address directory, then uses the find statement to search the file name read out by the dir statement, counts the files with the target suffix, and finally outputs.

Step: open the command line window, drag in the bat file to run the script, then enter the address of the target directory and the suffix name of the target file in turn, and press enter to get the number of files with a specific suffix name under the target directory.

Example and screenshot:

 

(there are 3 pdf files in D:\test directory)

2.for statement implementation

@echo off

set /p add=- ^where do you want to find  

set /p type=- ^which type of doc do you want to find   

set cnt=0

for /r %add% %%i in (.%type%) do @echo "%%i" && set /a cnt+=1

@echo %cnt%

pause

Idea: the input is also realized with the set/p function. This time, a counting variable cnt is defined, and the for/r statement is used to recursively traverse the files with the target directory address, output and count the names of the files with the target suffix, and finally output the quantity

Step: open the command line window, drag in the bat file to run the script, then enter the address of the target directory and the suffix name of the target file in turn, and press enter to get the number of files with a specific suffix name under the target directory.

Example and screenshot:

(there are three pdf files in the D:\test directory, with names of 3, 4 and 5 respectively)

  • C/C + + language

Operating environment:

 

code:

#define _CRT_SECURE_NO_WARNINGS	
#include <iostream>
#include <string.h>
#include <fstream>
#include <cstring>
#include <io.h>	

extern int num = 0;

using namespace std;

void findFile(const char* path, const char* format);

int main()
{
    findFile("D:\\test", ".pdf");
    printf("%d", num);
    return 0;
}

void findFile(const char* path, const char* format)
{
    char newpath[200];
    strcpy(newpath, path);
    strcat(newpath, "\\*.*");
    int handle;
    _finddata_t findData;
    handle = _findfirst(newpath, &findData);
    if (handle == -1)        // Check for success
        return;
    while (_findnext(handle, &findData) == 0) {
        if (findData.attrib & _A_SUBDIR) {
            if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
                continue;
            strcpy(newpath, path);
            strcat(newpath, "\\");
            strcat(newpath, findData.name);
            findFile(newpath, format);
        }
        else {
            if (strstr(findData.name, format)) {     //Determine whether it is a txt file
                cout << findData.name << "\t" << path << "\n";
                num += 1;
            }
        }
    }
    _findclose(handle);                              
}

Idea: it is mainly a traversal function. The goal is to traverse all file names under the target directory, then find the files with the target suffix, output their names, addresses and paths, count them, and finally output the total number.

Step: write the suffix name of the target directory and target file in the main function in the code, and then open the GUI window after ctrl+f5. The name, address and last occurrence times of the target file will appear in turn.

Example and screenshot:

 

(there are three pdf files in the D:\test directory, with names of 3, 4 and 5 respectively)

 

 

Posted by LiquidEagle on Tue, 28 Sep 2021 12:03:56 -0700