Hi,
I am trying to make a little application to revert the effect of some
viruses on USB memories, and running it from Windows encountered some
exceptions that IMHO should not have happened.
Maybe I am missing something. Can you please check the commented blocks
for errors?
Also, is there a way from Linux to remove attributes readonly, system,
hidden ?
Regards, Hugo
import std.stdio;
import std.path;
import std.regex;
import std.file;
import std.string;
static string info = "CureUSB 0.3 (2014-01-14)";
static string str_moved = "Moved";
static string str_deleted = "Deleted";
static string str_finished = "Done";
static string str_setattrib = "Attributes set";
static string str_safename = "SafeToDelete";
// Expression to match directories with blanks for name
static auto re = regex(`^\s+$`);
version(Windows)
{
import core.sys.windows.windows;
extern(Windows) uint SetFileAttributesA(LPCSTR, DWORD);
}
int main()
{
string ExePath = thisExePath();
string SearchPath = rootName(ExePath);
string BlankDirToDelete;
version(Windows) SetConsoleOutputCP(65001);
writefln(info);
foreach(DirEntry d; dirEntries(SearchPath, SpanMode.shallow))
{
auto m = matchFirst(baseName(d.name), re);
if (m.captures.length > 0)
{
if (isDir(d.name))
{
foreach(DirEntry f; dirEntries(d.name, SpanMode.shallow))
{
string origname = buildPath(SearchPath, baseName(f.name));
rename(f.name, origname);
writefln(`%s "%s"`, str_moved, origname);
}
BlankDirToDelete = d.name;
}
break;
}
}
// The following block does not work; supposedly the process does not
have
// access to the directory because it is being used by another
process,
// which does not seem to be the case. Uncomment to try
/*
if (exists(BlankDirToDelete)) try
{
string SafeToBeDeleted = buildPath(SearchPath, str_safename)
rename(BlankDirToDelete, SafeToBeDeleted);
remove(SafeToBeDeleted);
}
catch (FileException e)
writeln(e.msg);
*/
foreach(DirEntry d; dirEntries(SearchPath, SpanMode.shallow))
{
if (d.name != ExePath)
{
string bname = baseName(d.name);
version(Windows)
{
if ( (bname != "RECYCLER")
&& (bname != "$Recycle.bin")
&& (bname != "System Volume Information")
&& (bname != "Recovery")
&& (bname != "MSOCache")
) SetFileAttributesA(toStringz(d.name), FILE_ATTRIBUTE_NORMAL +
FILE_ATTRIBUTE_ARCHIVE);
}
// The following block gives a FileException, claiming that the
specified file
// could not befound. Uncomment to try
/*
string exten = extension(d.name);
if (exten.length > 0)
{
// writefln(`"%s" "%s"`, d.name, extension(d.name)); //debug
line
if ( (exten == ".exe")
|| (exten == ".lnk")
|| (exten == ".scr")
|| (exten == ".cpl")
|| (exten == ".hta")
|| (exten == ".com")
|| (exten == ".bat")
|| (exten == ".vb")
|| (exten == ".vbs") )
if (isDir(buildPath(SearchPath,
stripExtension(baseName(d.name)))))
{
remove(d.name);
writefln(`%s "%s"`, str_deleted, d.name);
}
}
*/
}
}
writefln("%s", str_setattrib);
writefln("%s.", str_finished);
return 0;
}