I wrote a batch file which creates a file on the C: drive root folder. However if logging in as a normal user, the file won't be created and I will get an "Access Denied" error.
How can I avoid this problem?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
The user does not have permission to write to the root of the drive. You need to create the file in a location that the user has write access to. For example, their Application Data folder. The root of the C drive is typically locked down to prevent you from writing into it, as it is not best practice to do so.
If you absolutely must write into the root of the drive, edit the NTFS permissions for the drive to permit the user to write to it. But be aware that this is not a good way to go about things.
As stated, using %TEMP% or %TMP% is recommended if the file does not need to be kept tracked of/saved. If not, I would just use %USERPROFILE% (c:\Documents and Settings\normal) where 'normal' is your username... as that is the simplest/logical place to put it if you do not have the correct privileges to access the root directory. %USERPROFILE%\desktop if you want it to be placed on the desktop for easy access.
Instead of using a "hardcoded" path, you could use one that usually can be written to such as
%UserProfile%. There you can create a file or preferably a folder/directory to be used by your batch file.
Alternatively, you can have the user to specify the absolute filename to use.
If you really need admin rights, you can use the
runas command (e.g. with savecred) from within your script.
I fully agree to what has been said here already. In addition I think
%UserProfile% is sometimes not the preferred place to put temporary files. Instead you could use
%TEMP% for such things. For data which needs to be available permanently to the user you can use
%AppData%\someFolderName. The main advantage of
%AppData% is that is also roams to other machines if roaming profiles are enabled in corporate environments.
Remember that also
%TEMP% is user-specific. So each user logging on to the system will have its own temporary file which is not shared. If you need to share files among multiple users you should consider placing the file somewhere in
%PUBLIC% which is available to all users on the machine.