Thread: Finding out the name of a file, and changing directory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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;
    }

  2. #2
    Registered User
    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.

  3. #3
    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???

  4. #4
    Registered User
    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()

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, 09: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, 03:58 PM