vb.net help, how to skip deleting a file without permission - vb.net

On VB.net i was making a file cleaner, one that deletes stuff like temp etc, however with folders like prefetch and temp where some are in use at the time is there a way to make the program skip over the undeletable files and clear everything else, thanks

You should use the following code on Button click event (administrator required):
Shell("CLEANMGR", "/d <drive_letter> /sagerun:64")
The following items may be deleted on execution of the above code:
Temporary Internet Files
Temporary Setup Files
Downloaded Program Files
Old CHKDSK Files
Recycle Bin
Temporary Files
Windows DUMP and Error logs
Source of using CleanMGR: cleanmgr command line switches...

You need to wrap your delete routine in a try catch statement:
Try
System.IO.File.Delete([FILENAME])
Catch ex as Exception
'Log out to console
Console.WriteLine(ex.Message)
End Try

If your goal is to write this utility (so you aren't interested in re-using an existing utility that does the same thing), there isn't a good way to check to see if you can delete a file before you try. Some things you can't test for, and even when you can, there's no guarantee that the file will be in the same state when you try to delete it (even a very short time later), so your operation might fail anyway.
Thus, the only way to go is to put a Try/Catch around every file operation (or accept the possibility that a failure might lead to a crash).
The resulting pseudocode is something like:
For Each (item in top-level list of places to search)
Try
'If directory, try to enter it. If successful, recurse.
'If file, try to delete it.
Catch
'Can log or just skip, this is a valid case to eat an exception
'At a minimum, you might see a System.IO.IOException or a couple of different security-related exceptions
End Try
Next

Related

Lua, Altered Program Flow When Rquired File isn't there

I want to test to see if a require'd file is there or not.
At the present, when I execute this command in Lua:
require "File_That_May_or_May_Not_Be_There.inc"
All is well if he's there. If not, my script is dead on the spot.
Is there a way for me to recover from this ?
I looked HERE on the Lua.Org site and HERE on StackOverflow and haven't seen this answered.
Is there a way to do something like this ?...
if (this_exists("That_File")) then
require "That_File"
else
print "Your file does not exist"
end
I'm trying to let the user have a little better idea of what went wrong, and why.
Use pcall.
local ok, mod = pcall(require, "That_File")
If you don't need to use require, which looks for modules in several locations, you can use
local f,e=loadfile(filename)
if f==nil then
print(e)
else
f()
end

Set timeout for system call in Rails

From Rails, I make a system call to wget:
system("wget", ...)
I want to set a timeout for this call, so that if it takes too long (which is likely to mean too many files downloaded, or a large file downloaded), I want to stop it and return an error to the user, so that my server is not overloaded. How can I do that?
Do you specifically need to run the call in a subshell like that? If not, use timeout and backticks:
require 'timeout'
Timeout.timeout(3) do
puts `tree /` # raises an exception, which you can rescue and handle
end
If you do need to run it externally, though, I'd go with something like Subexec
In general, try wrapping the call in a SystemTimer. https://rubygems.org/gems/SystemTimer
In your particular case, try system("wget -T #{timeout_in_seconds}")

Why does GetBasicPropertiesAsync() sometimes throw an Exception?

In Windows8, I'm trying to use GetBasicPropertiesAsync() to get the size of a newly created file. Sometimes, but not always (~25% of the time), this call gives an exception of:
"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))".
The file is created using DotNetZip. I'm adding thousands of files to the archive which takes a few minutes to run:
using (ZipFile zip = new ZipFile())
{
zip.AddFile(...); // for thousands of files
zip.Save(cr.ArchiveName);
}
var storageFile = await subFolder.GetFileAsync(cr.ArchiveName);
// storageFile is valid at this point
var basicProperties = await storageFile.GetBasicPropertiesAsync(); // BOOM!
A few apparently random things seem to decrease the likelihood of the exception:
Deleting an existing copy of cr.ArchiveName before the start of the loop.
Not viewing the directory using File Explorer
Weird, huh? It smells like it might be a bug related to File System Tunneling or maybe it's some internal caching that DotNetZip is performing and holding onto resources (maybe renaming the TEMP file) even after the ZipFile is disposed?
Trying to (unsuccessfully) answer my own question.
At first, I though this was a known issue with DotNetZip holding onto file handles until the next garbage collection. I am using the SL/WP7 port of DotNetZip from http://slsharpziplib.codeplex.com/ which presumably doesn't include the bug fixed by this workitem:
http://dotnetzip.codeplex.com/workitem/12727
But, according to that theory, doing:
GC.Collect();
GC.WaitForPendingFinalizers();
should have provided a work around, which it didn't.
Next I tried using handle, which didn't show any other activity on the failing StorageFile.
So for now, I'm still stumped.

SMJobRemove succeeds, but plist and helper tool not deleted

I'm trying to remove a privileged helper tool installed via SMJobBless, I'm getting a positive return value and no errors, yet the files at /Library/PrivilegedTools and /Library/LaunchDaemons are not deleted. Do I have to delete these files myself?
From the documentation I read:
Return Value true if the job was removed successfully, otherwise
false.
I'm calling the following to remove the job:
result = SMJobRemove(kSMDomainSystemLaunchd, (__bridge CFStringRef)label, _authRef, YES, &errorCF);
Thanks jatoben, that thread had the answer I was looking for.
As suspected you do have to remove the files yourself or use the following: (Taken from Apple dev forums:)
SMJobRemove is the equivalent of "launchctl remove". That is, it
removes the job from launchd but has no effect on the disk at all.
Thus the job will get reloaded the next time you start up. To get
around that you have to either remove the plist yourself or by
fork/exec'ing "launchctl unload -w".
Have you seen https://github.com/brenwell/SMJobBless-Demo/blob/master/Uninstall.sh? It was very helpful for me.

SSIS 2005 flat file source - partial row which isn't actually a partial row

I'm currently working on an SSIS package to load mainframe logs from multiple server/file sources into a database.
As it stands at the moment I'm using a foreach loop container to loop through a recordset containing filenames and load the files using a Data Flow task from a Flat File Source and File connection to an OLE DB Destination through a Derived column.
I've built in error handling on the Data Flow task to allow for the fact that there won't always be a log file in the location specified (ie. because the server was down for maintenance during a specific period as the files are generated on an hourly basis), but the problems start after it finishes handling these errors.
If the file immediately following an attempt to load a file that wasn't found exists it begins to load it but then throws the following warning message: [Message Log File Source (NORDXSL) [57]] Warning: There is a partial row at the end of the file., and doesn't load all of the records in that file.
However, when I remove the files I know won't exist from the recordset (so that it only attempts to load files that do exist, including the one with the alleged "partial row"), everything works fine and all files/rows are loaded without a problem. It just seems to not want to load the first file after it's failed a missing file correctly and I can't for the life of me work out why?
I've tried calling Dispose() and ReleaseConnection() on the file connection after the Data Flow task has finished processing but this makes no difference and I'm now completely out of ideas.
Any help would be really appreciated as this is the last bug in this project and I want to get it out the door. PLEASE!!
Thanks,
James
I've now found a workaround for this problem...
I've added a Script Task before the Data Flow Task to load the files that checks to see if the file I want to read exists:
If (System.IO.File.Exists(Dts.Variables("MQLogMessagePath").Value.ToString)) Then
Dts.TaskResult = Dts.Results.Success
Else
Dts.TaskResult = Dts.Results.Failure
End If
If it doesn't exist it fails the iteration of the Foreach Loop container and continues onto the next file.
BINGO!