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
November 4, 2014 at 10:34 pm
I tried the create shared folder routine. Running Windows 8.1, Delphi XE6. It compiles, steps through the code with no errors, but does not create the folder or set the folder as shared. As a test I simply cut and pasted your code attempting to create the shared folder C:\Finance
November 4, 2014 at 11:10 pm
You can check the value returned by the create method. and then read the documentation about the code returned http://msdn.microsoft.com/en-us/library/aa389393(v=vs.85).aspx
November 4, 2014 at 11:23 pm
Thank you. It is returning 2= Access Denied. Is this a Wndows 8 security issue? Is there any way to resolve it?
November 4, 2014 at 11:32 pm
Try this http://stackoverflow.com/questions/2536105/i-have-problem-with-sharing-a-folder-through-programming-using-c