急!请大侠帮忙看看我的源码错在什么地主?我新建的txt文件为什么不能用??? ( 积分: 0 )

  • 主题发起人 主题发起人 lsxysuper
  • 开始时间 开始时间
L

lsxysuper

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm3.shiyanreadtimeClick(Sender: TObject);<br>var<br> &nbsp; &nbsp;myfile:string;<br> &nbsp; &nbsp;prufile:string;<br> &nbsp; &nbsp;f:textfile;<br> &nbsp; // f:file;<br> &nbsp; &nbsp;timetmp:string;<br> &nbsp; &nbsp;timetmp2:string;<br> &nbsp; &nbsp;rehosttime:string;<br> &nbsp; &nbsp;i:integer;<br> &nbsp; &nbsp;len:integer;<br> &nbsp; &nbsp;t:integer;<br> &nbsp; &nbsp;strCmd:string;<br>begin<br><br> &nbsp; &nbsp; &nbsp;myfile:='d:/project2server.exe';<br> &nbsp; &nbsp; &nbsp;prufile:='h:';<br> &nbsp; &nbsp; &nbsp;winexec('cmd /c net use h: //10.20.40.136/super &quot;&quot; /y',sw_shownormal);<br> &nbsp; &nbsp; &nbsp;//将远程机的super共享文件夹映射为 h:<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;if not fileexists('h:/project2server.exe') then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winexec('cmd /c copy d:/project2server.exe h:',sw_shownormal);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //将本地d:/project2server.exe 复制到 H:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; //若在C:根目录下有time.txt,则删除此time.txt<br> &nbsp; &nbsp; If FileExists('c:/time.txt') then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp;showmessage('the fiel is exists!');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winexec('cmd /c attrib -R c:/time.txt',sw_shownormal); &nbsp;//将文件的只读属性清楚<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deletefile('c:/time.txt');<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; //新建time.txt<br> &nbsp; &nbsp;// tfilestream.Create('c:/time.txt',fmcreate);<br> &nbsp; &nbsp; filecreate('c:/time.txt');<br> &nbsp; &nbsp; sleep(5000);<br> &nbsp; &nbsp; winexec('cmd /c attrib -R c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; //将文件的只读属性清楚<br> &nbsp; &nbsp; winexec('cmd /c net time //10.20.40.136 &gt;&gt;c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; &nbsp;//获取远程机的时间 并导入到time.txt中<br> &nbsp; &nbsp; sleep(1000);<br> &nbsp; // &nbsp;winexec('cmd /c net time //10.20.40.64 &gt;&gt;c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; winexec('cmd /c attrib +R c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; //将文件的属性设为只读,因为下面的reset(f)要求所对应的文件属性需为只读<br> &nbsp; // &nbsp; filesetattr('c:/time.txt',faReadOnly);<br> &nbsp; // &nbsp; filesetattr('c:/time.txt',1);<br> &nbsp; &nbsp; //get the time form &nbsp;c:/time.txt<br> &nbsp; &nbsp; &nbsp;assignfile(f,'c:/time.txt');//将文件名字符串str赋给文件变量f,程序对文件变量f的操作代替对文件str的操作<br> &nbsp; &nbsp; &nbsp;//打开一个已经存在的磁盘文件,其文件名必须先由assign过程赋给变量f,该文件只能读,指向文件元素的指针指向第一个元素。<br> &nbsp; &nbsp; &nbsp;reset(f); // 此文件只能读<br> &nbsp; &nbsp; &nbsp; //以下是将time.txt中的时间获取回来 并传给at 命令<br> &nbsp; &nbsp; &nbsp;readln(f,timetmp);<br> &nbsp; &nbsp; &nbsp;timetmp2:=timetmp;<br> &nbsp; &nbsp; &nbsp;i:=pos('午 ',timetmp);<br>// &nbsp; &nbsp; &nbsp;len:=length(timetmp);<br> &nbsp; &nbsp; &nbsp;rehosttime:=copy(timetmp,i+3,5);<br> &nbsp; &nbsp; &nbsp;if pos('下',timetmp2)&gt;0 then<br> &nbsp; &nbsp; &nbsp;begin &nbsp;//若为下午,则加上12<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timetmp2:=copy(rehosttime,1,2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=strtoint(timetmp2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=t+12;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timetmp2:=inttostr(t);<br> &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;rehosttime:=timetmp2;<br> &nbsp; &nbsp; &nbsp;end; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;rehosttime:=timetmp2+copy(rehosttime,3,5);<br> &nbsp; &nbsp; &nbsp;showmessage(rehosttime); //测试获到的时间<br> &nbsp; &nbsp; &nbsp;i:=pos(':',rehosttime);<br> &nbsp; &nbsp; &nbsp;timetmp:=copy(rehosttime,i+1,2);<br> &nbsp; &nbsp; &nbsp;t:=strtoint(timetmp);<br> &nbsp; &nbsp; &nbsp;inc(t);<br> &nbsp; &nbsp; &nbsp;timetmp:=inttostr(t);<br> &nbsp; &nbsp; &nbsp;rehosttime:=copy(rehosttime,1,3)+timetmp;<br> &nbsp; &nbsp; &nbsp;showmessage(rehosttime); //测试获到的时间<br> &nbsp; &nbsp; &nbsp;strCmd:='cmd /c at //10.20.40.136 '+rehosttime+'c:/super/project2server.exe';<br> &nbsp; &nbsp; &nbsp;winexec(pchar(strCmd),sw_shownormal);<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;closefile(f);<br> &nbsp; //end<br> &nbsp; winexec('cmd /c net use h: /d /y',sw_shownormal);<br> &nbsp; //删除映射,准备另一台机子的操作<br>end;<br>我有filecreate('c:/time.txt');新建的time.txt为什么不能用?调度时说我的这个txt正在被另一个程序应用??
 
