Home > Delphi, WMI > WMI Tasks using Delphi – Files and Folders

WMI Tasks using Delphi – Files and Folders

How do I rename a local or remote file?

Use the CIM_DataFile class. Ensure that you pass the entire path name when calling the Rename method, for example, “C:\Foo\Test.txt” instead of “Text.txt”.

const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
  FileName      : string;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FileName      := 'C:\\Foo\\Test.txt';//look how the name of the file to rename is escaped
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM CIM_DataFile Where Name="%s"',[FileName]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    FWbemObject.Rename('C:\Foo\Test_new.txt');
    FWbemObject:=Unassigned;
  end;
end;

How do I determine whether users have .MP3 files stored on their computer?

Use the CIM_DataFile class and select files using the following WQL WHERE clause: Where Extension = “MP3″.

const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT Name,Extension,Path FROM CIM_DataFile Where Extension="%s"',['mp3']),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    Writeln(Format('File Name %s.%s',[String(FWbemObject.Name),String(FWbemObject.Extension)]));
    Writeln(Format('Path      %s',[String(FWbemObject.Path)]));
    FWbemObject:=Unassigned;
  end;
end;

How do I create shared folders on a computer?

Use the Win32_Share class and the Create method.

const
  FILE_SHARE          = 0;
  MAXIMUM_CONNECTIONS = 25;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObject   : OLEVariant;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  FWbemObject   := FWMIService.Get('Win32_Share');
  FWbemObject.Create('C:\Finance', 'FinanceShare', FILE_SHARE, MAXIMUM_CONNECTIONS, 'Public share for the Finance group.');
end;

For more samples of using the WMI and Delphi to handle files and folders in local and remote machines check this article Manipulating local/remote files and folders using Delphi and WMI

This post is based in the MSDN entry WMI Tasks: Files and Folders

Advertisement
Categories: Delphi, WMI
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 48 other followers