Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2005, 2:45 PM   #1
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
How to get file listing of a dir

Is there any way to get a list of the files in a dir with win api or c++ standard libs?
some1 is offline   Reply With Quote
Old Mar 27th, 2005, 5:25 PM   #2
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
are you familiar with the "dir" and "cd" commands and the command prompt?
Klick is offline   Reply With Quote
Old Mar 27th, 2005, 5:31 PM   #3
Klick
Newbie
 
Join Date: Mar 2005
Posts: 7
Rep Power: 0 Klick is on a distinguished road
If so then, use the "system" command in C++ to change to the directory you want and view a list of files. Here the code:

#include <cstdlib>

system ("command");
system ("cd\\");
// if you wanted to switch to the windows directory for instance.
system ("cd windows");
system ("dir"); //Displays a list of the files in that directory.
// or use this is there are a lot of files in that directory and they can't all be fit
// on the command prompt window:
system ("dir /p");
Klick is offline   Reply With Quote
Old Mar 29th, 2005, 7:45 AM   #4
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
Quote:
Originally Posted by Klick
If so then, use the "system" command in C++ to change to the directory you want and view a list of files. Here the code:

#include <cstdlib>

system ("command");
system ("cd\\");
// if you wanted to switch to the windows directory for instance.
system ("cd windows");
system ("dir"); //Displays a list of the files in that directory.
// or use this is there are a lot of files in that directory and they can't all be fit
// on the command prompt window:
system ("dir /p");
thanks

is there any way to do this with windows api?
some1 is offline   Reply With Quote
Old Mar 29th, 2005, 8:26 AM   #5
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
search for FindFirstFile and FindNextFile for windows api implementations
spydoor is offline   Reply With Quote
Old Mar 31st, 2005, 10:21 AM   #6
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
Ah, I don't want to burst your bubble, but system() forks a new shell so the present working directory should only be changed as long as that shell is running; e.g.

pwd is /home/mackenga
code does: system("cd ..");
pwd is /home/mackenga

In this scenario, I do go into the /home directory, but only until the system() function returns.

Another problem with your method is: what if you want your program to know a list of the files, rather than just display said list to the user without actually getting any information into your application about what's there?

The usual way to read directories in C, Perl, etc., is using opendir() then readdir(). Well, I'm /sure/ this applies to Perl, and suspect C too since most things do though I have to blush and confess I've never read a dir from C as far as I can remember (terrible, isn't it?).
mackenga is offline   Reply With Quote
Old Mar 31st, 2005, 11:49 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
http://www.metalshell.com/view/source/116/
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 2nd, 2005, 2:10 PM   #8
Floppie
Newbie
 
Join Date: Feb 2005
Posts: 18
Rep Power: 0 Floppie is on a distinguished road
I'm having this same problem. I need the "right" (non-system()) way to do it. I run a webserver of my own creation, and I implemented this for auto-file indexing. I manipulated it to output the dir command to a file (system("dir > temp.txt");) and read it back in, but if two people try to request a directory (same or not) at once, the server crashes.

Thank you for that link. <3

Last edited by Floppie; Apr 2nd, 2005 at 2:55 PM.
Floppie is offline   Reply With Quote
Old Apr 2nd, 2005, 2:57 PM   #9
some1
Programmer
 
Join Date: Dec 2004
Posts: 50
Rep Power: 4 some1 is on a distinguished road
FindFirstFile() and FindNextFile() work perfectly thanks
some1 is offline   Reply With Quote
Old Apr 2nd, 2005, 3:19 PM   #10
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
File locking

To avoid problems with multiple simultaneous requests crashing your server or producing undesirable behaviour, you'll really need to look into file locking. You can use portable advisory file locking (flock) from a variety of languages and it works under most circumstances (not great over an NFS share though).

An alternative for your problem might be to use unique filenames for the requests; to do this properly, include the process ID and the current system time in your tempfile names and the chances of a clash are very slim (two processes would have to get the same pid in the same second, which is highly unlikely).
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC