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++.
4 5260
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
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: - #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.)
Now, in that same folder, it is throwing a "File exists" error.
Now, there is no error at all! I need beta testers to confirm this...
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |