ページ 1 / 1
opendir/readdirに対応するコマンド
Posted: 2005年11月04日(金) 22:54
by Sinryow
C(UNIX系のみ),およびPerl(Win/UNIXともに)に存在する「opendir」「readdir」等に相当することをABで(=API等を用いて)することは可能なのでしょうか。
ただし,FindFirstFile/FindNextFileを使うものを除きます(その方法なら私も分かります)。
FindFirstFile/FindNextFileでダメということはありませんが,もし他の方法が可能ならその方が効率が良いと思ったからです。
Posted: 2005年11月05日(土) 10:37
by konisi
http://www.google.com/search?num=50&hl= ... lr=lang_ja
ここらへん使って少し調べてみましたがどうやらないようです。
Perl言語は確かインタプリンタ言語ですので、インタプリンタを逆アセンブラなどして解析してActiveBasicに移植するのが一番手っ取り早いかと。
http://www.vector.co.jp/vpack/filearea/ ... index.html
また、ベクターでopendirと調べて出てきた物からどんどんたどっていったらこんなページにたどり着きました。
http://www3.tky.3web.ne.jp/~arsene/hsp/force/
健闘を祈ります。
Re: opendir/readdirに対応するコマンド
Posted: 2005年11月05日(土) 14:03
by NoWest
> C(UNIX系のみ),およびPerl(Win/UNIXともに)に存在する「opendir」「readdir」等に相当することをABで(=API等を用いて)することは可能なのでしょうか。
密かにWindows版のVC++でもopendirは使えます。
(かなり使い方は異なっていますが… 感覚的にはfopenに近い)
> ただし,FindFirstFile/FindNextFileを使うものを除きます(その方法なら私も分かります)。
fopenはcrtdllに入っているのでopendirも入っているかと調べてみても
dllにはopendirは含まれていないようなので、コンパイルされたEXEを探ったところVC++のopendirはVC++のコンパイラがFind~File系のAPIに置き換えているようです。ということでopendirに相当するAPIは無いっぽいです。
**追加情報 興味のある人だけどうぞ。** [ここをクリックすると内容が表示されます] [ここをクリックすると非表示にします]
MSDNにUNIXからの移植サンプルがありました。
コード: 全て選択
UNIX example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void ScanDir(char *dir, int indent)
{
DIR *dp;
struct dirent *dir_entry;
struct stat stat_info;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((dir_entry = readdir(dp)) != NULL) {
lstat(dir_entry->d_name,&stat_info);
if(S_ISDIR(stat_info.st_mode)) {
/* Directory, but ignore . and .. */
if(strcmp(".",dir_entry->d_name) == 0 ||
strcmp("..",dir_entry->d_name) == 0)
continue;
printf("%*s%s/\n",indent,"",dir_entry->d_name);
/* Recurse at a new indent level */
ScanDir(dir_entry->d_name,indent+4);
}
else printf("%*s%s\n",indent,"",dir_entry->d_name);
}
chdir("..");
closedir(dp);
}
int main(int argc, char* argv[])
{
char *topdir, defaultdir[2]=".";
if (argc != 2) {
printf("Argument not supplied - using current
directory.\n");
topdir=defaultdir;
}
else
topdir=argv[1];
printf("Directory scan of %s\n",topdir);
ScanDir(topdir,0);
printf("done.\n");
exit(0);
}
コード: 全て選択
Win32 example 1: using directory handling functions
This sample prints out the current directory, and then recurses through subdirectories. It uses the FindFirstFile(), FindNextFile(), and FindClose() Win32 API functions.
#include <windows.h>
#include <stdio.h>
void ScanDir(char *dirname, int indent)
{
BOOL fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
TCHAR szSubDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
printf("No files found\n\n");
}
else
{
// Traverse through the directory structure
fFinished = FALSE;
while (!fFinished)
{
// Check the object is a directory or not
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(FileData.cFileName, ".") != 0) &&
(strcmp(FileData.cFileName, "..") != 0))
{
printf("%*s%s\\\n", indent, "",
FileData.cFileName);
// Get the full path for sub directory
sprintf(szSubDir, "%s\\%s", dirname,
FileData.cFileName);
ScanDir(szSubDir, indent + 4);
}
}
else
printf("%*s%s\n", indent, "", FileData.cFileName);
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
}
}
}
FindClose(hList);
}
void main(int argc, char *argv[])
{
char *pszInputPath;
char pwd[2] = ".";
if (argc < 2)
{
printf("Argument not supplied - using current directory.\n");
pszInputPath = pwd;
}
else
{
pszInputPath = argv[1];
printf("Input Path: %s\n\n", pszInputPath);
}
ScanDir(pszInputPath, 0);
printf("\ndone.\n");
}
これを見る通りopendirの移植にはFind~File系のAPIを推奨しているようです。
Posted: 2005年11月05日(土) 22:03
by イグトランス
こういった操作は最終的には全てOSのAPIを呼ぶしかないと思います。
つまり他の方法でもWindowsでは結局FindFirstFile/FindNextFileの呼び出しになるのではないでしょうか。
(FindFirstFile/FindNextFileを使わず直接ディスクから読み出すというのは考えないことにします)
Posted: 2005年11月06日(日) 20:34
by NoWest
> こういった操作は最終的には全てOSのAPIを呼ぶしかないと思います。
> つまり他の方法でもWindowsでは結局FindFirstFile/FindNextFileの呼び出しになるのではないでしょうか。
> (FindFirstFile/FindNextFileを使わず直接ディスク空読み出すというのは考えないことにします)
と、いうことでopendir rewinddir readdir closedirをモジュール化したので
「実践コードモジュール」で参照してください。
Posted: 2005年11月07日(月) 18:18
by Sinryow
皆さん,ありがとうございます。
最終的にはすべてFind~Fileでやっているんですね。自分のものもFind~Fileで書くことにしました。