![](/img/trans.png)
[英]How to delete the contents of temp folder in remote machine through command
[英]How I can list the contents of a folder of a remote machine
我正在尋找 windows api function 或其他方式來獲取位於我 LAN 上的機器中的文件夾的內容(文件夾和文件)。 當然,對於我想要訪問的每台機器,我都有一個有效的 windows 用戶和密碼。
您可以使用 WMI,檢查CIM_DataFile
和CIM_Directory
類。
1.首先您必須在客戶端機器中啟用 wmi 遠程訪問。 閱讀這些文章,了解如何執行此操作以及 windows 版本之間的差異Connecting to WMI on a Remote Computer
, Securing a Remote WMI Connection
。
2.始終必須使用過濾器(Where 條件)來限制這些 WMI 類的結果。
3.始終必須使用Drive
字段作為條件,因為這些類返回所有驅動器的文件。
4.Wmi 將\
(反斜杠)字符解釋為保留符號,因此您必須對該字符進行轉義以避免WQL 語句出現問題。
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj,
Variants;
procedure GetRemoteFolderContent(Const WbemComputer,WbemUser,WbemPassword,Path:string);
const
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
WmiPath : string;
Drive : string;
begin;
//The path
//Get the drive
Drive :=ExtractFileDrive(Path);
//get the path and add a backslash to the end
WmiPath :=IncludeTrailingPathDelimiter(Copy(Path,3,Length(Path)));
//escape the backslash character
WmiPath :=StringReplace(WmiPath,'\','\\',[rfReplaceAll]);
Writeln('Connecting');
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
//Establish the connection
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
Writeln('Files');
Writeln('-----');
//Get the files from the specified folder
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM CIM_DataFile Where Drive="%s" AND Path="%s"',[Drive,WmiPath]),'WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('%s',[FWbemObject.Name]));
FWbemObject:=Unassigned;
end;
Writeln('Folders');
Writeln('-------');
//Get the folders from the specified folder
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM CIM_Directory Where Drive="%s" AND Path="%s"',[Drive,WmiPath]),'WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(Format('%s',[FWbemObject.Name]));
FWbemObject:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
GetRemoteFolderContent('remote_machine','user','password','C:\');
GetRemoteFolderContent('remote_machine','user','password','C:\Program Files');
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
沒有授權部分,這很簡單。 進行授權的正確方法是調用 Windows.pas 方法WNetAddConnection2
和 go 方法。
但是,因為我處於簡單的黑客模式,所以我嘗試了這個,它基本上可以工作:
uses Types, IOUtils, ShellApi; // Works in Delphi XE.
procedure TForm5.Button1Click(Sender: TObject);
var
dirs:TStringDynArray;
files:TStringDynArray;
apath, dir,filename:String;
begin
ListBox1.Items.Clear;
apath := '\\hostname\sharename';
// This should be calling WNetAddConnection2:
// instead It's an evil (portable) hack.
ShellExecute(HWND(0), 'open', PChar('net use /delete '+ apath),
nil,nil,SW_SHOW );
ShellExecute(HWND(0), 'open', PChar('net use '+ apath+' /user:uid pswd'),
nil,nil,SW_SHOW );
dirs := TDirectory.GetDirectories(apath);
if Length(dirs)=0 then
ListBox1.Items.Add('None found.')
else
for dir in dirs do
ListBox1.Items.Add('Directory: '+dir);
files := TDirectory.GetFiles(apath);
for filename in files do
ListBox1.Items.Add('File: '+filename );
end;
為 ShellExecute “網絡使用”的丑陋黑客行為道歉。 (Grin) 請注意,我選擇“掛載”這個共享文件夾而不給它一個驅動器號,避免了如果該驅動器已經映射了該怎么辦的問題。
這是一個很好的鏈接,其中包含一個 WNetAddConnection2 代碼示例,我將鏈接到該鏈接而不是偷獵。 它展示了一個非邪惡方法的示例。 :-) 然后您可以使用上面顯示的目錄枚舉代碼。
我想這包含在 Warren 的回答中,但切入正題, IOUtils.TDirectory支持 UNC:
implementation
uses IOUtils,types;
procedure GetFiles;
var
i: integer;
files: TStringDynArray;
begin
files := TDirectory.GetFiles('\\aServer\aPath\aShare\', '*.aFileFilter');
for i := Low(files)to High(files) do
memo1.Lines.Add(files[i]);
end;
等等等等……
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.