Cannot rename previously created folder - vb.net

I have following code that takes a local directory and copies it with a temporary name on a network drive.
Once there, it Waits a moment, deletes the original folder, and then renames the previously uploaded folder to the proper name.
All steps work fine, except for the last one. When it tries to rename the folder it throws an error saying that I do not have the permissions needed. Why? The folder is created by the program, it can delete the other folder just fine, why wouldnt it have the rights to rename it? Is it somehow "busy" or "used"? Any idea on how to fix this problem?
It happensonly from time to time, not every time.
copy("c:\somefolder\", "Y:\someotherfolder\temporary folder\")
System.Threading.Thread.Sleep(3000)
Directory.Delete("Y:\someotherfolder\original name\" , True)
System.Threading.Thread.Sleep(1000)
Directory.Move("Y:\someotherfolder\temporary folder\", "Y:\someotherfolder\original name\") ''this line causes trouble
thank you!

Related

vb.net set windows.old folder permission

Dim myDirectoryInfo As DirectoryInfo = New DirectoryInfo("C:\Windows.old")
Dim myDirectorySecurity As DirectorySecurity = myDirectoryInfo.GetAccessControl()
Dim User As String = "Everyone"
myDirectorySecurity.AddAccessRule(New FileSystemAccessRule(User, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow))
myDirectoryInfo.SetAccessControl(myDirectorySecurity)
I have a program that deletes junk files on the computer.
I want to delete the files in windows.old but the above code doesn't work.
Can you help me?
Windows.old is created and maintained for a short period of time in case you want to roll back to a previous install. It normally contains all the files from a previous install.
It should go away by itself, but if not, go to Settings, System, storage and click on the drive at the top of the page. Run down to Temporary files and it should be listed as Previous version of Windows. See if you can delete it there.
If you have deleted parts of it manually, no telling what it will do.
I would not recommend writing code to handle this as it isn't a folder that should come back after you deal with it once.

Can't delete .xlsx file - Next Step?

I'm getting an IOException (file in use by another process) error when trying to delete an .xlsx file. I moved the delete code to the front of Form_Load() just to make absolutely certain that nothing else I was doing was trying to open or read it.
Process Explorer can't find any references to the file to see what else might be trying to use it.
The file properties indicate that the user and system have full control.
I don't know what to look at next.
Private Sub PreEdit2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Computer.FileSystem.DeleteFile("\\HOSTNAME\Folder\Import Data\Other Import Files\" & "9710.xlsx")
I tried a local reference to the file just for good measure.
My.Computer.FileSystem.DeleteFile("C:\Folder\Import Data\Other Import Files\" & "9710.xlsx")
Really stumped here. Any suggestions sincerely appreciated.
EDIT:
Modified my program to delete all kinds of other files in the same directory. They delete with no problem.
Created an empty test program with one line that tries to delete the file
and it gets the same IOException.
So, my program isn't doing it. The error says something else is using the file, no monitoring program I have used shows the any process involved with the file, and File Explorer can delete it? Strange things are afoot at the Circle K.
EDIT 2:
I have moved the file and tried deleting it in lots of different directories. I can delete it everywhere else I try it. The only place I don't seem to be able to delete it (with my new single purpose test program) is in the directory it was originally copied to. It should be remembered that all of the other files in that directory I can use the program to delete with no problem.

Setting Directories and the If Len(Dir(... statement in VBA

I have a file exists under this path:
//path/folder1/folder2/datafile.gdp
It is an input to an external program being called from vba in this manner:
Sub RunProgram()
Dim wsh As Object
SetCurrentDirectory "\\path\"
ChDir "\\path\folder1\folder2\" 'Set Directory
Set wsh = VBA.CreateObject("WScript.Shell")
check = CurDir
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
But on the final line, including \path\folder1\folder2\ before the input file name appears to cause the external program to want to write some files into a duplicated directory that doesn't exist, causing an error. It used to work in this format before the .exe was updated by an external company. It now wants to write some files here, all prefixed with the name of my input file:
\\path\folder1\folder2\PATH\FOLDER1\FOLDER2\
Hoping to fix this, I changed the final line of the code to this, following some comments on a previous post here on SO:
Statusnum = wsh.Run(Command:="program.exe ""datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
Since the directory is set correctly prior to calling the .exe, I thought removing the path for the input file would solve the issue.
The program now launches, but doesn't appear to load the input file with it and no longer runs calculations automatically in the background as it should. Instead, it launches and the main .exe window pops up to the user as if it had just been launched for setting up a new project, calculations don't occur automatically.
To check which directory the VBA code was try to pull my datafile.gdp from, I created these loops directly before calling the .exe:
If Len(Dir("\\path\folder1\folder2\datafile.gdp")) = 0 Then
FileIsMissing1 = True 'I use Excel-VBA watches to break if true
End If
If Len(Dir("datafile.gdp")) = 0 Then
FileIsMissing2 = True
End If
Bizarrely, neither of these loops causes a break. The file only exists in
\\path\folder1\folder2\datafile.gdp
Not in the duplicated directory... so why are both of these statements satisfied? Does entering the directory make no difference even when the current directory has been set? The current directory seems to be impacting the line:
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)
But not these if loops, and I'm not sure why.

Macro broke after re-boot - Excel unable to locate external text files

As part of a larger macro Excel retrieves .dat files from a folder. After a crash and rebooting, the macro no longer works and seizes up upon trying to refresh.
There was also changes in security upon reboot where I had to enable all macros to be able to step though this current one.
I have double checked to make sure the path is correct and the files are still there.
Updated code and ended up crashing for unrelated memory issue. Fixed the memory problem and now won't run and is giving the same error code and message as before even with updated code. Current code which will run through the first loop but fails on the second.
If I am not mistaken, Dir just returns the filename found within that directory and not the full path to that file. What likely happened is your directory was initially set to F: so the macro used this as the default path when searching for the file. The best way to do this would be to store your directory in a constant string, and then append the two together. Something like this:
Const fPath as String = "F:YourPathHere\"
strFile = fPath & Dir(fPath & "*.dat")
Debug.Print strFile ' Just to make sure it is setting properly.

Can't Delete INI File After Calling GetPrivateProfileString

I am having trouble deleting files after calling the GetPrivateProfileString command. I have the following code:
'Read the INI File
sb = New StringBuilder(500)
Select Case FileType
Case "Scanner File"
res = GetPrivateProfileString("ScannerSetings", "ScannerType", "", sb, sb.Capacity, Filename)
Case "Scale File"
res = GetPrivateProfileString("ScaleSetings", "ScaleType", "", sb, sb.Capacity, Filename)
End Select
'If the result is a value store it, otherwise move it to unprocessed
If res <> 0 Then InputArray.Add(sb.ToString)
File.Delete(Filename)
After reading the details from the INI file, as soon as I try to delete the file, I am getting the following error: The process cannot access the file 'R:\Drop\011_11_Scanner' because it is being used by another process.
I cannot even delete these files manually until I exit my application.
Any help would be appreciated.
Thanks
I cannot even delete these files manually until I exit my application.
This clearly shows that the file that Filename points to is locked. As long as it's locked, you won't be able to delete it.
Check your code for any file handles you have opened (eg for writing purposes) and did not close.
If you don't close a file after you've opened it, the file is not released and remains in locked state… which practically means it can not be deleted until (a) you close that file-handle, or (b) you close your program, since that's what's holding the file-handle after opening.
EDIT
The next thing that comes to mind is that VB.NET may need special user access rights to remove the INI on recent Windows versions. You can quickly cross-check that by simply executing your application with elevated user rights (eg via right-click menu; run as admin). I can't imagine that that's actually the problem — but it's worth a shot. Should it indeed be the problem, check (and modify) the permissions on the folder your application and/or INI resides in.