procedure TForm3.shiyanreadtimeClick(Sender: TObject);<br>var<br> &nbsp; &nbsp;myfile:string;<br> &nbsp; &nbsp;prufile:string;<br> &nbsp; &nbsp;f:textfile;<br> &nbsp; // f:file;<br> &nbsp; &nbsp;timetmp:string;<br> &nbsp; &nbsp;timetmp2:string;<br> &nbsp; &nbsp;rehosttime:string;<br> &nbsp; &nbsp;i:integer;<br> &nbsp; &nbsp;len:integer;<br> &nbsp; &nbsp;t:integer;<br> &nbsp; &nbsp;strCmd:string;<br>begin<br><br> &nbsp; &nbsp; &nbsp;myfile:='d:/project2server.exe';<br> &nbsp; &nbsp; &nbsp;prufile:='h:';<br> &nbsp; &nbsp; &nbsp;winexec('cmd /c net use h: //10.20.40.136/super &quot;&quot; /y',sw_shownormal);<br> &nbsp; &nbsp; &nbsp;//将远程机的super共享文件夹映射为 h:<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;if not fileexists('h:/project2server.exe') then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winexec('cmd /c copy d:/project2server.exe h:',sw_shownormal);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //将本地d:/project2server.exe 复制到 H:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; //若在C:根目录下有time.txt,则删除此time.txt<br> &nbsp; &nbsp; If FileExists('c:/time.txt') then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp;showmessage('the fiel is exists!');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winexec('cmd /c attrib -R c:/time.txt',sw_shownormal); &nbsp;//将文件的只读属性清楚<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deletefile('c:/time.txt');<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; //新建time.txt<br> &nbsp; &nbsp;// tfilestream.Create('c:/time.txt',fmcreate);<br> &nbsp; &nbsp; filecreate('c:/time.txt');<br> &nbsp; &nbsp; sleep(5000);<br> &nbsp; &nbsp; winexec('cmd /c attrib -R c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; //将文件的只读属性清楚<br> &nbsp; &nbsp; winexec('cmd /c net time //10.20.40.136 &gt;&gt;c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; &nbsp;//获取远程机的时间 并导入到time.txt中<br> &nbsp; &nbsp; sleep(1000);<br> &nbsp; // &nbsp;winexec('cmd /c net time //10.20.40.64 &gt;&gt;c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; winexec('cmd /c attrib +R c:/time.txt',sw_shownormal);<br> &nbsp; &nbsp; //将文件的属性设为只读,因为下面的reset(f)要求所对应的文件属性需为只读<br> &nbsp; // &nbsp; filesetattr('c:/time.txt',faReadOnly);<br> &nbsp; // &nbsp; filesetattr('c:/time.txt',1);<br> &nbsp; &nbsp; //get the time form &nbsp;c:/time.txt<br> &nbsp; &nbsp; &nbsp;assignfile(f,'c:/time.txt');//将文件名字符串str赋给文件变量f,程序对文件变量f的操作代替对文件str的操作<br> &nbsp; &nbsp; &nbsp;//打开一个已经存在的磁盘文件,其文件名必须先由assign过程赋给变量f,该文件只能读,指向文件元素的指针指向第一个元素。<br> &nbsp; &nbsp; &nbsp;reset(f); // 此文件只能读<br> &nbsp; &nbsp; &nbsp; //以下是将time.txt中的时间获取回来 并传给at 命令<br> &nbsp; &nbsp; &nbsp;readln(f,timetmp);<br> &nbsp; &nbsp; &nbsp;timetmp2:=timetmp;<br> &nbsp; &nbsp; &nbsp;i:=pos('午 ',timetmp);<br>// &nbsp; &nbsp; &nbsp;len:=length(timetmp);<br> &nbsp; &nbsp; &nbsp;rehosttime:=copy(timetmp,i+3,5);<br> &nbsp; &nbsp; &nbsp;if pos('下',timetmp2)&gt;0 then<br> &nbsp; &nbsp; &nbsp;begin &nbsp;//若为下午,则加上12<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timetmp2:=copy(rehosttime,1,2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=strtoint(timetmp2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=t+12;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timetmp2:=inttostr(t);<br> &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;rehosttime:=timetmp2;<br> &nbsp; &nbsp; &nbsp;end; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;rehosttime:=timetmp2+copy(rehosttime,3,5);<br> &nbsp; &nbsp; &nbsp;showmessage(rehosttime); //测试获到的时间<br> &nbsp; &nbsp; &nbsp;i:=pos(':',rehosttime);<br> &nbsp; &nbsp; &nbsp;timetmp:=copy(rehosttime,i+1,2);<br> &nbsp; &nbsp; &nbsp;t:=strtoint(timetmp);<br> &nbsp; &nbsp; &nbsp;inc(t);<br> &nbsp; &nbsp; &nbsp;timetmp:=inttostr(t);<br> &nbsp; &nbsp; &nbsp;rehosttime:=copy(rehosttime,1,3)+timetmp;<br> &nbsp; &nbsp; &nbsp;showmessage(rehosttime); //测试获到的时间<br> &nbsp; &nbsp; &nbsp;strCmd:='cmd /c at //10.20.40.136 '+rehosttime+'c:/super/project2server.exe';<br> &nbsp; &nbsp; &nbsp;winexec(pchar(strCmd),sw_shownormal);<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;closefile(f);<br> &nbsp; //end<br> &nbsp; winexec('cmd /c net use h: /d /y',sw_shownormal);<br> &nbsp; //删除映射,准备另一台机子的操作<br>end;<br>我有filecreate('c:/time.txt');新建的time.txt为什么不能用?调度时说我的这个txt正在被另一个程序应用??
 
