怎么拷贝程序所在目录里面的一个目录包含所有文件到另外一个地方?详细请进!(20分)

  • 主题发起人 sohuandsina
  • 开始时间
S

sohuandsina

Unregistered / Unconfirmed
GUEST, unregistred user!
[red]我想拷贝本程序所在目录底下的一个目录的所有文件到同样目录下的一个新建立
的目录里面[/red]。
目录层次是这样的:
....程序所在的目录->A目录->下面包含的文件
达到的目的。
....程序所在的目录->B目录->A目录下的文件
1、怎么知道程序所在的目录?
2、怎么新建b目录?
3、怎么拷贝a下面的文件到b下面?
 
1.var AppDir: String;
AppDir := ExtractFileDir(Application.ExeName);
2.CreateDir('d:/mydir');
3.copyfile(....);
 
用TShFileOperation
论坛中有好多例子,搜索一下
 
var
path:String;
path=ExtractFilePath(Application.ExeName); //就是你程序当前目录。
ShellExecute(handle,nil,pchar('xcopy '+path+'b '+'path'+'a /t'),nil,nil,sw_shownormal); //建立a目录
ShellExecute(handle,nil,pchar('xcopy '+path+'b '+'path'+'a /s'),nil,nil,sw_shownormal); //拷贝b下所有目录到a
 
var AppDir: String;
begin
AppDir:= ExtractFileDir(Application.ExeName);
label1.Caption:=AppDir;
CreateDir(appdir+'/bak');
copyfile(appdir+'/data/*.*',appdir+'/bak',false);
end;
我这样写的为什么提示是:tstring 和pcchar 不同类型啊?我该怎么改?
 
你的copyfile中的参数类型应为pchar
 
uses shellapi;

var
F:TShFileOpStruct;
FillMemory(@F,sizeof(F),0);
F.wnd:=Handle;
F.wFunc:=FO_COPY;
F.pFrom:='a'#0#0;
F.pTo:='b'#0#0;
SHFileOperation(F);
 
sohuandsina,你别看他写的简单就用它,其实,即使你转转成PChar也是错误的
CopyFile只能一个文件一个文件拷贝,而且第一个参数必须是已经存在的一个文件名
第二个参数是目的文件名字,一次只能拷贝一个文件
 
TO:pipi.,大侠!
那我应该怎么解决,我真的不知道怎么简单的拷贝真个文件夹的。能告诉我吗?谢谢了。
 
在 windows 系统中,使用Shell Dos命令的方法是不行的。你可以使用FileSystemObject
对象操作文件系统,该对象的使用可以用CScript,WScript到Microsoft站点中查找,或者找VBScript编写
网站方面的书。Com 对象很好用的。
 
TO:大家。
我还是没有解决问题。求救!~``
 
最上面我写的不行吗
 
你可以Shell运行以下程序,也可以翻译成Delphi
'这个程序复制一个目录到目标目录,用于定时系统备份任务
'它的特点是对于不变的文件不复制,对变的都复制。源文件删除,目标文件也删除
'它必须有两个参数 源目录 目标目录
' CopyRight by Pan maolin At 2002.01
' Version 1

Option Explicit

'检查是新文件或者已经被修改
'返回 n 是新建 m 是修改
Function FCheckModify(ff,ffiles)
dim bf,f
bf = "n"
for each f in ffiles
if (ff.name = f.name) then
if (ff.DateLastModified=f.DateLastModified) then
bf = " "
else
bf = "m"
end if
exit for
end if
next
FCheckModify = bf
end function

'检查文件在文件夹中
Function FinFiles(ff,ffiles)
dim bf,f
bf = false
for each f in ffiles
if (ff.name = f.name) then
bf = true
exit for
end if
next
FinFiles = bf
end function

'同步目的目录与源中的文件
sub synFiles(source,destination)
dim fso,f,fsfiles,fdfiles
dim bUpdate
dim iVal

