Deleting a folder using VBA and it still appears in File Explorer until the next iteration - vba

VBA - Access
I'm using fso.DeleteFolder to delete a folder on a server that an earlier function in the same Access DB created. The fso.DeleteFolder code runs without error and running fso.FolderExists after shows the folder no longer exists (prior to the delete, fso.FolderExists stated that the folder did exist).
However the folder is still listed in File Explorer on the server. Refreshing does no good. When double-clicking the folder on the server, I get an error stating that the folder is not accessible and access denied (prior to the delete, the folder was normal).
The folder may or may not have subfolders and/or files - but I do not think that is important because if I run the create / delete processes over again using a different folder name, the issue repeats - EXCEPT the prior folder is now no longer listed in File Explorer (truly deleted now as God intended), replaced with the new folder that was supposed to be deleted with the same double-click error and so on.
I kill my fso object at the end of the function, I believe the same issue also happens with KILL instead of fso.
Any help/thoughts would be appreciated.

Just encountered that problem!
For some reason deleting a directory with VBA leaves that lingering "lock" on the deleted file UNTIL it performs another directory type action then it flushes the "lock" If you were to delete three directories in one script you would likely find that the only "failed" delete action would be on the last directory deleted.
Performing a "directory read" type action within the script after the RmDir action works for me. Try something like this...
If Dir("yourpath") = "" Then MkDir "yourpath"

Related

How can I find if a file exists in the Microsoft Teams (SharePoint) environment using VBA?

I have created a file structure within Microsoft Teams to support a sales forecasting tool. VBA routines access and modify these files as appropriate. I would like to check if a file exists in a target directory before attempting to open it and then handle any missing files within VBA. I cannot find a way of doing something analogous to the Dir instruction, e.g.
If Len(Dir("c:\Filename.xlsx")) = 0 Then Msgbox "This file does NOT exist."
Substituting the URL returns error 52 "Bad file name".
Opening and saving files works fine, but if the file is not present, I only get a system error which I cannot handle within my environment.
If you sync the files locally, you can go on with Dir and local path instead of url.

System.IO.Directory.Delete v Git Repository - Unauthorized access

I'm writing a piece of software that maintains files in a Git repository.
One of the executables I've written has one simple purpose: delete everything to do with the software. It is given to the user in a separate, completely unrelated Windows folder.
One of the folders it needs to delete contains a Git repository, i.e. it contains a hidden .git folder
Now here's the problem I'm having. When I'm on the PC itself looking at the file system I can find that folder, click on it, delete it .... and it's gone. Not a problem.
So I try to do the same thing in my assembly by calling System.IO.Directory.Delete on that folder.
Here is what I am finding:
Everything is removed from the folder, but the folder itself is still there
When I try to THEN manually delete the folder from Windows Explorer it gives me some Unauthorised Access message. I cannot delete it.
The only way to actually get rid of it is to then try RENAME the folder in Windows Explorer, at which point I once again get an Unauthorized Access message but wouldn't you know it.... the folder disappears.
What gives!?!? Why does a System.IO.Directory.Delete NOT do exactly the same thing as me going in through Windows Explorer and deleting the folder manually?
I have tried a number of things. For example I tried getting all the files in the directory and setting all their Attributes to Normal, and through all the folders and setting their Attributes to Directory And Not Hidden. Still no joy. I even tried using Process.Start to run a RMDIR folder /S /Q - which runs perfectly from a .bat file - but the same thing happens.
I do have TortoiseGit and all its dependencies installed in case that has anything to do with anything.... and the folder does have the green Tick on it to indicate that its content is all up to date.
So my question is:
Is there any way I can successfully delete a folder containing a Git repository though VB.NET?
I finally got to the bottom of this mysterious mystery.
The culprit is TortoiseGit
When I killed the TGitCache.exe process just before deleting the folder, everything worked perfectly.
Go figure.

How to prevent file from access if any file failed to write to a required folder

I have 3 xml file to be written to a folder for client. while writing the 2 files got written perfectly but 3rd file failed. what are the ways by which I can prevent the client to open any file or all the files got deleted or locked if anything failed?
If your application has delete privileges on the system, keep a record of the filenames to the files you're writing. If a file fails for whatever reason, go through the list of file names and delete the files from the directory. A simple string list with a for loop should do it.

SSIS File System Task Deleting Directory Leaves Directory and Removes all Permissions

I have a rather bizarre issue with an SSIS package I have. After processing some files within a Directory I am removing the Directory so my For Each Loop doesn't process the same files again.
It fails to Delete the Directory but yet leaves it in the folder and then tells me I do not have access to this folder, I cannot take ownership of it or even move it to another directory.
I have even tried a VB.NET Script tasks to try and remove it and I get the same result.
A reboot of the server makes the folder disappear - has anyone experienced this before.
Regards,
JML
Sounds like the directory is "in use" still but with the deletion pending. When you reboot, the lock on the directory is cleared and the deletion completes. The discussion at
try ing to move a file "The process cannot access the file because it is being used by another process."
may be helpful.

FTP Concurrency issues using Ipswitch WS-FTP Pro

I think we have a problem in our FTP scripts that pull files from a remote server to a local machine. I couldn't find an answer in their knowledge base, nor scripting documentation.
We are doing an MGET *.* and then a MDELETE *.* immediately after it. I think what is happening is that, while we are copying files from the server, additional files are copied into the same directory and then the delete command deletes everything from the server. So we end up deleting file we never copied down.
Is there a straight-forward way to delete only the files that were copied, or is it going to be some sort of hack job where we generate a dynamic delete script based on what we actually copied down?
Answers that are product specific would be much appreciated!
Here were the options that I came up with and what I ended up doing.
Rename the extension on the server, copy the renamed files, and then delete the renamed files. This could not work because there is no FTP rename command that works with wildcards (Windows rename command will by the way).
Move the files to a subdirectory on the server, copy the files from that location, and then delete from the remote location. This could not work because there is no FTP command to move the files on the remote server.
Copy the files down in one script and SHELL a batch file on the local side that dynamically builds a script to connect to the server and delete the files that were copied down. This is the solution I ended up using to solve this problem.