Windows - Writing to file with `.txt` or `.zip` says Access denied - windows

I'm trying to write data to a text file in Windows programatically (either through cmd directly or from a scripting language like Lua), but if I try to open any file with the following conditions, an error occurs, saying permission denied.
the file name ends with .txt or .zip
I try to create the file inside some subfolders of my user folder, e.g. %USERPROFILE%\Documents\**\file.txt or %USERPROFILE%\Pictures\**\file.zip.
this is done programatically (works fine using any GUI).
This happens only on Windows, and even if I run cmd with admin rights.
C:\Users\Username\Pictures>echo aaaa > a.txt
Access denied.
C:\Users\Username\Documents>echo aaaa > a.txt
Access denied.
EDIT: I noticed this was specific to subfolders of %USERPROFILE%, and not related to Lua, which the original text of this question was about. So I rewrote the question to reflect the problem properly.
I still can't find any pattern of which folders are affected by this, currently only Pictures and Documents.

Related

Stata can't open file saved in local cloud storage folder on Mac

I am using Stata on a Mac. I updated the OS to Monterey today. If I try to open a .dta file---that is saved on my local computer in a folder that syncs to OneDrive---from code in the Do File Editor/Command prompt (where I refer to the file using its full file path), it doesn't open. The error is r(601) - File ... cannot be found.
But if I click on it in Finder, it opens in Stata just fine. After I have done this, I can then open this file in Stata by running the exact same Stata code that didn't work before. Somehow it seems like Stata can't see the file/folder initially, but then it can. I am not sure if this has to do with the OneDrive file/folder permissions or something else.
If, in Stata, I change the directory to the correct directory using cd and then load the file (without referring to the path), it works. However, I would like to be able to refer to the file using the entire file path and not have to change the directory each time.
Interestingly, if I change the directory to the correct directory using Change working directory in the File drop down and then try to load the file using the file path, it also works. But if I do not do the drop down and write out the cd using code, and try accessing the file using the path it doesn't work!
Somehow Stata has to initially "see"/"access" the correct folder (in a very particular way) before being able to access it with the file path. Very strange. (Just to be clear, if I do not change the directory to the correct directory and then try to load the file using its file path, it doesn't work.)
I also tried creating a file using texdoc init... (in a folder that already exists, but that has not been used by Stata before) and get this error:
could not create directory .....
mkdir(): 693 could not create directory
texdoc_mkdir(): - function returned error
texdoc_init(): - function returned error
<istmt>: - function returned error
I do not get this error if I instead use another folder in the same directory that I have previously used.
I think that the error I am encountering is the same or similar to this one (with no acceptable answer): https://answers.microsoft.com/en-us/msoffice/forum/all/onedrive-folder-doesnt-exist/e6b97d47-3f6a-4863-bf68-d3a02832f2bb

What file permissions make a file unreadable by owner in Windows?

I am working with the automated test suite of a software project. I have an automated test case that should verify that the code works correctly when a specific file is found, but the program does not have proper permission to read the contents of that file. This test case is currently working on Linux and macOS, but I am trying to get it to work on Windows as well.
The file is created by the test process, modified to have the read permission removed, and then the code tries to read the file, and the test verifies that the code fails in the expected way. After the test has run, the permissions are restored (if necessary), and the file is removed.
On Linux and macOS the read permission is removed by chmod 000 unreadable.txt, which makes the file unreadable by everyone (except root). I am trying to find a similar solution on Windows, where I can make this test file unreadable by the current user using command line tools, so the tested program can fail.
From my understanding, a command like icacls unreadable.txt /deny 'Everyone:(RC)' should have done the trick. icacls confirm the deny rule:
Everyone:(DENY)(Rc,S)
but I have nevertheless full access to read the file.
I've tried various incantations of calls to icacls and cacls, with different combinations of trying to explicitly denying for my specific user account, etc. All to no avail.
I'm starting to wondering, is is impossible in Windows to disallow the owner of a file from reading it?
Or, if it is possible, what is the command line I need to make the file unreadable by the owner?

Windows can access file, DOS file copy gets Access Denied

I'm trying to set up a set of folders on a Windows NT server 2008 (yes, I know, old) where a user doesn't have access to see the list of files within the folder, but can read a file if they know the full file path.
So I've set up the following AD permissions:
Permissions on the containing folder ("This folder only"):
Traverse folder / execute file
Read attributes
Read extended attributes
Read permissions
Permissions on the files ("Files only"):
Traverse folder / execute file
List folder / read data
Read attributes
Read extended attributes
Read permissions
... and from windows, everything looks great! I can't see inside the folder, but if I know the full path to a file within, I can type it into an address bar and open the file.
But when I run in Command Prompt:
COPY "FullPathToSameFileAsBefore.txt" "C:\someLocalSpot.txt"
... I get:
Access is denied.
0 file(s) copied.
Any ideas? Is there some special access Command Prompt needs to perform the copy that windows doesn't in order to read the file? Any alternatives that will work instead? I can set any of the permissions that are needed, with the caveat that the user cannot see the list of files within the directory.
EDIT with additional info:
So I tried to perform a copy with VBScript using a FileSystemObject. Same error. But using VBScript to read the file with an ADODB binary stream does work.
So it seems to boil down to "You can read this file, but you can't perform a copy." Which seems weird, since if you can read the file, you can certainly copy it (read it, then write the contents someplace else.)

How to Make Folder Only Accesible when User Run the Script

My question is how I can make one folder accessible when script running.
In this case lets call there is bob who copying his folder by using script to specific location and there is jeff who also sharing the same group as bob also he copying his file to there with using script.
The problem is that when I set file group they need write and execute permission and when I gave to them they are able to see each other file content if they know full path of the file.
To stop that I am thinking to completely deleting all permission on folder and only giving the permission when script running and doing copying process.
But problem is that when those users run the script and script try to chmod the file permission they are not going to be able to because they don't have enough permission to do it. Also if I add them on sudoers, they are going to be able to chmod and change anything as they want to change.
So I am so confused about how I can make the script change permission of folder and when copying completed turn back to previous permission
You should add a sudoers entry to allow ALL or the selected group to run a given script that does the copy to a restricted directory, with NOPASSWD to avoid the password prompt.
Then the users invoke
$ sudo /path/to/copy-to-restricted-dir files*
but users don't have access to restricted directory nor to chmod.

DOS ftp listing to local file

I'm trying to find a way to see if a file exists on an ftp site via DOS. I tried a get command on the file hoping that if it didn't exist it wouldn't download it to my local directory. However it seams that it still does, but it's an empty file. This doesn't work for me however because the file I'm looking for is just a empty trigger file so I can't tell the difference.
I would like to dump a listing ls of the ftp directory to a text file on my local drive and so I try
ls > listing.txt.
It creates the listing.txt file locally but it's always empty even though there are files on the ftp site.
What are my options with this?
I have used dir > listing.txt and ls > listing.txt and every time listing.txt is empty even though there are files in the directories I'm running those commands on.
Sorry if I didn't make this clear, but I'm trying to get the listing for an automated process and not simply for my visual when manually doing this.
Unless you're on FreeDOS, you're probably not using DOS. Perhaps you're using ftp.exe in the windows console? If that's the case, don't use a normal file redirect. Instead check here the syntax for ls in the standard Windows ftp client:
ls [RemoteDirectory] [LocalFile]
So you can do a ls . listing.txt to get a list of files in the current remote directory. The listing.txt file will appear in your user directory, e.g. c:\Users\user.

Resources