473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Renaming: Permission denied

6
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++.
Oct 9 '12 #1
4 5260
johny10151981
1,059 1GB
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
Oct 10 '12 #2
Icyhot
6
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <string>
  5. #include <stdio.h>
  6. #include <vector>
  7. #include <windows.h>
  8. #include <Lmcons.h>
  9.  
  10. using namespace std;
  11.  
  12. class filemanip
  13. {
  14.     private:
  15.         string str,dirstr,path;
  16.         bool keepgoing;
  17.         vector <string> files, oldfiles;
  18.         WIN32_FIND_DATA fd; //You have to declare a spot in the WIN32_FIND_DATA structure...
  19.         HANDLE h;   //...and a handle for finding files.
  20.         TCHAR name[UNLEN+1];    //array for the username
  21.         DWORD size;   //value of the size of the array
  22.         int c;  //this is for PressEnterToContinue()
  23.         int result; //and this is for the rename()
  24.     public:
  25.         filemanip();
  26.         filemanip(string);
  27.         void PressEnterToContinue();
  28.         void findfiles();
  29.         void filerename();
  30. };  //end class
  31.  
  32. filemanip::filemanip()
  33. {
  34.     c = 0;
  35.     result = 0;
  36.     path = "";
  37. }   //end null constructor
  38.  
  39. filemanip::filemanip(string directory)
  40. {
  41.     str = directory;
  42.     size = UNLEN+1;
  43.     keepgoing = true;
  44. }   //end constructor
  45.  
  46. void filemanip::PressEnterToContinue()
  47. {
  48.     printf( "Press ENTER to continue... " );
  49.     fflush( stdout );   //flush the standard output
  50.     do      //This loop is for forcing the user to press ENTER to continue...
  51.         c = getchar();  //...by continuing to ask for the user's input...
  52.     while ((c != '\n') && (c != EOF));  //...while that input is NOT the ENTER key
  53. }
  54. void filemanip::findfiles()
  55. {
  56.     if (GetUserName((TCHAR*)name,&size))
  57.     {
  58.         path = "C:/Users/"+(string)name+'/'+str+'/';
  59.         dirstr = path+"/*+*.docx";
  60.  
  61.         //FindFirstFile() takes in a full directory, so you would need to examine ALL directories where the .docx files are held... :(
  62.         //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
  63.         //'+' in their name
  64.         h = FindFirstFile(dirstr.c_str(), &fd);
  65.         if (h!=INVALID_HANDLE_VALUE)
  66.         {
  67.             //compact conditional
  68.             do
  69.             {
  70.                 oldfiles.push_back(path+fd.cFileName);   //store the file in the vector
  71.             }   //end do
  72.             while (FindNextFile(h, &fd));
  73.             FindClose(h);   //close the handle
  74.         }   //end if
  75.         else
  76.         {
  77.             cout<<"FindFirstFile failed"<<GetLastError()<<endl;
  78.         }   //end else
  79.         for (int x=0; x<oldfiles.size(); x++)
  80.         {
  81.             cout<<"oldfiles["<<x<<"] == "<<oldfiles[x]<<endl;
  82.         }   //end for
  83.     }   //end if
  84.     else
  85.     {
  86.         cout << "An error occurred with the retrieval of the username."<<endl;
  87.         bool keepgoing = false;
  88.         PressEnterToContinue();
  89.     }   //end else
  90. }   //end findfiles
  91.  
  92. void filemanip::filerename()
  93. {
  94.     if (!keepgoing)
  95.     {
  96.         //Do nothing; the program must end!
  97.     }   //end if
  98.     else
  99.     {
  100.         //At this point, the oldfiles vector has files in it. We need to fill the files vector with the same files, without their space-
  101.         //substitute characters.
  102.         for (int x = 0; x<oldfiles.size(); x++)
  103.         {
  104.             files.push_back(oldfiles[x]);   //Dumping the contents of oldfiles into files
  105.             //to perform string manipulation on them
  106.             for (size_t pos = (files[x]).find_first_of('+'); pos != (files[x]).npos; pos = (files[x]).find('+', pos+1))
  107.             {
  108.                 (files[x]).at(pos) = ' ';
  109.             }   //end inner for
  110.             cout<<"files["<<x<<"] == "<<files[x]<<endl;
  111.             result = rename((oldfiles[x]).c_str(), (files[x]).c_str());   //Now to rename the oldfiles to the files hahaha
  112.             if (result != 0)
  113.             {
  114.                 perror("This error occurred: ");
  115.                 PressEnterToContinue();
  116.             }   //end if
  117.         }   //end for
  118.     }   //end else
  119. }   //end filerename
  120.  
  121. int main()
  122. {
  123.     string ctor[] = {"Desktop", "Documents", "Downloads"};
  124.     //ctor[2] might get its own conditional.
  125.     filemanip * f = new filemanip [3];
  126.     for (int y = 0; y < 3; y++)
  127.     {
  128.         f[y] = filemanip(ctor[y]);
  129.         f[y].findfiles();
  130.         f[y].filerename();
  131.     }   //end for
  132.     return 0;
  133. }   //end main
  134.  
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.)
Oct 11 '12 #3
Icyhot
6
Now, in that same folder, it is throwing a "File exists" error.
Oct 12 '12 #4
Icyhot
6
Now, there is no error at all! I need beta testers to confirm this...
Oct 13 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Klunk | last post by:
Hi, last week I moved the folder of my Intranet application to another disk of the same server. After this change all the ASP page return this error: Microsoft VBScript runtime error...
6
by: Jon Montana | last post by:
CDONTS was working well until I installed Exchange 2000. I thought it might be a relay problem, but I allowed the server IP address to relay. Thanks for anything you can offer. here's the...
1
by: Mark E. Hamilton | last post by:
Sorry, I probably should have re-stated the problem: We're using Python 2.3.5 on AIX 5.2, and get the follow error messages from some of our code. I haven't yet tracked down exactly where it's...
2
by: Taishi | last post by:
New user of SQL Everything is on the same machine My error is close to the bottom After reading it today, I can see there is a problem with 2 dbases 'PUBS' and 'Master' There are also some...
4
by: stephen | last post by:
Hi, I am getting an error while trying to create an excel file. "Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the...
1
by: doctorhardik | last post by:
other interesting thing i observe during my work which i describe below: i am using dotproject2.0.4 on fc3. it is working fine. but i want to generate pdf file report during this time i face...
0
by: debug03 | last post by:
I am executing a DTS package on a Windows 2000 sp4 server running SQL Server 2000 and IBM DB2 V7 client. The DTS package source data(SQL Server) is selected from SQL server table and inserts data to...
0
by: private.anders | last post by:
Hi David! Really need assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have...
0
by: private.anders | last post by:
Really need your assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have...
4
by: xzzy | last post by:
I have a v1.1 web app that works on my local computer. However, running it at the host computer, the following breaks: when a viewer selects a different country, the State dropdown should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.