File Renaming: Permission denied

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Icyhot
    New Member
    • Oct 2012
    • 6

    File Renaming: Permission denied

    Hello world! I am writing a C++ program that finds the Word Documents on your computer that have '+' or "%2B" in the name and replacing those '+' and "%2B" with ' '. I am writing this program for my school's University Writing Center, so I predict the only places they will have these files are the USB memory stick, Desktop, Documents, and Downloads. I encounter a problem, however, when I run this program and it renames the files in Downloads: Permission denied. I am using the windows.h to do this. How do I give the program permissions to that folder?

    update:
    This program is going to be written in C++.
    Last edited by Niheel; Oct 10 '12, 06:11 AM. Reason: merged information, this thread was moved back to C++, user is asking about a C++ programming question on the windows platform.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Several reason can be available for an application to fail renaming.

    first thing is you cant have a file with name that include
    \/:*?"<>| ;anyway that is not the problem you are having

    you didnt say if it is failing with all kind of doc file. The doc file you are trying to rename may be in use. you can't delete or rename a doc file which is possessed ;) by Application.

    You may have trying to edit or rename file of other user.

    you better state more briefly about your problem

    Comment

    • Icyhot
      New Member
      • Oct 2012
      • 6

      #3
      This program works (renames the desired .docx files in ALL directories) on one of the school computers, but it would not work on my computer (see complaint above). Here is my code:
      Code:
      #include <iostream>
      #include <fstream>
      #include <cstring>
      #include <string>
      #include <stdio.h>
      #include <vector>
      #include <windows.h>
      #include <Lmcons.h>
      
      using namespace std;
      
      class filemanip
      {
          private:
              string str,dirstr,path;
              bool keepgoing;
              vector <string> files, oldfiles;
              WIN32_FIND_DATA fd; //You have to declare a spot in the WIN32_FIND_DATA structure...
              HANDLE h;   //...and a handle for finding files.
              TCHAR name[UNLEN+1];    //array for the username
              DWORD size;   //value of the size of the array
              int c;  //this is for PressEnterToContinue()
              int result; //and this is for the rename()
          public:
              filemanip();
              filemanip(string);
              void PressEnterToContinue();
              void findfiles();
              void filerename();
      };  //end class
      
      filemanip::filemanip()
      {
          c = 0;
          result = 0;
          path = "";
      }   //end null constructor
      
      filemanip::filemanip(string directory)
      {
          str = directory;
          size = UNLEN+1;
          keepgoing = true;
      }   //end constructor
      
      void filemanip::PressEnterToContinue()
      {
          printf( "Press ENTER to continue... " );
          fflush( stdout );   //flush the standard output
          do      //This loop is for forcing the user to press ENTER to continue...
              c = getchar();  //...by continuing to ask for the user's input...
          while ((c != '\n') && (c != EOF));  //...while that input is NOT the ENTER key
      }
      void filemanip::findfiles()
      {
          if (GetUserName((TCHAR*)name,&size))
          {
              path = "C:/Users/"+(string)name+'/'+str+'/';
              dirstr = path+"/*+*.docx";
      
              //FindFirstFile() takes in a full directory, so you would need to examine ALL directories where the .docx files are held... :(
              //The algorithmic complexity from this alone might be high. Not to mention the fact that you would also have to search the files for a
              //'+' in their name
              h = FindFirstFile(dirstr.c_str(), &fd);
              if (h!=INVALID_HANDLE_VALUE)
              {
                  //compact conditional
                  do
                  {
                      oldfiles.push_back(path+fd.cFileName);   //store the file in the vector
                  }   //end do
                  while (FindNextFile(h, &fd));
                  FindClose(h);   //close the handle
              }   //end if
              else
              {
                  cout<<"FindFirstFile failed"<<GetLastError()<<endl;
              }   //end else
              for (int x=0; x<oldfiles.size(); x++)
              {
                  cout<<"oldfiles["<<x<<"] == "<<oldfiles[x]<<endl;
              }   //end for
          }   //end if
          else
          {
              cout << "An error occurred with the retrieval of the username."<<endl;
              bool keepgoing = false;
              PressEnterToContinue();
          }   //end else
      }   //end findfiles
      
      void filemanip::filerename()
      {
          if (!keepgoing)
          {
              //Do nothing; the program must end!
          }   //end if
          else
          {
              //At this point, the oldfiles vector has files in it. We need to fill the files vector with the same files, without their space-
              //substitute characters.
              for (int x = 0; x<oldfiles.size(); x++)
              {
                  files.push_back(oldfiles[x]);   //Dumping the contents of oldfiles into files
                  //to perform string manipulation on them
                  for (size_t pos = (files[x]).find_first_of('+'); pos != (files[x]).npos; pos = (files[x]).find('+', pos+1))
                  {
                      (files[x]).at(pos) = ' ';
                  }   //end inner for
                  cout<<"files["<<x<<"] == "<<files[x]<<endl;
                  result = rename((oldfiles[x]).c_str(), (files[x]).c_str());   //Now to rename the oldfiles to the files hahaha
                  if (result != 0)
                  {
                      perror("This error occurred: ");
                      PressEnterToContinue();
                  }   //end if
              }   //end for
          }   //end else
      }   //end filerename
      
      int main()
      {
          string ctor[] = {"Desktop", "Documents", "Downloads"};
          //ctor[2] might get its own conditional.
          filemanip * f = new filemanip [3];
          for (int y = 0; y < 3; y++)
          {
              f[y] = filemanip(ctor[y]);
              f[y].findfiles();
              f[y].filerename();
          }   //end for
          return 0;
      }   //end main
      The first elements in the heap result in both functions working without difficulty. However, the third one results in the program finding the files like it's supposed to, but it can't rename them because of permission denial. (This might be something dealing with my computer itself, but how to resolve the error is beyond my knowledge at this moment.)
      Last edited by Icyhot; Oct 11 '12, 04:43 AM. Reason: taking out dead code

      Comment

      • Icyhot
        New Member
        • Oct 2012
        • 6

        #4
        Now, in that same folder, it is throwing a "File exists" error.
        Last edited by Stewart Ross; Oct 12 '12, 07:04 PM. Reason: Removed sentence with word 'fu**' in it - against site rules, and did not add to what was posted

        Comment

        • Icyhot
          New Member
          • Oct 2012
          • 6

          #5
          Now, there is no error at all! I need beta testers to confirm this...

          Comment

          Working...