Using _getdrive(), _getch(), _putch(), _getdcwd(), _chdrive(), _mkdir(), _rmdir(), system()
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Manipulating drive, directory and files
To show: Using _getdrive(), _getch(), _putch(), _getdcwd(), _chdrive(), _mkdir(), _rmdir() and system()
/* More example of the drive, directory and file */
#include <stdio.h>
#include <conio.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
int chr, drive, curdrive;
static char path[_MAX_PATH];
char buffer[_MAX_PATH];
char newdir[50] = "\\testdir";
// char path1[50] = "C:\\WINNT\\System32\\config"; - Windows 2000
char path1[50] = "C:\\Windows\\System32\\config";
/* Save current drive. */
curdrive = _getdrive();
printf("Available drives in this machine are: \n");
/* If we can switch to the drive, it exists. */
for(drive = 1; drive <= 26; drive++)
if(!_chdrive(drive))
printf("%c: ", drive + 'A' - 1);
printf("\n\nType drive letter to check: ");
chr = _getch();
if(chr == 27)
printf("Illegal drive input\n");
if(isalpha(chr))
_putch(chr);
if(_getdcwd(toupper(chr) - 'A' + 1, path, _MAX_PATH) != NULL)
printf("\nCurrent directory on that drive is:\n%s\n", path);
/* Restore original drive. */
_chdrive(curdrive);
/* Get the current working directory */
if(_getcwd(buffer, _MAX_PATH) == NULL)
perror("_getcwd error");
else
printf("\nCurrent working directory is: %s\n", buffer);
/* Create a directory and then delete */
if(_mkdir(newdir) == 0)
{
printf("\nDirectory %s was successfully created\n", newdir);
system("dir \\testdir");
if(_rmdir(newdir) == 0)
printf("\nDirectory %s was successfully removed\n", newdir);
else
printf("\nProblem removing directory %s\n", newdir);
}
else
printf("\nProblem creating directory %s\n", newdir);
/* Uses _chdir() function to verify that a given directory exists */
printf("\n");
printf("Change directory........\n");
if(_chdir(path1))
printf("Unable to locate the directory: %s\n", path1);
else
system("dir *.log /a");
printf("\n");
return 0;
}
Output example:
Available drives in this machine are:
A: C: E: F: G: J: K:
Type drive letter to check: c
Current directory on that drive is:
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
Current working directory is: F:\vc2005project\cplus\cplus
Directory \testdir was successfully created
Volume in drive F is hd0c
Volume Serial Number is 98DB-52B1
Directory of F:\testdir
12/10/2006 09:55 PM <DIR> .
12/10/2006 09:55 PM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 8,647,696,384 bytes free
Directory \testdir was successfully removed
Change directory........
Volume in drive C has no label.
Volume Serial Number is 6476-59E1
Directory of C:\Windows\System32\config
12/10/2006 09:10 PM 1,024 default.LOG
12/10/2006 09:10 PM 1,024 SAM.LOG
12/10/2006 09:20 PM 1,024 SECURITY.LOG
12/10/2006 09:55 PM 24,576 software.LOG
12/10/2006 09:16 PM 1,024 system.LOG
07/17/2005 04:55 AM 1,024 TempKey.LOG
07/17/2005 04:56 AM 1,024 userdiff.LOG
7 File(s) 30,720 bytes
0 Dir(s) 703,168,512 bytes free
Press any key to continue . . .