C言語関係掲示板

過去ログ

No.338.ディレクトリ構成一覧表示

[戻る] [ホームページ]

No.2148

教えてください!!ディレクトリ検索
投稿者---初心者太郎(2002/07/23 00:28:21)


C言語を始めて1週間の初心者です。

ディレクトリ構成一覧表示のプログラムを教えてください。
コマンドラインでコマンド名とドライブ名を入力するとそのドライブ内に
あるディレクトリ構成一覧を表示したいのですがよくわかりません。
いろいろ調べて「_dos_findfirst」を使うことはわかったのですが・・
以下のようなことを表示したいと思ってます。

例)コマンドライン--test(コマンド名)C:(ドライブ名)
実行結果 C:\Windows\System\***
C:\Tool\***\***

ディレクトリだけで結構です。
ファイルまでだとかなりの量になってしまうので。
申し訳ありませんがよろしくお願いします。



No.2151

Re:教えてください!!ディレクトリ検索
投稿者---かずま(2002/07/23 02:13:37)


> ディレクトリ構成一覧表示のプログラムを教えてください。
> コマンドラインでコマンド名とドライブ名を入力するとそのドライブ内に
> あるディレクトリ構成一覧を表示したいのですがよくわかりません。
> いろいろ調べて「_dos_findfirst」を使うことはわかったのですが・・

Windows なら、16ビット Cランタイムの _dos_findfirst よりも、
32ビット Cランタイムの _findfirst を使ったほうが良いのではありませんか。
#include <stdio.h>
#include <string.h>
#include <io.h>

void dir(char *path, int len)
{
    struct _finddata_t  fd;
    long  handle;

    strcpy(path + len, "\\*");
    handle = _findfirst(path, &fd);
    if (handle == -1) return;
    do {
        if ((fd.attrib & _A_SUBDIR)
          && strcmp(fd.name, ".")
          && strcmp(fd.name, "..")) {
            sprintf(path + len, "\\%s", fd.name);
            puts(path);
            dir(path, len + 1 + strlen(fd.name));
        }
    } while (_findnext(handle, &fd) == 0);
}

int main(int argc, char **argv)
{
    char path[4096];

    if (argc == 2) {
        strcpy(path, argv[1]);
        dir(path, strlen(path));
    }
    return 0;
}


No.2154

Re:教えてください!!ディレクトリ検索
投稿者---かずま(2002/07/23 13:13:27)


> Windows なら、16ビット Cランタイムの _dos_findfirst よりも、
> 32ビット Cランタイムの _findfirst を使ったほうが良いのではありませんか。

_findclose() を忘れていました。
#include <stdio.h>
#include <string.h>
#include <io.h>

void dir(char *path, int len)
{
    struct _finddata_t fd;   long handle;   int n;

    strcpy(path + len, "\\*");
    handle = _findfirst(path, &fd);
    if (handle == -1) return;
    do {
        if ((fd.attrib & _A_SUBDIR) == 0) continue;
        if (strcmp(fd.name, "..") == 0) continue;
        if (strcmp(fd.name, ".") == 0) continue;
        n = sprintf(path + len, "\\%s", fd.name);
        puts(path);
        dir(path, len + n);
    } while (_findnext(handle, &fd) == 0);
    _findclose(handle);
}

int main(int argc, char **argv)
{
    char path[4096];

    if (argc != 2) return 1;
    strcpy(path, argv[1]);
    dir(path, strlen(path));
    return 0;
}


No.2156

Re:教えてください!!ディレクトリ検索
投稿者---かずま(2002/07/23 13:54:21)


> Windows なら、16ビット Cランタイムの _dos_findfirst よりも、
> 32ビット Cランタイムの _findfirst を使ったほうが良いのではありませんか。

16ビット Cランタイムでないと、LSI C-86 ではコンパイルできないんですね。
失礼しました。
#include <dos.h>

void dir(char *path, int len)
{
    struct find_t f;  int n;

    strcpy(path + len, "\\*");
    if (_dos_findfirst(path, _A_SUBDIR, &f) != 0) return;
    do {
        if ((f.attrib & _A_SUBDIR) == 0) continue;
        if (strcmp(f.name, "..") == 0) continue;
        if (strcmp(f.name, ".") == 0) continue;
        n = sprintf(path + len, "\\%s", f.name);
        puts(path);
        dir(path, len + n);
    } while (_dos_findnext(&f) == 0);
}


No.2165

ありがとうございました
投稿者---初心者太郎(2002/07/24 00:21:56)


ありがとうございました。
何とか解決することが出来ました。
また、質問するかも知れませんので、
そのときはよろしくお願いします。