Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, I cant change the name of a file that the owner is administrator.
Let me explain, I made a program that changes the name of the files that in the desktop, My program work well but the program cant change the file name and his data of Administrator file.

How can i do this ?

https://prnt.sc/pruvek

This is the link for what I explained.

C++
srand(time(0));
	string dirPath = "C:\\Users\\" + username() + "\\Desktop";
	auto path = fs::path(dirPath);
	auto dir = fs::recursive_directory_iterator(path);
	filesystem::path currPath = path;
	int fileName = rand() % 1920;

	for (auto& block : dir)
	{

		auto innerPaths = vector<fs::path>();
		innerPaths.push_back(currPath);
		auto currentDepthSize = dir.depth();

		if (currentDepthSize != dir.depth())
		{
			currentDepthSize = dir.depth();
			currPath = innerPaths[currentDepthSize];
		}
		if (block.is_directory())
		{
			currentDepthSize = dir.depth();
			currPath = dir->path();
			innerPaths.push_back(currPath);
		}
		else
		{
			string filedata = (currPath.string() + "\\" + to_string(fileName));
			fs::rename(block.path(), filedata);
			fileName++;
			fstream file_data(filedata);
			file_data << randomString();
			file_data.close();
		}
	}

This code is changing the files names and data in the desktop.

How can i do that my program will also change files that have Owner option as Administrator ?

What I have tried:

I have not tried anything yet.
Posted
Updated 3-Nov-19 6:44am
v3
Comments
phil.o 3-Nov-19 9:05am    
No offense, but adding a random string to any file, even the ones which you do not own, seems rather malicious.
[no name] 3-Nov-19 9:06am    
Why ?
Richard MacCutchan 3-Nov-19 9:35am    
That file is owned by "Administrators". So if you are not that user, and you do not have Administrator rights, then you cannot (obviously) change it.
[no name] 3-Nov-19 9:36am    
Please read my comment before
Richard MacCutchan 3-Nov-19 9:40am    
Which comment?

If the account running this code is not the account that owns the Users folder you're trying to change files in, your code cannot touch those files.

Security is there for a reason and it's going to prevent you from doing what you want to do.


Why? Think about it. If your code renames file to anything you want, you can seriously screw up what people are working on and even mess with files the system needs to run. This is considered a security risk.
 
Share this answer
 
Comments
[no name] 3-Nov-19 9:13am    
My program is only changing the files only in the desktop, I'm not touching any security files.

Do you think that fs::perms will work in this case ?
Dave Kreskowiak 3-Nov-19 9:17am    
There's no such thing as "security files".

There is not a single "Desktop". Every user has their own Desktop folder. There is also a Public Desktop folder that contains files that is visible on every user that logs into the system.

If your account is not the account which owns or has permissions to the Users folder your trying to modify files under, your code can NOT touch those files.

Would you want some other users on the system to mess with your files you have on the Desktop? Or in your Documents folder? No? Then why should the system let you do it to someone else?

[no name] 3-Nov-19 9:19am    
But im not messing any files on any computer, i'm just asking, i want to know how can i change file's perms and get owner perms.

Without this my program is not working like i except , please help me with this.
RickZeeland 3-Nov-19 9:23am    
He means you have to login in Windows with administrator rights, or use "Run as administrator".
Dave Kreskowiak 3-Nov-19 9:24am    
If you're asking how to change the files under the Public folder, you can't, unless the account running your code is an Admin, and on versions of Windows with UAC and UAC turned on, you run the code with admin permissions. Right-click your .exe file and pick "Run as administrator".
Try the simple example here and see if that works: rename, _wrename | Microsoft Docs[^]
When running from Visual Studio remember to start Visual Studio with "Run as administrator".

If this does not work, read: How User Account Control (UAC) Affects Your Application | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
[no name] 3-Nov-19 13:06pm    
What ? did you even read my question ?

Bad answer!
RickZeeland 3-Nov-19 13:10pm    
Yes, I read your question and all comments and my conclusion is that there is too much confusion and that there are too much unknown factors. So it would be best to take a step back and try something simple first.
[no name] 3-Nov-19 13:12pm    
The 2 links that you gave me, i know them very well.

I built a program that changes the name and the data of the whole files in pc, and i already set my visual studio to "run as administrator", so you please take step back and read my question again, thank you!
RickZeeland 3-Nov-19 13:14pm    
Ok then, I won't bother you again :)
[no name] 3-Nov-19 13:16pm    
I asked for help, can you help me please with my question ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900