線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2691
推到 Plurk!
推到 Facebook!

如何判斷文字檔正在使用中

尚未結案
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-02-20 17:07:13 IP:211.75.xxx.xxx 未訂閱
我現有個排程,隔一段時間, 會從A路徑的檔案搬移到B路徑, A路徑的檔案, 可能是由FTP傳來的, 或是由其他USER直接開檔寫入, 現在有個問題, 當我要搬移檔案時,  我要如何判斷 該檔案可能正在FTP中, 尚未下傳完成, 或USER正在編輯檔案, 尚未存入??    
ralph
初階會員


發表:41
回覆:82
積分:29
註冊:2003-02-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-02-21 00:11:06 IP:66.171.xxx.xxx 未訂閱
function FileInUse(FileName: string): Boolean; var hFileRes: HFILE; begin Result := False; if not FileExists(FileName) then exit; hFileRes := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (hFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(hFileRes); end;
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-02-23 11:51:38 IP:211.75.xxx.xxx 未訂閱
我以word叫出文字檔, 可測試出該檔案正被使用中, 再以WordPad叫出文字檔, 則無法測試出該檔案正被使用中, 想再請問, 若文字檔沒被鎖定, 是採分享的方式, 那有何方式, 判斷有人正在使用中???
ralph
初階會員


發表:41
回覆:82
積分:29
註冊:2003-02-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-02-24 01:20:03 IP:66.171.xxx.xxx 未訂閱
There is one function to do this. HFILE OpenFile( LPCSTR lpFileName, // pointer to filename LPOFSTRUCT lpReOpenBuff, // pointer to buffer for file information UINT uStyle // action and attributes ); If it returns "HFILE_ERROR", it means " File in use".
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-02-24 13:42:24 IP:211.75.xxx.xxx 未訂閱
 function FileLocked(Fn: string): Boolean;
var
  I : Integer;
  Struct: TOfStruct;
  Style: Cardinal;
  Hdl: Hfile;
  Drive: String;
begin
  Style := OF_Share_Exclusive; //排它方式打開
  Drive := UpperCase(Fn[1]);
  Struct.fFixedDisk := Ord(Drive <> 'A'); //判斷是否是硬碟
  Struct.cBytes := SizeOf(Struct);
  For I := 1 to Length(Fn) do
  Struct.szPathName[I-1] := Fn[I];
  Struct.szPathName[I] := Chr(0); //填充文件名
  Hdl := OpenFile(Pchar(Fn), Struct, Style);
  if Hdl = HFILE_ERROR then  begin
     Result := True; //文件被鎖定
     Showmessage(SysErrorMessage(GetLastError)); //顯示錯誤原因
  end  else
     Result := False;
end;
我試過以
 if FileLocked('c:\temp\A.Txt') then 
    showmessage('File in Use')
 else
   showmessage('File not Use');
 
在執行這段之前, 先以WordPad打開'c:\temp\A.Txt' , 結果是showmessage('File not Use'), 我想大概檔案一定要是鎖定狀態, 才能判斷是否使用中吧!! 若採分享狀態, 其他的程序是無法判斷是否使用中吧??? 還是謝謝 >
ralph
初階會員


發表:41
回覆:82
積分:29
註冊:2003-02-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-02-24 14:07:12 IP:66.171.xxx.xxx 未訂閱
It works for me.    Try to chnage if FileLocked('c:\temp\A.Txt')  to "if Not FileLocked('c:\temp\a1.txt')" I saw the source codes did like that. 發表人 - ralph 於 2004/02/24 14:08:58
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-02-25 11:11:54 IP:211.75.xxx.xxx 未訂閱
對不起, 我不太懂吔!! >
ralph
初階會員


發表:41
回覆:82
積分:29
註冊:2003-02-04

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-02-25 22:38:37 IP:66.171.xxx.xxx 未訂閱
I think the function name "FileLock" confused you.    You need to focus on the function "OpenFile".    Let's try to say it in Chinese "This file can be open? No, it is failed.(Result:=false). You mean it is locked(Result:=true)".    if Hdl = HFILE_ERROR then begin Result := True; //文件被鎖定 Showmessage(SysErrorMessage(GetLastError)); //顯示錯誤原因 end else Result := False; if Hdl = HFILE_ERROR then begin Result := false; //The file is failed to open (Can we open the file? No! So, the file is locked? Yes!) end else Result := true; To think the following line: if (OpenFile(....) <> HFILE_ERROR) then showmessage('...successfully'); or if (OpenFile(....)) then showmessage('...successfully'); Sorry! I do not have Chinese keyboard on my hand. I hope my explanation in English is clear. 發表人 - ralph 於 2004/02/26 00:11:26
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-02-27 14:17:00 IP:147.8.xxx.xxx 未訂閱
引言:我以word叫出文字檔, 可測試出該檔案正被使用中, 再以WordPad叫出文字檔, 則無法測試出該檔案正被使用中, 想再請問, 若文字檔沒被鎖定, 是採分享的方式, 那有何方式, 判斷有人正在使用中???< face="Verdana, Arial, Helvetica"> For Notepad and Wordpad, the WHOLE file is being read into the application's memory and closed. You can even delete the file afterwards. So I think the best you can do is detecting whether the file is being opened (i.e. fail in acquiring an exlusing lock) or not. Refering to your original question, please test if your ftp server should hold a lock on the file during transfer (probably yes). Or you could use the file creation/modification datetime for some kind of additional reference.
ko
資深會員


發表:28
回覆:785
積分:444
註冊:2002-08-14

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-02-27 17:12:01 IP:61.221.xxx.xxx 未訂閱
alice 你好: 下面是一篇有關win32檔案異動的訊息得知或許post不完全,若是要詳細資料 可能要請你搜尋一下了 uses Windows, Messages,shlobj ..... const SHCNE_RENAMEITEM = $1; SHCNE_CREATE = $2; SHCNE_DELETE = $4; SHCNE_MKDIR = $8; SHCNE_RMDIR = $10; SHCNE_MEDIAINSERTED = $20; SHCNE_MEDIAREMOVED = $40; SHCNE_DRIVEREMOVED = $80; SHCNE_DRIVEADD = $100; SHCNE_NETSHARE = $200; SHCNE_NETUNSHARE = $400; SHCNE_ATTRIBUTES = $800; SHCNE_UPDATEDIR = $1000; SHCNE_UPDATEITEM = $2000; SHCNE_SERVERDISCONNECT = $4000; SHCNE_UPDATEIMAGE = $8000; SHCNE_DRIVEADDGUI = $10000; SHCNE_RENAMEFOLDER = $20000; SHCNE_FREESPACE = $40000; SHCNE_ASSOCCHANGED = $8000000; SHCNE_DISKEVENTS = $2381F; SHCNE_GLOBALEVENTS = $C0581E0; SHCNE_ALLEVENTS = $7FFFFFFF; SHCNE_INTERRUPT = $80000000; SHCNF_IDLIST = 0; // LPITEMIDLIST SHCNF_PATHA = $1; // path name SHCNF_PRINTERA = $2; // printer friendly name SHCNF_DWORD = $3; // DWORD SHCNF_PATHW = $5; // path name SHCNF_PRINTERW = $6; // printer friendly name SHCNF_TYPE = $FF; SHCNF_FLUSH = $1000; SHCNF_FLUSHNOWAIT = $2000; SHCNF_PATH = SHCNF_PATHW; SHCNF_PRINTER = SHCNF_PRINTERW; WM_SHNOTIFY = $401; NOERROR = 0; type TForm1 = class(TForm) ... ... .. . type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT; SHNOTIFYSTRUCT = record dwItem1 : PItemIDList; dwItem2 : PItemIDList; end; Type PSHFileInfoByte=^SHFileInfoByte; _SHFileInfoByte = record hIcon :Integer; iIcon :Integer; dwAttributes : Integer; szDisplayName : array [0..259] of char; szTypeName : array [0..79] of char; end; SHFileInfoByte=_SHFileInfoByte; Type PIDLSTRUCT = ^IDLSTRUCT; _IDLSTRUCT = record pidl : PItemIDList; bWatchSubFolders : Integer; end; IDLSTRUCT =_IDLSTRUCT; function SHNotify_Register(hWnd : Integer) : Bool; function SHNotify_UnRegister:Bool; //function SHEventName(strPath1,strPath2:string;lParam:Integer):string; Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall; external 'Shell32.dll' index 4; Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord; lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2; Function SHGetFileInfoPidl(pidl : PItemIDList; dwFileAttributes : Integer; psfib : PSHFILEINFOBYTE; cbFileInfo : Integer; uFlags : Integer):Integer;stdcall; external 'Shell32.dll' name 'SHGetFileInfoA'; implementation {$R *.dfm} type USER_INFO_3 = packed record usri3_name : PWideChar; usri3_password : PWideChar; usri3_password_age: PDWord; usri3_priv: PDWord; usri3_home_dir: PWideChar; usri3_comment: PWideChar; usri3_flags: PDWord; usri3_script_path: PWideChar; usri3_auth_flags: PDWord; usri3_full_name: PWideChar; usri3_usr_comment: PWideChar; usri3_parms: PWideChar; usri3_workstations: PWideChar; usri3_last_logon: PDWord; usri3_last_logoff: PDWord; usri3_acct_expires: PDWord; usri3_max_storage: PDWord; usri3_units_per_week: PDWord; usri3_logon_hours: PByte; usri3_bad_pw_count: PDWord; usri3_num_logons: PDWord; usri3_logon_server: PWideChar; usri3_country_code: PDWord; usri3_code_page: PDWord; usri3_user_id: PDWord; usri3_primary_group_id: PDWord; usri3_profile: PWideChar; usri3_home_dir_drive: PWideChar; usri3_password_expired: PDWord; end; type PUSER_INFO_3 = ^USER_INFO_3; function NetUserEnum(ServerName:PWideChar;Level:DWORD;Filter:DWORD;var Buf:Pointer;PrefMaxLen:DWORD; var EntriesRead:PDWord;var TotalEntries:PDWord;var ResumeHandle:PDWord):Longint;stdcall;external 'netapi32.dll' Name 'NetUserEnum'; function SHEventName(strPath1,strPath2:string;lParam:Integer):string; var sEvent:String; begin case lParam of SHCNE_RENAMEITEM: sEvent := '重命名文件' strPath1 '?' strpath2; SHCNE_CREATE: sEvent := '建立文件 檔案名:' strPath1; SHCNE_DELETE: sEvent := '刪除文件 檔案名:' strPath1; SHCNE_MKDIR: sEvent := '新建目錄 目錄名:' strPath1; SHCNE_RMDIR: sEvent := '刪除目錄 目錄名:' strPath1; SHCNE_MEDIAINSERTED: sEvent := strPath1 '中插入可移動存儲介質'; SHCNE_MEDIAREMOVED: sEvent := strPath1 '中移去可移動存儲介質' strPath1 ' ' strpath2; SHCNE_DRIVEREMOVED: sEvent := '移去驅動器' strPath1; SHCNE_DRIVEADD: sEvent := '添加驅動器' strPath1; SHCNE_NETSHARE: sEvent := '改變目錄' strPath1 '的共用屬性'; SHCNE_ATTRIBUTES: sEvent := '改變文件目錄屬性 檔案名' strPath1; SHCNE_UPDATEDIR: sEvent := '更新目錄' strPath1; SHCNE_UPDATEITEM: sEvent := '更新文件 檔案名:' strPath1; SHCNE_SERVERDISCONNECT: sEvent := '(斷)伺服器的連接' strPath1 ' ' strpath2; SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE'; SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI'; SHCNE_RENAMEFOLDER: sEvent := '重命名文件夾' strPath1 '?' strpath2; SHCNE_FREESPACE: sEvent := '磁碟空間大小改變'; SHCNE_ASSOCCHANGED: sEvent := '改變文件關聯'; else sEvent:='未知操作' IntToStr(lParam); end; Result:=sEvent; end; } function SHNotify_Register(hWnd : Integer) : Bool; var ps:PIDLSTRUCT; begin Result:=False; new(ps); If m_hSHNotify = 0 then begin if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP, m_pidlDesktop)<> NOERROR then Form1.close; if Boolean(m_pidlDesktop) then begin ps.bWatchSubFolders := 1; ps.pidl := m_pidlDesktop; m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST), (SHCNE_ALLEVENTS Or SHCNE_INTERRUPT), WM_SHNOTIFY, 1, ps); Result := Boolean(m_hSHNotify); end Else CoTaskMemFree(m_pidlDesktop); End; end; function SHNotify_UnRegister:Bool; begin Result:=False; If Boolean(m_hSHNotify) Then If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin {$R-} m_hSHNotify := 0; CoTaskMemFree(m_pidlDesktop); Result := True; {$R-} End; end;
------
======================
昏睡~
不昏睡~
不由昏睡~
alice
初階會員


發表:41
回覆:49
積分:28
註冊:2002-04-30

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-03-01 17:22:47 IP:211.75.xxx.xxx 未訂閱
感謝各位的鼎力幫忙,我的問題獲得解決了!! 對wordpad,也清楚是怎麼回事!!
系統時間:2024-07-06 4:26:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!