On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
if not(fso.FolderExists(source)) then
exit sub
end if
'如果没有目标目录,创建它
if not(fso.FolderExists(destination)) then
Set f = fso.CreateFolder(destination)
end if
'创建文件集合
Set f = fso.GetFolder(source)
Set fsfiles = f.files
Set f = fso.GetFolder(destination)
Set fdfiles = f.files
'删除目的中文件在源中不存在文件
for each f in fdfiles
if not FinFiles(f,fsfiles) then
fso.DeleteFile(destination+"/"+f.name)
end if
next
'更新文件
For Each f In fsfiles
bUpdate = FCheckModify(f,fdfiles)
if bUpdate <> " " then
Err.Clear '清除错误。
fso.CopyFile source+"/"+f.name,destination+"/"+f.name,true
If Err.Number<>0 then
iVal = MsgBox(Err.Description,vbExclamation+vbOKOnly,"文件复制")
writelog(CDate(Now)&" 无法复制文件"&source&"/"&f.name&"到"&destination&"/"&f.name)
End If
End If
next
end sub

'同步目的目录
sub synForders(source,destination)
'同步文件
call synfiles(source,destination)
'处理目录
dim fso,f,fsfiles,fdfiles
Set fso = CreateObject("Scripting.FileSystemObject")
if not(fso.FolderExists(source)) then
exit sub
end if
'如果没有目标目录,创建它
if not(fso.FolderExists(destination)) then
Set f = fso.CreateFolder(destination)
end if
'创建目录集合
Set f = fso.GetFolder(source)
Set fsfiles = f.SubFolders
Set f = fso.GetFolder(destination)
Set fdfiles = f.SubFolders
'删除目的中在源中不存在目录
for each f in fdfiles
if not FinFiles(f,fsfiles) then
fso.DeleteFolder destination+"/"+f.name , true
end if
next
'更新目录
for each f in fsfiles
call synForders(source+"/"+f.name,destination+"/"+f.name)
next
end sub

'日志处理程序
sub writelog(logstr)
dim logfile
dim fso,f
logfile = WScript.ScriptName
if instr(logfile,".") <> 0 then
logfile = left(logfile, instr(logfile,".")-1)
end if
logfile = logfile & "_err.log"
Set fso = CreateObject("Scripting.FileSystemObject")
set f = fso_OpenTextFile(logfile, 8 , True)
f.writeline(logstr)
f.close
end sub


'主程序
sub main
dim args,i
dim source,destination
dim fso,f

set args = WScript.Arguments
if args.count < 2 then
WScript.Echo "没有足够的参数"+ vbCRLF + "参数是 源目录 目标目录"
exit sub
end if
source = args(0)
destination = args(1)

writelog(CDate(Now)&" 开始同步 "&source&" 到 "&destination)

Set fso = CreateObject("Scripting.FileSystemObject")
if not(fso.FolderExists(source)) then
WScript.Echo "没有正确源目录"
exit sub
end if
'如果没有目标目录,创建它
if not(fso.FolderExists(destination)) then
Set f = fso.CreateFolder(destination)
end if
if not(fso.FolderExists(destination)) then
WScript.Echo "没有正确目标目录"
exit sub
end if

call synForders(source,destination)
end sub

main
writelog(CDate(Now)&" ok")
 
T0:pipi大侠.
你写的我看不懂,真的。你能看看能不能帮我完成象我那种方法的目的。当然方法是你的那种。
var AppDir: String;
begin
AppDir:= ExtractFileDir(Application.ExeName);
label1.Caption:=AppDir;
CreateDir(appdir+'/bak');
copyfile(appdir+'/data/*.*',appdir+'/bak',false);
end;
因为,我的路径是根据程序的路径来的。不是固定的。我搞不好。你能不能按照我的意思。
用你的方法给我重写一下。真的谢谢了。
 
COPY文件没什么大不了的,就是一个CopyFile就可以了
而没有目录要创建新的目录就需要多写几行程序了。

大体的思路是,先判断有无这个目录,没有,就先建立此目录,
可把这个写成一个函数,来完成工作。
如要建立多级目录,就是是让这个函数进行递归调用就是,
这样就可完成一个多级目录的创建,
创建完目录,就可以很顺利地COPY文件了。

对了,还有一个很简单的问题,就是你可以使用XCOPY来完成这个工作,
XCOPY可以在你没有此目录时,它自己帮你建立这一系列的路径,
你的工作,就是把参数写完整了。
你找一个XCOPY的文件来,让它与你的软件一起发行,
你要作的就是把它当作一个外部文件来调用。
 
多人接受答案了。
 
顶部