关于DeleteFile()的问题,急等!(100分)

  • 主题发起人 主题发起人 crazyjimmy
  • 开始时间 开始时间
C

crazyjimmy

Unregistered / Unconfirmed
GUEST, unregistred user!
for i:=0 to BadList.count-1 do<br>begin<br>&nbsp; &nbsp; &nbsp;if deletefile(BadList) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.lines.add('success!');<br>&nbsp; &nbsp; &nbsp;end else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.lines.add('fail!');<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>在循环中无法删除文件,badList中的文件名路径绝对正确,而且没有打开<br><br>如果不用循环就可以删除<br>procedure TFrm_CheckMedia.RzButton1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;fileName : string;<br>begin<br>&nbsp; &nbsp; &nbsp;fileName := 'D:/Mtv/小小/flash/冲动的惩罚0.dat';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp;//SetFileAttributes(pchar(filename), FILE_ATTRIBUTE_NORMAL);<br><br>&nbsp; &nbsp; &nbsp;if DeleteFile(fileName) then<br>&nbsp; &nbsp; &nbsp;showMessage('k');<br>end;<br>请大家指点迷津!
 
大家帮忙啊!
 
你把BadList)取出来<br>看看文件名称串是否正确
 
绝对正确,都写到memo了
 
for i:=0 to BadList.count-1 do<br>改为 for i := BadList.Count - 1 downto 0
 
TO jsqin:<br>for i:=0 to BadList.count-1 do<br>改为 for i := BadList.Count - 1 downto 0<br>这样改动,有什么含义啊?
 
for iLoop := 0 to (iBadCount-1) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LoopExit then Break;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;badFileStr := BadList[iLoop] ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RzProgressBar.PartsComplete := iLoop;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RzLabel_MediaFilePath.Caption := BadFileStr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//SetFileAttributes(pchar(BadFileStr), FILE_ATTRIBUTE_NORMAL);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if DeleteFile(BadFileStr) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RzMemo.Lines.Add('删除 ' + BadFileStr + ' 成功!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RzMemo.Lines.Add('删除 ' + BadFileStr + ' 失败!'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +IntToHex(GetFileAttributes(pchar(BadFileStr)),4)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +SysErrorMessage(GetLastError)); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>提示“进程无法访问文件,因为另一个程序正在使用此文件”<br>如何知道什么程序打开该文件,怎么关闭呢?
 
最好用 if fileexists(badlist) then ...<br>另外,看样子你可能是用的stringlist 吧 如果是,就是<br>fileexists(badlist.strings)
 
文件存在,就是不知道被什么程序打开了
 
jsqin说得很有道理啊,就是那样. 你仔细看看就明白了.
 
高手们,快来啊!怎么删除一个被其他程序打开的文件
 
使用 processxp 然后 ctrl+h 然后 ctrl+f 查找这个删除失败的文件名
 
代码是正确的,但是你的文件名只要不放出来给大家看,俺们还是怀疑你的文件有问题[:D]
 
不用着急,你用try except 找出错误信息,大家才能帮你。
 
散分了,问题解决!
 
给你个DEMO参考一下....<br>procedure DeleteAllExt(sDir: string; Ext: string = '*.*'); //sDir:来源路径<br>var //<br>&nbsp; pRec: TSearchRec;<br>&nbsp; i, iAttr: Integer;<br>&nbsp; s1: string;<br>&nbsp; sF, dF: string;<br>begin<br>&nbsp; if sDir[Length(sDir)] &lt;&gt; '/' then sDir := sDir + '/';<br><br>&nbsp; i := FindFirst(sDir + Ext, faAnyFile, prec);<br>&nbsp; try<br>&nbsp; &nbsp; while i = 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (pRec.Name = '.') or (pRec.Name = '..') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; i := FindNext(pRec);<br>&nbsp; &nbsp; &nbsp; &nbsp; continue;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; sf := sDir + pRec.Name;<br>&nbsp; &nbsp; &nbsp; DeleteFile(sf);<br>&nbsp; &nbsp; &nbsp; i := FindNext(pRec);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; findClose(pRec);<br>&nbsp; end;<br>end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
686
import
I
I
回复
0
查看
551
import
I
后退
顶部