Permissions error on FSO DeleteFile/MoveFile - file-io

I'm trying to delete/move a file using classic asp fso but I'm getting this error:
Microsoft VBScript runtime error '800a0046'
Permission denied
The file is located in c:\files\test\file.txt and ultimately I want to move it to c:\archive\file.txt
I started out with Move but I got the error. Then I split it up into Copy and Delete. The copying went fine but still the same error on Delete.
I ruled out the Delete method because I was able to use Delete on the file in the 'archive' folder using this code:
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.GetFile("C:\archive\file.txt")
f.Delete
set f=nothing
set fs=nothing
I also double-checked the permissions on both folders and they're exactly the same for IIS_IUSRS: Read&Execute, List folder contents, Read, Write are checked. I tried giving Modify rights as well but that made no difference.
Finally, I checked the file's attributes and it came back as 32 (Archive). I also tried to force the delete:
fso.DeleteFile FileName, true

IIS_USRS folder permissions had to specifically include 'Delete' from the 'Advanced' dialog in Windows.

Related

Word VBA Documents.Open deleting files in folder when falsely passing that folder's path instead of file path

I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show

Check if file exists in OneDrive via VBA

I'm trying to check if an Excel file exists in OneDrive using Dir(Filename) (without opening it):
strFilePath = "https://d.docs.live.net/ce59asdf4hj34h5k/Folder 1/Folder 2/File.xlsm"
If Dir(strFilePath) <> "" Then
'do something
End If
but Dir(Filename) give me an error: Run-time error '52': Bad filename or number
I tried to do a workaround and check if the URL itself is valid (using the code sample from here) unfortunately the URL is always valid regardless if the file exists or not (if the file in question does not exist One Drive seems to return a default file, at least that's what it does if you test it in a browser, see screenshot below)
Any other ideas on how to solve the problem would be appreciated (I'm trying to do this without actually opening the file since this will create other problems I'm trying to avoid).
Note: I have OneDrive (Sync) installed, but using a local path unfortunately is out of solution's scope. The Soultion has to work with files that are not synced locally.

Can't open xlsm file from SSIS package job

I have a number of SSIS jobs that open some Excel files and manipulate them. The task within the job that opens the Excel file is basically the same in each job, it just points to a different Excel file. Each of these jobs work fine except for one. But even this one works when ran manually from the package and not in the job.
The applicable code is:
Dim reportLocation As String
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oWB As Microsoft.Office.Interop.Excel.Workbook
reportLocation = "\\testlocation\share\test.xlsm"
'Open Excel instance
oXL = New Microsoft.Office.Interop.Excel.Application
oXL.Visible = False
'Open the Excel file to edit
oWB = oXL.Workbooks.Open(reportLocation) 'Error here
The error received is as below:
Microsoft Excel cannot access the file '\\testlocation\share\test.xlsm'.
There are several possible
reasons:
• The file name or path does not exist. • The file is being used by
another program. • The workbook you are trying to save has the same
name as a currently open workbook. at
Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object
UpdateLinks, Object ReadOnly, Object Format, Object Password, Object
WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin,
Object Delimiter, Object Editable, Object Notify, Object Converter,
Object AddToMru, Object Local, Object CorruptLoad) at
ScriptTask_ffa2c543b1224c3987d5764694df1079.ScriptMain.Main()
This was the latest package to be created, and was a copy/paste job, just changing the locations it looks for the files. I have checked all permissions and there is no issue there. I've even given access to "Everyone" to see if that was an issue, but the same error occurred. The file definitely exists in the location and it is not open by anyone. What possible reason is there that this specific file can't be opened when run in a SQL job but can be opened when run manually in the package? I have also tried changing the job to run the package as a user with full admin access via a proxy. But still no joy.
This does not look like a valid path to me:
\\testlocation\test.xlsm
You at least need a share name after the host name:
\\testlocation\Share\test.xlsm
How do the paths look for your working files? I'm guessing they have a share.
Having revisited some answers on here for similar questions I was able to find the solution. This can be seen at Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'
I created the directory C:\Windows\SysWOW64\config\systemprofile\Desktop on the server and it miraculously now works. I had looked at this yesterday and thought the directory already existed so didn't implement this solution. But having a second look at it today shows the directory didn't exist and creating it fixed it.
Now the question is, why does this work?! But that is possibly another question.

Permission Denied Error During Update of a Local MSOffice Add-In from Network

I am attempting to write a procedure in PowerPoint 2003 that will allow automatic updating of an installed add-in. The general process is as follows:
Uninstall the add-in
For Each objAddIn In Application.AddIns
If UCase(objAddIn.Name) = UCase(AddInName) Then
With objAddIn
.Registered = msoFalse
.AutoLoad = msoFalse
.Loaded = msoFalse
End With
End If
Next
Delete the file from the local Add-Ins directory
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(FileName) Then
Set objFSO = Nothing
Kill FileName
End If
Copy over the file from the network location
Install the updated add-In
Upon reaching step 2, any attempt at deleting the file post-uninstall using either the FileSystemObject or a straight Kill inevitably generates Run-time error '70': Permission denied. If I hit Debug and then play, it runs through as if there was never a problem.
Side note: I realize I can use FSO to overwrite the local file, but that gives me the same run-time error.
I'm guessing the problem has to do with some aspect of the file being in use, but I can't figure out how to "release" the old add-in so that the underlying file can be deleted.
Does anyone have insight that can help?
You need to remove it from the Addins Collection before it can get physically deleted. Put this, right after your End With:
Application.AddIns.Remove objAddIn.Name
The KB Article that refers to a VBA function you can use to CHECK for a locked file is here.
http://support.microsoft.com/kb/209189
It contains a simple function you can add to your VBA to check that a file is not locked and uses the same code sample that Otaku refers to in his answer.
What I would do is...
Replace the Kill Filename line in your code
If FileLocked(Filename) = True Then
SetAttr Filename, vbNormal
Kill Filename
End If
*Where FileLocked is a custom function referred to in KB209189
**If this doesn't work, reply back, we could also look at replacing the Kill statement above with a lower level Win32 function that does the same thing (but perhaps more throughly). :D

VBA How to get path to The Current Users Application data folder?

In general,
Using VBA, how do I determine where the Current users Application Data folder is?
The FileSystemObjects special folders only knows about 3 folders
WindowsFolder
SystemFolder
TemporaryFolder
Specifically, I need a Word Macro to copy a file to the a folder under the Application Data folder.
e.g. In VB.Net I can use My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData to do this
You can use Environ("AppData") to get this path. Environ will pull any system variable, which can be found by using the set command at the DOS prompt.
Using advapi32.dll, you can get the USERPROFILE via
Environ("USERPROFILE")
Connect this with the "Application Data" directory (which has a standard, specific name) to get what you want
CStr(Environ("USERPROFILE") & "\Application Data")
For more information, check out MSDN