ドメクラコピー


Ver 0.65 05/01/13

1.紹介

「Windowsドメイ ン内のクライアント100台のデスクトップに、ファイルサーバへのショートカットを作成したい」
こんなシュツエーションが求められて、作成しました。

やっていることは管理共有(c$)の配下にあるAllUsersプロファイルのデスクトップへファイルをコピーしている だけです。
一応、NTと2000/XPではプロファイルの位置が違うので、そこは自動的に判断してます。

ログオンスクリプトでも良いのですが、即時性が求められたためログオフ・ログオンを求めるログオンスクリプトでは満足でき なかっただけです。

ScreenShot

2.動作解説

ドメクラコピーは管理共有を使用するので、ド メインアドミンの権限が必要です。
クライアント一覧にホスト名を指定します(冒頭に\\が必要です)。
クライアント一覧は管理者が作成するか、コマンドラインで「net view」コマンドを使用して取得すれば、現在起動している端末だけを(ざっくりと)取得できます。
NTと2000でコピー先が異なるので指定できます。

3.詳細解説

単純な動作をしているため、ソースも公開しま す。ソースコードはDelphi5で作成されています。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls,filectrl;

type
  TForm1 = class(TForm)
    Memo1: TMemo; //コピー対象クライアント指定
    Memo2: TMemo; //エラークライアント出力
    Memo3: TMemo; //成功クライアント出力
    Button1: TButton;
    ProgressBar1: TProgressBar;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel; //処理中クライアント名
    CheckBox1: TCheckBox; //ファイルの上書きフラグ
    procedure Button1Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
    h,h1,c1,c2,p1,p2:string;
    b:boolean;
begin

  setcurrentdir(extractfilepath(paramstr(0)));

  if memo1.Lines.Count=0 then
    begin
      ShowMessage(' クライアント名が指定されていません');
      exit;
    end;

  c1:=ExpandFileName(edit3.text); //ファイルを絶対パスで取り出す

  if not(fileexists(c1)) then
    begin
      ShowMessage(' コピーファイル名が間違っています');
      exit;
    end;

  c2:='\'+extractfilename(c1);
  p1:=edit1.text;
  p2:=edit2.text;

  //仕様として「コピー先パスの先頭には\が要り、最後にはいらない」
  if copy(p1,1,1)<>'\' then p1:='\'+p1;
  if copy(p1,length(p1),1)='\' then delete(p1,length(p1),1);
  if copy(p2,1,1)<>'\' then p2:='\'+p2;
  if copy(p2,length(p2),1)='\' then delete(p2,length(p2),1);

  progressbar1.Position:=0;
  progressbar1.Max:=memo1.Lines.Count;

  for i:=0 to memo1.lines.Count-1 do
    begin

      b:=false;

      h:=trimright(memo1.lines[i]);
      h1:=h;
      label7.Caption:=h;

      application.ProcessMessages;

      if DirectoryExists(h+p1) then
        begin                 //\\client \folder \file  変数の\文字責任
          if fileexists(h+p1+c2) then
            begin
              h1:='(*) '+h;  //ファイル既存マーク
              if checkbox1.Checked then //上書きするか?
                b:=CopyFile(pchar(c1),pchar(h+p1+c2),false);
            end  //True=成功
          else
            begin
              b:=CopyFile(pchar(c1),pchar(h+p1+c2),false);
            end;
        end
      else
      if DirectoryExists(h+p2) then
        begin
          if fileexists(h+p2+c2) then
            begin
              h1:='(*) '+h;  //ファイル既存マーク
              if checkbox1.Checked then //上書きするか?
              b:=CopyFile(pchar(c1),pchar(h+p1+c2),false);
            end //True=成功
          else
            begin
              b:=CopyFile(pchar(c1),pchar(h+p2+c2),false);
            end;
        end;

      if b then
       memo3.Lines.Add (h1)  //成功ホスト一覧
      else
        memo2.Lines.Add(h1); //失敗ホスト一覧

      progressbar1.StepIt;
    end;

  ShowMessage('処理が終了しました。');

end;

end.


ソースコード

prev.gif