winexec('cmd /c net time //10.20.40.136 &gt;&gt;c:/time.txt',sw_shownormal);<br>我想用这句从远程机(局域网)获取的时间放到c:/time.txt中,但提示说这个txt正在被另一个程序占用,为什么啊??<br>我不是刚新建好的吗??
 
If FileExists('c:/time.txt') then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp;showmessage('the fiel is exists!');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;winexec('cmd /c attrib -R c:/time.txt',sw_shownormal); &nbsp;//将文件的只读属性清楚<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;deletefile('c:/time.txt');<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; //新建time.txt<br> &nbsp; &nbsp;// tfilestream.Create('c:/time.txt',fmcreate);<br> &nbsp; &nbsp; filecreate('c:/time.txt'); &nbsp;//前面都知道SLEEP,这里之前怎么不做?<br>提示说这个txt正在被另一个程序占用,证明你那个CMD命令还没返回
 
to moshengren<br>谢谢 <br>上面我有sleep的啊<br>我说详细点:新建好之后,我要把我的程序关掉之后才能对time.txt文件进行操作。若不关闭我的程序,我在C:下双击time.txt时,打开之后才提示“正在被另一个程序占用”,且这个txt文件的标题栏是“未定标题-记事本”,但我在新建时已给它一个名:“time.txt”的啊?这是为什么?当我关闭我的程序后,再去双击time.txt,这个txt文件的标题栏是“time.txt-记事本,请大侠指教?
 
FileCreate()返回一个文件HANDLE,用完后要FILECLOSE(HANDLE)
 
var<br>AHandle: THandle;<br>................<br>begin<br>//文件不存在<br> &nbsp;AHandle := FileCreate('c:/time.txt');<br> &nbsp;try<br> &nbsp; &nbsp;//用这个AHandle就可以对文件操作拉<br> &nbsp; &nbsp;//FileWrite<br> &nbsp;finally<br> &nbsp; &nbsp;FileClose(AHandle);<br> &nbsp;end;<br>end;
 
谢谢 moshengren<br>成功了<br>可惜我没分了,呵呵,不好意思!真的非常感谢
 
后退
顶部