Solved

Write file in each Drive

Posted on 2011-09-29
14
395 Views
Last Modified: 2012-08-13
Hi,

i want add "autorun.inf" in each drive with few attributes like faReadOnly or faHidden or faSysFile or faArchive. Anyoone can help me with source code? If previously "autorun.inf"  exist in drive then also delete it and create new one.



Thanks
0
Comment
Question by:prasiddutta
  • 8
  • 6
14 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36890240
easiest way to create text files is to use TStringList to build it then .SaveToFile to create or overwrite it.
What will be the content of this autorun ? Same for all drives ?
you'll have also to be able to list the drives you want.

Here it is all wrapped in
Type
 TDriveType=(dtUnknown,dtNotMounted,dtRemovable,dtFixed,dtRemote,dtCDRom,dtRAMDisk);
 TDriveTypeSet=set of TDriveType;
 
function DriveType(Drive: Char): TDriveType;
Var
 DriveStr:String;
begin
 Result:=dtNotMounted;
 Drive:=UpCase(Drive);
 if Not Drive in ['A'..'Z'] Then Exit;
 DriveStr:=Drive+':\';
 Result:=TDriveType(GetDriveType(PChar(DriveStr));
end;

function DiskInDrive(Drive: ANSIChar): Boolean;
var EMode: Word;
begin
 Result := False;
 Drive:=UpCase(Drive);
 if Not Drive in ['A'..'Z'] Then Exit;
 EMode := SetErrorMode(SEM_FAILCRITICALERRORS);
 try
  Result:=DiskSize(Ord(Drive)-$40)>=0;
 finally
  SetErrorMode(EMode);
 end;
end;

function GetDriveList(DriveTypes:TDriveTypeSet;MountedOnly:Boolean=False):ANSIString;
var
 D:ANSIChar;
begin
 Result:='';
 for D:='A' to 'Z' do 
  if DriveType(D) In DriveTypes Then
   if (Not MountedOnly) Or DiskInDrive(D) Then
    Result:=Result+D;
end;

procedure CreateAutorun(Drives:String);
Var
 i:integer;
 Autorun:TStringList;
 DriveList,AutoRunFileName:ANSIString;
begin
 DriveList:=GetDriveList([dtRemovable,dtFixed],True); // Get only fixed or inserted removable, local disk
 AutoRunFileName:='C:\AutoRun.inf'; // first letter will change 
 Autorun:=TStringList.Create;
 // fill Autorun here if file is common to all drives
 for i:=1 to Length(DriveList) do
  begin
   // fill Autorun here if file is specific to each drive
   AutoRunFileName[1]:=DriveList[i];
   AutoRun.SaveToFile(AutoRunFileName);
   // set file attributes
   FileSetAttr(AutoRunFileName, faReadOnly Or faHidden or faSysFile or faArchive );
  end;
 Autorun.Free;
end;

Open in new window

0
 

Author Comment

by:prasiddutta
ID: 36890301
Yes all autorun.inf same and it'll protect my all drive from autorun virus.
0
 

Author Comment

by:prasiddutta
ID: 36890396
I make some mistake in your code. Can you share Unit? or upload code elsewhere? Thanks
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:epasquier
ID: 36890408
what do you mean you made some mistake ? Can't you copy the code I gave you ?
0
 

Author Comment

by:prasiddutta
ID: 36890415
give much error in your code. I use Delphi XE2
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 36890430
ok, I understand that I made a few syntax errors. Sorry, I typed that code in a plain text editor.
I've fixed it and tested it, it's ok now
Type
 TDriveType=(dtUnknown,dtNotMounted,dtRemovable,dtFixed,dtRemote,dtCDRom,dtRAMDisk);
 TDriveTypeSet=set of TDriveType;
 
function DriveType(Drive: ANSIChar): TDriveType;
Var
 DriveStr:String;
begin
 Result:=dtNotMounted;
 Drive:=UpCase(Drive);
 if Not (Drive in ['A'..'Z']) Then Exit;
 DriveStr:=Drive+':\';
 Result:=TDriveType(GetDriveType(PChar(DriveStr)));
end;

function DiskInDrive(Drive: ANSIChar): Boolean;
var EMode: Word;
begin
 Result := False;
 Drive:=UpCase(Drive);
 if Not (Drive in ['A'..'Z']) Then Exit;
 EMode := SetErrorMode(SEM_FAILCRITICALERRORS);
 try
  Result:=DiskSize(Ord(Drive)-$40)>=0;
 finally
  SetErrorMode(EMode);
 end;
end;

function GetDriveList(DriveTypes:TDriveTypeSet;MountedOnly:Boolean=False):ANSIString;
var
 D:ANSIChar;
begin
 Result:='';
 for D:='A' to 'Z' do
  if DriveType(D) In DriveTypes Then
   if (Not MountedOnly) Or DiskInDrive(D) Then
    Result:=Result+D;
end;

procedure CreateAutorun;
Var
 i:integer;
 Autorun:TStringList;
 DriveList,AutoRunFileName:ANSIString;
begin
 DriveList:=GetDriveList([dtRemovable,dtFixed],True); // Get only fixed or inserted removable, local disk
 AutoRunFileName:='C:\AutoRun.inf'; // first letter will change 
 Autorun:=TStringList.Create;
 // fill Autorun here if file is common to all drives
 for i:=1 to Length(DriveList) do
  begin
   // fill Autorun here if file is specific to each drive
   AutoRunFileName[1]:=DriveList[i];
   AutoRun.SaveToFile(AutoRunFileName);
   // set file attributes
   FileSetAttr(AutoRunFileName, faReadOnly Or faHidden  );
  end;
 Autorun.Free;
end;

Open in new window

0
 

Author Comment

by:prasiddutta
ID: 36890491
Now I just call

CreateAutorun;

Open in new window


Nice work man. You are great, your first code was a error related PChar and so on.

And I ask one thing. Suppose I insert a USB stck and autorun.inf is found with virus. I want to block instantly and remove inf file. Possible.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36890623
yes it's possible. Don't quite remind how to intercept removable drive insertion, but that exists.
Post a new question
0
 

Author Comment

by:prasiddutta
ID: 36890683
0
 

Author Comment

by:prasiddutta
ID: 36891668
Sometime I saw this error:

"Cannot create file "C:\AutoRun.inf". Access is denied." and program hanged.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36892568
I suppose some process is locking that file just to stop you from erasing it... Virus ?
0
 

Author Comment

by:prasiddutta
ID: 36892683
no virus. I think it don’t got permission to delete.

Exception occur when I call

CreateAutorun;
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36892768
I guess you'll have to find a way to change the permissions or remove lock, that's another problem.
In the meantime, here I improve the CreateAutorun function (tapping error, so that clean up is done correctly)

 for i:=1 to Length(DriveList) do
  try
   // fill Autorun here if file is specific to each drive
   AutoRunFileName[1]:=DriveList[i];
   AutoRun.SaveToFile(AutoRunFileName);
   // set file attributes
   FileSetAttr(AutoRunFileName, faReadOnly Or faHidden  );
  except
  // manage here if you can't save the file
  end;

Open in new window

0
 

Author Comment

by:prasiddutta
ID: 36892893
Thanks, this loop work well
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Making delphi communicate with a c# service 16 111
Delphi: ForceDirectory plain function 7 42
Multi-layered image in FireMonkey 9 48
firemonkey Android Listview Sort items 7 50
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question