renamefile文件夹重命名问题,急,高手给看看!(100分)

  • 主题发起人 主题发起人 kdb9813
  • 开始时间 开始时间
K

kdb9813

Unregistered / Unconfirmed
GUEST, unregistred user!
如果用renamefile(,'c:/test/a/b');<br>后再执行renamefile('c:/test/a','c:/test/b');后,<br>父文件夹更改不成功,如果不关闭程序,手工改的话,提示正在使用,无法更改.<br>如何在改更'c:/test/a/a'后让它变成非使用状态??
 
Renamefile是文件重命名
 
renamefile可能重命名文件夹.已经试过了.没问题的.
 
RenameFile在SysUtils.pas单元,它引用API函数MoveFile:<br>function RenameFile(const OldName, NewName: string): Boolean;<br>begin<br>{$IFDEF MSWINDOWS}<br> &nbsp;Result := MoveFile(PChar(OldName), PChar(NewName));<br>{$ENDIF}<br>{$IFDEF LINUX}<br> &nbsp;Result := __rename(PChar(OldName), PChar(NewName)) = 0;<br>{$ENDIF}<br>end;<br><br>以下是MoveFile的使用说明:<br>请注意下面的说明:The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive. <br><br>the MoveFile function renames an existing file or a directory (including all its children). <br><br>BOOL MoveFile(<br><br> &nbsp; &nbsp;LPCTSTR lpExistingFileName, // address of name of the existing file &nbsp;<br> &nbsp; &nbsp;LPCTSTR lpNewFileName // address of new name for the file <br> &nbsp; ); <br> <br><br>Parameters<br><br>lpExistingFileName<br><br>Points to a null-terminated string that names an existing file or directory. <br><br>lpNewFileName<br><br>Points to a null-terminated string that specifies the new name of a file or directory. The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive. <br><br> <br><br>Return Values<br><br>If the function succeeds, the return value is nonzero.<br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks<br><br>The MoveFile function will move (rename) either a file or a directory (including all its children) either in the same directory or across directories. The one caveat is that the MoveFile function will fail on directory moves when the destination is on a different volume. :
 
to chnplzh:<br>你要看清楚我的说明.
 
楼主的题目好象进行了修改。<br>以下是我临时写的一些代码,测试好好的。<br>楼主可以到这里下载:<br>地址: ftp.mlfjnp.com<br>帐户和密码均为:dfw<br><br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/a','c:/test/a/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/b','c:/test/a/a') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//这里只是简单写一下,没有进行出错判断<br> &nbsp;CreateDirectory('c:/test',nil);<br> &nbsp;CreateDirectory('c:/test/a',nil);<br> &nbsp;CreateDirectory('c:/test/a/a',nil);<br>end;<br><br>end.
 
哦,是父目录,重新修改了一下,测试没问题<br>楼主可以到这里重新下载:<br>地址: ftp.mlfjnp.com<br>帐户和密码均为:dfw<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Button3: TButton;<br> &nbsp; &nbsp;Button4: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure Button3Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button4Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/a','c:/test/a/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/b','c:/test/a/a') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//这里只是简单写一下,没有进行出错判断<br> &nbsp;CreateDirectory('c:/test',nil);<br> &nbsp;CreateDirectory('c:/test/a',nil);<br> &nbsp;CreateDirectory('c:/test/a/a',nil);<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a','c:/test/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/b','c:/test/a') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>end.
 
to chnplzh<br>我是做批量修改,有一个文件列表里面包括文件夹如何修改,而不是像你所说的那样用两个按钮分别修改,如果放在一个按扭事件里情况就不一样了:<br>比如:<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/a','c:/test/a/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br><br> &nbsp;if renamefile('c:/test/a','c:/test/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br><br>end;<br>而且是先更改子目录,再更改父目录,注意顺序.
 
用最底层的吧.安全,可靠...而且效率高.下面例子绝对可以运行.前几天我才刚写的,还热着呢..<br>首先在uses中加入 &nbsp;ShellAPI单元<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> if RenFileDir('E:/test1','E:/test/')then<br> begin<br> showmessage('改名成功!');<br> end else<br> begin<br> showmessage('改名失败!');<br> end;<br>end;<br><br>function TForm1.RenFileDir(const OldName,NewName:string): boolean;<br>var<br> &nbsp;fo: TSHFILEOPSTRUCT;<br>begin<br> &nbsp;FillChar(fo, SizeOf(fo), 0);<br> &nbsp;with fo do<br> &nbsp;begin<br> &nbsp; &nbsp;Wnd := 0;<br> &nbsp; &nbsp;wFunc := FO_RENAME;<br> &nbsp; &nbsp;pFrom := PChar(OldName+#0);<br> &nbsp; &nbsp;pTo := pchar(NewName+#0);<br> &nbsp; &nbsp;fFlags := FOF_NOCONFIRMATION+FOF_SILENT+FOF_NOERRORUI;<br> &nbsp;end;<br> &nbsp;Result := (SHFileOperation(fo) = 0);<br>end;
 
和顺序有关吗?应该不会,楼主可以重新下载我的测试代码:<br>楼主可以到这里重新下载:<br>地址: ftp.mlfjnp.com<br>帐户和密码均为:dfw<br><br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Button3: TButton;<br> &nbsp; &nbsp;Button4: TButton;<br> &nbsp; &nbsp;Button5: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp; &nbsp;procedure Button3Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button4Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button5Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/a','c:/test/a/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a/b','c:/test/a/a') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>//这里只是简单写一下,没有进行出错判断<br> &nbsp;RemoveDir('c:/test/a/a');<br> &nbsp;RemoveDir('c:/test/a/b');<br> &nbsp;RemoveDir('c:/test/b/a');<br> &nbsp;RemoveDir('c:/test/b/b');<br> &nbsp;RemoveDir('c:/test/a');<br> &nbsp;RemoveDir('c:/test/b');<br> &nbsp;RemoveDir('c:/test');<br><br> &nbsp;CreateDirectory('c:/test',nil);<br> &nbsp;CreateDirectory('c:/test/a',nil);<br> &nbsp;CreateDirectory('c:/test/a/a',nil);<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/a','c:/test/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br> &nbsp;if renamefile('c:/test/b','c:/test/a') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br> &nbsp;sleep(100);<br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br> if renamefile('c:/test/a/a','c:/test/a/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br><br> &nbsp;if renamefile('c:/test/a','c:/test/b') then<br> &nbsp; &nbsp; Application.MessageBox('转换成功','提示')<br> &nbsp;else<br> &nbsp; &nbsp; Application.MessageBox('转换不成功','提示');<br><br>end;<br><br>end.
 
哈..问题解决了.主要是我没想到是别的原因,我的程序是先批量更文件夹名再恢复回来,我提出的问题是如何恢复回来,但问题出现在先批量更文件夹名后没有closefile(sr),st:TSearchRec,结果造成"拒绝访问"。没把全部代码贴出来 ,对不住大家了。感觉各位支持。结贴!
 
结贴要给分.
 
后退
顶部