Menu

#3 dir.exe cannot be called from program

open
nobody
None
8
2009-08-28
2009-08-28
No

After some testing, I found out that dir.exe does not run from system(). It does run perfectly from command line though. I think it's a command within cmd.exe.
Does anybody know a workaround or maybe another method to get the file names from the files directory?

Function: void runScan()

This is required for the functioning of the Windows version.

Discussion

  • Anonymous

    Anonymous - 2009-08-30

    Hi Robert,
    Fixed the bug. Find the code below for the function runScan()

    #include <dirent.h>
    .....
    .....
    .....

    //----------------------------------------------------------------------------------
    // CUT HERE
    // replaced entire function for runScan()
    // I have'nt done any error handling and used c type file operations

    void runScan()
    {
    DIR *filesDIR;
    struct dirent *pent;

    int LengthOfString = 0;

    FILE * fp1;
    FILE * fp2;

    remove("securedfilelist");
    remove("filelist");

    fp1 = fopen("filelist","wb");
    fp2 = fopen("securedfilelist","wb");

    filesDIR = opendir("files");

    while ( (pent=readdir(filesDIR)) )
    {

    LengthOfString = strlen(pent->d_name);

    // to remove ten "." and ".." from the listing
    if( !(strcmp (pent->d_name,".")) || !(strcmp (pent->d_name,"..")))

    continue;

    else if(strcmp ( (&(pent->d_name) [ LengthOfString - 8]),"_secured"))
    {
    fputs(pent->d_name,fp1);
    fputs("\n",fp1);
    }
    else
    {
    fputs(pent->d_name,fp2);
    fputs("\n",fp2);
    }
    }

    closedir(filesDIR);
    fclose(fp1);
    fclose(fp2);
    }
    // CUT TILL HERE
    // -------------------------

     
  • Robert Alblas

    Robert Alblas - 2009-09-05

    Thanks for the code, it's looks like an awesome solution, since system commands are not always reliable. You can't be sure that the command exists on every system.

    This code however, can run on any system, also cross platform.

    There is one problem, it doesn't handle subdirectories the right way. The function should descent into subdirecortories rather than displaying them as files.

    I'll try to fix this.

     
  • Nobody/Anonymous

    well I had not tested the module completely.

    google on the dirent.h and I am sure you will find something..

    I will do the same activity simultaneously and post if find anything useful.

    cheers

     
  • Robert Alblas

    Robert Alblas - 2009-09-07

    I think I got something, I'm working it out tonight.

     
  • Nobody/Anonymous

    Right.. I tried, but it doesn't appear to work. The following code does compile (replacing void runscan() by it) but it doesn't write anything to the files.

    It's kind of ugly code, as I'm using C++ strings with C strings and mixing up all functions..
    I'm using mingw32 and wine to test it, so my test may not me 100% accurate (yes.. I screwed my windows partition..) I'll post it here anyway:

    void getFiles(string directory)
    {
    DIR *filesDIR;
    struct dirent *pent;

    int LengthOfString = 0;

    FILE * fp1;
    FILE * fp2;

    fp1 = fopen("filelist","wb");
    fp2 = fopen("_securedfilelist","wb");

    filesDIR = opendir(directory.c_str());

    while ( (pent=readdir(filesDIR)) )
    {
    //check if pent is a file or directory
    bool fileIsDir;
    errno = 0;
    DIR *fileOrDir;
    fileOrDir = opendir(pent->d_name);
    if (errno == ENOTDIR) fileIsDir = false;
    else fileIsDir = true;

    LengthOfString = strlen(pent->d_name);

    // to remove ten "." and ".." from the listing
    if( !(strcmp (pent->d_name,".")) || !(strcmp (pent->d_name,"..")))

    continue;

    else if (fileIsDir)
    {
    string directoryArg = directory + pent->d_name + "\\";
    getFiles(directoryArg.c_str());
    }

    else if(strcmp ( (&(pent->d_name) [ LengthOfString - 8]),"_secured"))
    {
    fputs(directory.c_str(),fp1);
    fputs(pent->d_name,fp1);
    fputs("\n",fp1);
    }
    else
    {
    fputs(directory.c_str(),fp2);
    fputs(pent->d_name,fp2);
    fputs("\n",fp2);
    }
    }

    closedir(filesDIR);
    fclose(fp1);
    fclose(fp2);
    }

    void runScan()
    {
    remove("_securedfilelist");
    remove("filelist");

    getFiles(".\\files\\");
    }

     
  • Anonymous

    Anonymous - 2009-09-22

    Hi Robert,

    I had been a little busy with Holy month of Ramadan.

    i will start looking into this topic from now on.

    I was looking at a recursive approach to solve the problem.

    feedback on the same.

    Regards,
    younus saleem saifullah

     
  • Robert Alblas

    Robert Alblas - 2009-11-05

    I've found working code on dreamincode.net for the Windows function of this. It's using windows.h. We will have to make a different Windows version of this function, which is too bad. The Linux and other Unices versions will depend on GNU find (findutils).

    This will leave us with a huge portability problem, one of my philosophies for this application. If anyone finds a cross platform solution for this, PLEASE tell me :)

    Next version is to be released very soon, now I have this one done.

     
  • Robert Alblas

    Robert Alblas - 2009-11-05

    #include <windows.h>
    #include <iostream>
    #include <string>

    //Thanks to Lillefix from dreamincode.net for this code.

    using namespace std;

    void list(string name)
    {
    HANDLE hFind;
    WIN32_FIND_DATA FindData;
    string actualSearch = name + "\\*.*";

    // Find the first file
    hFind = FindFirstFile(actualSearch.c_str(), &FindData);

    // Look for more
    do
    {
    //If no files are found
    if (hFind == INVALID_HANDLE_VALUE)
    {
    //Done checking this folder
    return;
    }

    string fileName (FindData.cFileName);

    //If the file is a self-reference
    if (fileName == "." || fileName == "..")
    {
    //Skip to next file
    continue;
    }

    //If the file is a folder
    else if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    cout << name << fileName << endl;
    list(name + "\\" + fileName);
    }
    //If the file is a file
    else
    {
    cout << name << "\\" << fileName << endl;
    }
    } while (FindNextFile(hFind, &FindData));

    FindClose(hFind);
    }

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.