-
Need a simple program!
Hi all,
It's been some time since I last touched programming, but if anyone is free, here's a simple request which I'm sure most programmers can churn out in 5 mins or less. :D
Basically, I need a program that will loop through a folder and get the names of all the files in that folder, and output them in a list box or text box.
To make things even simpler, all the user need is to place the program in the same folder, click a button and and it will loop through all the files and get the file names etc.
Or if anyone has a link to a program like that, let me know as well. :wave:
Thanks!
Xeon.
-
Re: Need a simple program!
Your right - it's simple. I can guide you through it in 6 easy steps, and you don't have to even fire up your favorite code generator....
Please note that these instructions are tailored for Windows users...
1) Open a command window
2) Navigate to the desired folder
3) type 'dir > filename.txt' (insert the filename of your choice here)
4) hit Enter
5) type 'notepad filename.txt' (same filename as above)
6) View the list you just created in all it's glory in the Notepad textbox. :thumb:
If you really want to get fancy, you can add a 7th step, and copy the above steps into a batch file :-)
Enjoy!!
-
Re: Need a simple program!
Here's how you find files in the directory that this .exe resides in:
Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
CHAR szDir[MAX_PATH];
GetCurrentDirectoryA(sizeof(szDir), szDir);
printf("Path: %s\n\n", szDir);
strcat(szDir, "\\*");
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(szDir, &ffd);
do
{
if(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
LARGE_INTEGER filesize;
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
printf("Name: %s\t\tSize: %ld\n", ffd.cFileName, filesize.QuadPart);
}
}
while(FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
puts("\nOperation Complete.");
getchar();
return 0;
}
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
Hi all,
It's been some time since I last touched programming, but if anyone is free, here's a simple request which I'm sure most programmers can churn out in 5 mins or less. :D
Basically, I need a program that will loop through a folder and get the names of all the files in that folder, and output them in a list box or text box.
To make things even simpler, all the user need is to place the program in the same folder, click a button and and it will loop through all the files and get the file names etc.
Or if anyone has a link to a program like that, let me know as well. :wave:
Thanks!
Xeon.
Listing the Files in a Directory
http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx
-
Re: Need a simple program!
Thanks a lot, guys! Actually, this program is for a friend of mine who wants to list the file names of all the videos in his folder (he has hundreds of videos), and he needs to send the list to me.
The steps given by gjs is very good, but even I myself is stuck at that cos' I've never really touched MS-DOS in my life, same as my friend.
So, if anyone could generate a small simple program doing the above and attach it to this thread, I would be grateful! (specs: looping through the folder, getting all the file names and putting them in text box for easy copying)
I wanted to copy ashukasama and Bitshifter's code to create my own program yesterday, but I found my VC++ 6.0 software was missing since 2 years ago!
And this is not school homework! View my past posts and you'll see I'm some sort of half-boiled shareware programmer at one time. :D
-
Re: Need a simple program!
Just do what gjs368 suggested. It'll take 30 seconds. No program needed.
-
Re: Need a simple program!
Xeon,
It's obvious that you can type... :D
And as mentioned above, this is really NOT that complicated. The hardest part of it is the same no matter.. You have to navigate to the folder that you want to list. If you want it simpler than typing the path, well then, just open up Windows Explorer and navigate to the folder that you want a listing from with your mouse, copy the FULL directory listing from the address bar to the Clipboard, and paste it into a command window for step 2 of my original post (at the cursor, type in the two letters cd <change directory> then a space, and then right click in the window to paste the address you copied in from the Explorer address bar).
-
Re: Need a simple program!
Thanks a lot, guys.
My problem is that when I open up the DOS window, the directory path is at:
C:\Documents and Settings\Xeon >
The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"
So, I tried to type the following:
C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
But it gives an error saying "paramete format not correct".
I've also tried putting quotes around my paths but it still doesn't work.
Thanks!
Xeon
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
Thanks a lot, guys! Actually, this program is for a friend of mine who wants to list the file names of all the videos in his folder (he has hundreds of videos), and he needs to send the list to me.
The steps given by gjs is very good, but even I myself is stuck at that cos' I've never really touched MS-DOS in my life, same as my friend.
The command-line is not MS-DOS. MS-DOS is the old 16-bit operating system that was popular during the 1980's and early 1990's.
But in all seriousness, any self-respecting Windows programmer should know how to use the command-line. There really is no excuse for a programmer not knowing simple commands such as "dir", "copy", "cd", etc.
Regards,
Paul McKenzie
-
Re: Need a simple program!
As gjs368 said, put this code into into a file then give it a .bat extension.
When you double click on the batch file it will spit the results into a text file.
This searches the current directory for all file types and sub-directories.
It doesn't get any easier than this...
Code:
@echo off
dir > results.txt
pause
-
Re: Need a simple program!
Bitshifter, using GetCurrentDirectoryA can come back to bite you....
Quote:
Originally Posted by bitshifter420
Here's how you find files in the directory that this .exe resides in:
Code:
#include <windows.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
CHAR szDir[MAX_PATH];
GetCurrentDirectoryA(sizeof(szDir), szDir);
printf("Path: %s\n\n", szDir);
strcat(szDir, "\\*");
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(szDir, &ffd);
do
{
if(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
LARGE_INTEGER filesize;
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
printf("Name: %s\t\tSize: %ld\n", ffd.cFileName, filesize.QuadPart);
}
}
while(FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
puts("\nOperation Complete.");
getchar();
return 0;
}
Note what the function returns:
Quote:
GetCurrentDirectoryA( )
This function returns the current working directory into a string
The current working directory may not be the one your app launched from. Here is a VC++ 6.0 function that should ALWAYS get you the directory the application resides in:
Code:
void GetAppDirectory(CString& szDirectory)
{
TCHAR szPath[(MAX_PATH + 1)];
TCHAR *szTemp = NULL;
GetModuleFileName(NULL, szPath, (sizeof(TCHAR) * MAX_PATH));
szTemp = strrchr(szPath, _TCHAR('\\'));
if (szTemp) *szTemp = NULL;
szDirectory.Format(_T("%s"), szPath);
}
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
Thanks a lot, guys.
My problem is that when I open up the DOS window, the directory path is at:
C:\Documents and Settings\Xeon >
The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"
So, I tried to type the following:
C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
But it gives an error saying "paramete format not correct".
I've also tried putting quotes around my paths but it still doesn't work.
We can't help you directly, since we don't have access to your computer. We don't know if that directory really exists. We don't know if you're really typing in that command-line. We don't know anything -- this needs to be diagnosed by you by first knowing how to use the cd command properly.
Can you change to the root directory?
If you can, then go up one folder at a time:
Code:
cd Documents and Settings
If this takes you to the directory, then go up another level. If it doesn't then your directory is not named "Documents and Settings", or it is hidden and you can't get access to it, or some other error we can't diagnose from our end.
Regards,
Paul McKenzie
-
Re: Need a simple program!
Quote:
Originally Posted by gjs368
Bitshifter, using GetCurrentDirectoryA can come back to bite you....
Yes, that's not my normal practice, i was trying to keep the code small.
I should have supplied a function that strips the name off of argv[0].
Anyway, thanks for the constructive criticism.
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
Thanks a lot, guys.
My problem is that when I open up the DOS window, the directory path is at:
C:\Documents and Settings\Xeon >
The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"
So, I tried to type the following:
C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
But it gives an error saying "paramete format not correct".
I've also tried putting quotes around my paths but it still doesn't work.
Thanks!
Xeon
I have mislead you a bit, sorry... The command line does not know how to deal with the spaces (Just like MS to go half-way by letting you use spaces and then have grief over trying to get there....) unless you enclose the path in quotes... Try this:
cd "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"
-
Re: Need a simple program!
Thanks guys!
But my main problem is with the "change directory", as follows (posted above previously):
Quote:
When I open up the DOS window, the directory path is at:
C:\Documents and Settings\Xeon >
The path to the folder containing the vids is "C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder"
So, I tried to type the following:
C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
But it gives an error saying "paramete format not correct".
I've also tried putting quotes around my paths but it still doesn't work.
I'm sure if I can get around this, everything would be smooth. :D
And yeah, it's not surprising for noob programmers not to know how to use these command-line stuff if all they ever did was MFC visual stuff all done in Windows 98 / XP interface. :)
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
C:\Documents and Settings\Xeon > cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
Xeon... Does your computer really have the TWO folder paths
C:\Documents and Settings\Xeon
and
C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
as you show from your post?
Is it possible that you want to navigate to
C:\Documents and Settings\Xeon\Desktop\VidsFolder
instead?
-
Re: Need a simple program!
I would say this is the prompt:
C:\Documents and Settings\Xeon >
And this is the response:
cd C:\Documents and Settings\ops-Xeon\Desktop\VidsFolder
He is changing directory via cd.
-
Re: Need a simple program!
No, my prompt doesn't have two folder paths, I'm just trying to cd from there, as what Bitshifter above says.
Anyway, I'll try again tonight, and if I can't, I will start hunting around for my VC++ CD since coding the thing from scratch is easier than this......this insanity!!!!! %((&*&$!
-
Re: Need a simple program!
Xeon,
At the command prompt, type cd .. (to back up 1 level), then type dir - Like this:
Code:
C:\Documents and Settings\Xeon >cd ..
Code:
C:\Documents and Settings\ >dir
Post the list of directories that show up when you have done that.
-
Re: Need a simple program!
Heh Xeon, good to hear from you again. Pretty funny that you are having trouble with the command line, but then the windows command line leaves a lot to be desired.
-
Re: Need a simple program!
Code:
void iterateFolder()
{
WIN32_FIND_DATA fileInfo;
lstrcat(rootPath,_T("\\*.*")); // where rootPath is the path to folder, use SHBrowseForFolder() to get path to folder.
HANDLE fileHandle = FindFirstFile(rootPath,(LPWIN32_FIND_DATA)&fileInfo);
if(fileHandle != INVALID_HANDLE_VALUE)
{
while(FindNextFile(fileHandle,(LPWIN32_FIND_DATA)&fileInfo))
{
//here you will get the filenames in fileInfo.cFileName
}
}
FindClose(fileHandle);
}
cheers:)
-
Re: Need a simple program!
Code:
C:\Documents and Settings\Xeon > help
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
And yeah, it's not surprising for noob programmers not to know how to use these command-line stuff if all they ever did was MFC visual stuff all done in Windows 98 / XP interface. :)
So when you create a simple program like this, how do you run it on a friends machine that doesn't have Visual C++ installed?
Code:
#include <iostream>
int main()
{
std::cout << "Hello World";
}
Won't it seem strange that you can't run a simple "Hello World" program, but at the same time claim to be a Windows programmer?
The command-line has been an integral part of Windows. IMO, any programmer whos realm is Windows programming is seriously deficient (and I mean seriously) in the knowledge of the OS if they do not know how to use the command-line, even for simple commands.
Regards,
Paul McKenzie
-
Re: Need a simple program!
To gjs368: Finally, the "cd .." is the trick that solves all the problems! With this trick, I can navigate to any directory I want! Cheers! :D
To SoulDog: It's been years! I still remember you, your ID and your avatar! Seems that for a lot of people here, once they've set an avatar, it's for life! :D
From Paul:
Quote:
So when you create a simple program like this, how do you run it on a friends machine that doesn't have Visual C++ installed?
Why does he need VC++? If I still had my VC++ compiler, I just need to paste the folder-looping code and compile the program into an EXE file and send the file to him, and he'll place the file in his folder and execute the program and it'll do the rest. :cool:
Quote:
Won't it seem strange that you can't run a simple "Hello World" program, but at the same time claim to be a Windows programmer?
Actually, for the command line, I only know how to open the window (Start menu > Run > command).
And the only commands I know of are: cd and dir.
I dunno anything else.
And from the VC++ studio, all I need is to click the Execute button to run my Win32 DOS programs, I don't even need anything. That software shielded me from everything.
Quote:
The command-line has been an integral part of Windows. IMO, any programmer whos realm is Windows programming is seriously deficient (and I mean seriously) in the knowledge of the OS if they do not know how to use the command-line, even for simple commands.
As long as the Windows programmer isn't creating any low-level software, he doesn't even need to know how to use the command line.
If you see those "Teach yourself VC++ ... in 21 days" or Jeff Prosise's MFC programming kind of books, they don't even mention the command line.
All they ever talk about are C++ code, VC++ software usage and MFC.
IMO, it's possible for a Windows programmer to write functional Windows programmers without even knowing there's such a thing called "command line".
Now, pls zip your mouth, go to bed and be a good little boy.
You should laugh more and be more tolerant of noobs.
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
As long as the Windows programmer isn't creating any low-level software, he doesn't even need to know how to use the command line.
There is a lot of software that is not low-level, but is started via command-line. For example, we have a utility that automatically scans images via TWAIN. The program is started from the command-line, however the retrieval of images is done via whatver UI of the scanner happens to be.
There are other utilities that do things like append image files together, start database engines, etc. These are not "low-level", as GUI wrappers can and have been written for these types of programs.
Quote:
If you see those "Teach yourself VC++ ... in 21 days" or Jeff Prosise's MFC programming kind of books, they don't even mention the command line. All they ever talk about are C++ code, VC++ software usage and MFC.
And if you were to get general C++ books, no mention is made of GUI, clicking on icons or anything else. You are only given a C++ program, similar to what I posted.
Quote:
IMO, it's possible for a Windows programmer to write functional Windows programmers without even knowing there's such a thing called "command line".
Try to find a Windows job with not knowing anything about simple comand-line commands. You will get strange looks by anyone who is a Windows professional in C++, no matter what applications they happen to be producing, whether they are all GUI, low level, mid level or whatever.
Quote:
You should laugh more and be more tolerant of noobs.
The ironic thing is that general C++ books for newbies assume you know how to use the command-line.
Regards,
Paul McKenzie
-
Re: Need a simple program!
Quote:
Originally Posted by Xeon
Why does he need VC++? If I still had my VC++ compiler, I just need to paste the folder-looping code and compile the program into an EXE file and send the file to him, and he'll place the file in his folder and execute the program and it'll do the rest. :cool:
I am referring to the simle "Hello World" program that I posted. Also by "friend", I could also mean any other computer that doesn't have VC++ installed.
Quote:
Actually, for the command line, I only know how to open the window (Start menu > Run > command).
And the only commands I know of are: cd and dir.
Hopefully you also know how to run executable programs. All you need to do is type in the name of the exe and hit enter.
Quote:
As long as the Windows programmer isn't creating any low-level software, he doesn't even need to know how to use the command line.
I'll add that many Java programmers will disagree with you.
Regards,
Paul McKenzie
-
Re: Need a simple program!
Xeon,
One last tidbit of info regarding Windows Command line stuff... Most all of the built-in functions (like cd) offer some help (sort of like UNIX man pages).
If you want to learn more about a command, type the name of the command follwed by a space and an then /? like this:
C:\>cd /?
If help is available (it is for the cd command), the help information for the related command will be output to the command window for your review.
If you want to learn more about other command line tools that come with the Windows cmd shell, type help at the command line prompt. It will return a pretty good list of useful utilities and a brief summary of what those commands do.
-
Re: Need a simple program!
-
Re: Need a simple program!
Quote:
Originally Posted by gjs368
(Just like MS to go half-way by letting you use spaces and then have grief over trying to get there....)
Well, it's the same for every shell, something has to be done to indicate that it's not parameters. In Linux (Debian) the standard way is using \ before every space character, can't say that's better (quotes are supported as well)
Edit: By the way, it took a long time but the shell now (since NT4 I think) have one of the *nix shell's handy functions "TAB to complete the line"
Try typing cd and then hit TAB and a dir will be added to your command line. If it's not the correct dir hit TAB again until it is. Giving the first character of the dir before pressing TAB speeds it up even more.