Finding out the name of a file, and changing directory

This is a discussion on Finding out the name of a file, and changing directory within the C Programming forums, part of the General Programming Boards category; [Edit]I am running Windows XP[/Edit] Hello. I have two questions: 1. How would you find the name of the next ...

  1. #1
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    Question Finding out the name of a file, and changing directory

    [Edit]I am running Windows XP[/Edit]

    Hello. I have two questions:

    1. How would you find the name of the next directory to go into, so you could chdir() to it?
    1. How would you assign the name of the first file in directory to an array?
    3. How would you find the name of the next file in a given directory (findnextfile() maybe)?

    And if you have to use findfirstfile() and/or findnextfile(), could you give an example of them. Thanks a bunch.
    Last edited by nindoja; 06-22-2004 at 03:19 PM. Reason: left out information

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If in linux, you might want to look here, the boards faq
    http://faq.cprogramming.com/cgi-bin/...&id=1045780608
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    whoops

    Whoops... Sorry for not telling what Operating System I use. I am running Windows XP. But thanks for the effort.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I am running Windows XP.
    Pipe the result of DIR into your program.
    My best code is written with the delete key.

  5. #5
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    right, but...

    Right, but how do I get the name of the directory that I need to change into. What I need to do is mass renaming of files in a given directory. The program will then go into the subsequent directories and rename all of the files. The program will be given the base directory, but it will have to get the name of the first subdirectory and go into it. Then, it will proceed to rename all of the files and go back to the base directory. After that, I will need to find the name of the next directory to go into. I know how to change directories, but I need to know how to get the directory name.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    IIRC, there's a perfectly good tree walker in the FAQ, if people would ever read it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    explain

    Alright, I looked at all of the examples, but they went completely over my head. If I can just get the names of functions to do this, that would be great. From there I can work out a program.

  8. #8
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    If you feel like using Windows C APi, which is very nice...
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<windows.h>
    
    int isDirGood(WIN32_FIND_DATA dir){
    	return	(dir.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    			&& (strcmp(dir.cFileName, "."))
    			&& (strcmp(dir.cFileName, ".."));
    }
    
    int isFileGood(WIN32_FIND_DATA file){
    	return	!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    }
    
    void renameFiles(char *startingDir){
    	HANDLE hTheFiles;
    	WIN32_FIND_DATA theFiles;
    
    	int len;
    
    	len = strlen(startingDir);
    	startingDir[len]='*';
    	startingDir[len+1]=0;
    	hTheFiles = FindFirstFile(startingDir, &theFiles);
    	startingDir[len]=0;
    
    	if(hTheFiles==INVALID_HANDLE_VALUE) return;
    
    	do{
    		if(isDirGood(theFiles)){
    			sprintf(startingDir+len, "%s\\", theFiles.cFileName);
    			//puts(startingDir);
    			renameFiles(startingDir);
    			startingDir[len]=0;
    		}else if(isFileGood(theFiles)){
    			//RENAME THE FILE
    			printf("FILE: %s%s renamed\n", startingDir, theFiles.cFileName);
    		}
    
    
    	}while(FindNextFile(hTheFiles, &theFiles));
    
    	FindClose(hTheFiles);
    }
    
    
    char *dirToFullPath(char *s){
    	char curDir[MAX_PATH];
    
    	if(!s) s = (char*)calloc(1, MAX_PATH);
    
    	if(!*s){//no dir specified
    		GetCurrentDirectory(MAX_PATH, s);
    		if(s[strlen(s)-1]!='\\')
    			strcat(s,"\\");
    	}
    	else if(s[1]==':'){//full path dir
    			if(s[strlen(s)-1]!='\\')
    				strcat(s,"\\");
    	}
    	else{//relative dir
    		GetCurrentDirectory(MAX_PATH, curDir);
    		if(curDir[strlen(curDir)-1]!='\\')
    			strcat(curDir,"\\");
    
    		if(*s=='\\') s++;
    		if(s[0] && s[strlen(s)-1]!='\\')
    			strcat(s,"\\");
    
    		strcat(curDir,s);
    		strcpy(s, curDir);
    	}
    	return s;
    
    }
    
    main(){
    	char s[1000];
    	strcpy(s, YOUR_DIR_HERE);
    	dirToFullPath(s);
    	renameFiles(s);
    
    }
    It's all done... just add your renaming criteria to the renameFiles function, and add more arguments to it if you want.
    Maybe you want to receive a directory by command line... If so the dirToFullPath is very good because it allows you to write any directory name, and changes it to its fullpath.
    e.g: c:\dir\dir2> rename mydir3\mydir2
    that input will make the startingDir string look like
    "c:\dir\dir2\mydir3\mydir2".
    The only problem is to allocate enough memory to the dir name buffer, but that's not really a problem...
    Plus... "if(s[1]==':')" this condition works only for windows. If you want to make your app work in other OS you should change that condition.
    Last edited by xErath; 06-22-2004 at 07:23 PM.

  9. #9
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    tried that

    Thanks a bunch, but I tried that (copied and pasted the code). The only change that I made was what directory to use at the bottom. When I run it, I get nothing... No window, no alerts, nothing. But it compiles well.

  10. #10
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by nindoja
    Thanks a bunch, but I tried that (copied and pasted the code). The only change that I made was what directory to use at the bottom. When I run it, I get nothing... No window, no alerts, nothing. But it compiles well.
    Make sure your using a directory that exists
    If not:
    if(hTheFiles==INVALID_HANDLE_VALUE) return;
    the function returns without doing nothing.
    Debug the function to find out where the problem is.
    You could also put something like puts(startingDir) at the beginning of the function.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >that input you make the startingDir string look like
    >"c:\dir\dir2\mydir3\mydir2".

    It had better look more like this:
    "c:\\dir\\dir2\\mydir3\\mydir2".

  12. #12
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    nope

    Nope, that didn't work. So here is my code:
    Code:
     #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<windows.h>
    int isDirGood(WIN32_FIND_DATA dir){
     return (dir.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
       && (strcmp(dir.cFileName, "."))
       && (strcmp(dir.cFileName, ".."));
    }
    int isFileGood(WIN32_FIND_DATA file){
     return !(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
    }
    void renameFiles(char *startingDir){
     HANDLE hTheFiles;
     WIN32_FIND_DATA theFiles;
     int len;
     
     
     
     
     puts(startingDir);
     
      
      
     len = strlen(startingDir);
     startingDir[len]='*';
     startingDir[len+1]=0;
     hTheFiles = FindFirstFile(startingDir, &theFiles);
     startingDir[len]=0;
     if(hTheFiles==INVALID_HANDLE_VALUE) return;
     do{
      if(isDirGood(theFiles)){
       sprintf(startingDir+len, "%s\\", theFiles.cFileName);
       //puts(startingDir);
       renameFiles(startingDir);
       startingDir[len]=0;
       printf("hahahahahahahahahahahahaha");
      }else if(isFileGood(theFiles)){
       //RENAME THE FILE
       printf("FILE: %s%s renamed\n", startingDir, theFiles.cFileName);
      }
    
     }while(FindNextFile(hTheFiles, &theFiles));
     FindClose(hTheFiles);
    }
    
    char *dirToFullPath(char *s){
     char curDir[MAX_PATH];
     if(!s) s = (char*)calloc(1, MAX_PATH);
     if(!*s){//no dir specified
      GetCurrentDirectory(MAX_PATH, s);
      if(s[strlen(s)-1]!='\\')
       strcat(s,"\\");
     }
     else if(s[1]==':'){//full path dir
       if(s[strlen(s)-1]!='\\')
    	strcat(s,"\\");
     }
     else{//relative dir
      GetCurrentDirectory(MAX_PATH, curDir);
      if(curDir[strlen(curDir)-1]!='\\')
       strcat(curDir,"\\");
      if(*s=='\\') s++;
      if(s[0] && s[strlen(s)-1]!='\\')
       strcat(s,"\\");
      strcat(curDir,s);
      strcpy(s, curDir);
     }
     return s;
    }
    int main(void){
     char s[1000];
     char a[1000]="c:\\test";
     strcpy(s, a);
     dirToFullPath(s);
     renameFiles(s);
     return 0;
    }

  13. #13
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Hum.. i'm not getting the problem.
    It works fine with me. Debug the source.. What IDE are you using?? Put a breakpoint before the cicle, and also before the return... Or put a printf() if you like before and after the return, and inside the cicle, outside the ifs.
    For shure, it's the return that finishes the program.
    Last edited by xErath; 06-22-2004 at 07:55 PM.

  14. #14
    C n00b
    Join Date
    Jun 2004
    Posts
    8

    nope

    I tried it using Visual C++ and Dev-C++, but both to no avail. I put break points on every line, and even then it won't work. Since we can't get it to work, does anyone know how to do it via a console application???

  15. #15
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by nindoja
    I tried it using Visual C++ and Dev-C++, but both to no avail. I put break points on every line, and even then it won't work. Since we can't get it to work, does anyone know how to do it via a console application???
    If you're using Visual C++ place a breakpoint at the beginnig of the function renameFiles(), and when the breakpoint is reached press F10 to go step by step, and see where the function exits. Plus do a printf() of the directory name after calling dirToFullPath()

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  4. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 10:33 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21