I am trying to copy files from one directory to another. What I don't know how to do is get a file name from a directory using ansi C.
Any help would be appreciated.
Printable View
I am trying to copy files from one directory to another. What I don't know how to do is get a file name from a directory using ansi C.
Any help would be appreciated.
It depends on what compiler you are using. So what compiler are you using? If you are using VC++6 than you can do whatever you might want to do from the command prompt with the system command, for example run this (if you have VC++6):
Code:#include<stdio.h>
#include<stdlib.h>
int main()
{
system("dir c:");
return 0;
}
I am using MS VC++ 6. I tried the system function but all it does is print the directory to the screen. I still don't see how I can get the file names into a variable.
Thanks for your response.
Garth
Put it into a text file first. Can you take it from there?Code:#include<iostream>
using namespace std;
int main()
{
system("dir c:\\ >> c:\\log.txt");
return 0;
}
I don't want to show you how to create a computer virus. I hope thats not what you want the information for.
Ohh come on. Just use a switch
dir c:\\ /B
Witch_King,
I already got your previouse response and I'm doing fine. I didn't post the last response you just replied to.
gls
This could save some time. Notice the /B switch.Code:#include<iostream>
using namespace std;
int main()
{
system("dir /B c:\\ >> c:\\log.txt");
return 0;
}
That way you just get the filenames. Nice and clean. Yup I got your email.
Also say you want to get into a folder with some spaces in the name. Than put a backslash before the quote like this.Code:#include<stdio.h>
#include<stdlib.h>
int main()
{
system("dir /B c:\\\"Program Files\" >> c:\\log.txt");
return 0;
}
If you want to avoid using the slow system routine, since your using MSVC you could do something like this -
Code:#include <windows.h>
int main()
{
char filename1[]="sourcedirectory\\*.*";
char filename2[255]="targetdirectory\\";
BOOL exists=0;
HANDLE filehandle;
WIN32_FIND_DATA find_data;
filehandle = FindFirstFile(filename1,&find_data);
strcat(filename2,find_data.cFileName);
CopyFile(find_data.cFileName,filename2,exists);
strcpy(filename2,"targetdirectory\\");
while(FindNextFile(filehandle,&find_data))
{
strcat(filename2,find_data.cFileName);
CopyFile(find_data.cFileName,filename2,exists);
strcpy(filename2,"targetdirectory\\");
}
return 0;
}
How does it work? What is the input?
filename1 - the source directory + *.* (to indicate to FindNextFile() that all files are required).
filename2 - the target directory (which is then appended with the filename that FindFirstFile/FindNextFile produces).
If I had a .txt file in the c:\temp directory isn't it supposed to copy it to the c:\ directory? Just trying to figure it out... Where did you go? I'm trying to get this thing to work...Code:int main()
{
char filename1[]="c:\\Temp\\*.txt";
char filename2[255]="c:\\";
...
Sorry, FindFirstFile() doesn't return the full path name you'd have to something like this -
Code:#include <windows.h>
int main()
{
char filename1[]="c:\\temp\\*.txt";
char filename2[255]="c:\\";
char fullpath[255]={0};
BOOL exists=0;
HANDLE filehandle;
WIN32_FIND_DATA find_data;
SetCurrentDirectory("c:\\temp\\");
filehandle = FindFirstFile(filename1,&find_data);
strcat(filename2,find_data.cFileName);
GetFullPathName(find_data.cFileName,sizeof(fullpath),fullpath,0);
CopyFile(fullpath,filename2,exists);
strcpy(filename2,"c:\\");
while(FindNextFile(filehandle,&find_data))
{
strcat(filename2,find_data.cFileName);
GetFullPathName(find_data.cFileName,sizeof(fullpath),fullpath,0);
CopyFile(fullpath,filename2,exists);
strcpy(filename2,"c:\\");
}
return 0;
}
Yeah, that son of a ***** worked. Good job. I'll keep this program.