比如filename='c:\delphi\bin\abc.exe';怎样实现将文件夹bin剪切至‘c:\delphi\heart\'文件夹中,bin文件夹中只有一个文件abc.exe

解决方案 »

  1.   

    The MoveFile function renames an existing file or a directory (including all its children). BOOL MoveFile(    LPCTSTR lpExistingFileName, // address of name of the existing file  
        LPCTSTR lpNewFileName  // address of new name for the file 
       );
     ParameterslpExistingFileNamePoints to a null-terminated string that names an existing file or directory. lpNewFileNamePoints 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.  Return ValuesIf the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError. ResThe 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.
      

  2.   

    MoveFile
    或者使用ShFileOperation:
    给你一个例子:
    deltree:
    procedure TForm1.Button4Click(Sender: TObject);
    var
      shfo :TSHFileOpStruct;
      from :string;
      pstr :array [0..MAX_PATH-1] of char;
    begin
      FillChar(pstr,MAX_PATH,#0);
      from := 'd:\tmp' ;
      strpcopy(pstr,from);
      with shfo do
      begin
        wnd   := Handle;
        wFunc := FO_DELETE;//(删除,fo_copy复制,fo_move移动)
        pfrom := pstr;
        pto   := nil;
        fFlags := FOF_ALLOWUNDO or FOF_SIMPLEPROGRESS;
      end;
       if ShFileOperation(shfo) <> 0 then
         ShowMessage('Failed')
       else
         ShowMessage('ok');
    end;
      

  3.   

    楼上的老兄,我从filepath怎样得到from呢(即我所要的文件夹)谢谢
      

  4.   

    ShFileOperation只有一个参数是LPSHFILEOPSTRUCT型的相当于delphi中的TSHFileOpStruct;
    c语言定义为:
    typedef struct _SHFILEOPSTRUCT{ 
        HWND         hwnd; 
        UINT         wFunc; 
        LPCSTR       pFrom; 
        LPCSTR       pTo; 
        FILEOP_FLAGS fFlags; 
        BOOL         fAnyOperationsAborted; 
        LPVOID       hNameMappings; 
        LPCSTR       lpszProgressTitle; 
    } SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT; 相应的pascal就是:
    type 
        _SHFILEOPSTRUCTA = packed record
        Wnd: HWND;
        wFunc: UINT;
        pFrom: PAnsiChar;
        pTo: PAnsiChar;
        fFlags: FILEOP_FLAGS;
        fAnyOperationsAborted: BOOL;
        hNameMappings: Pointer;
        lpszProgressTitle: PAnsiChar; { only used if FOF_SIMPLEPROGRESS }
      end; 
    hwnd:用来显示操作状态的对话框句柄。 例中是form1的句柄
    wFunc:执行的操作。可以是以下各值:(例中是FO_COPY)
            FO_COPY:拷贝pfrom域中指定的(目录,例中是'c:\a')到pto中指定的位置(例中为'c:\b') 
            FO_DELET:删除pfrom中指定的文件.   (pTo不用) 
            FO_MOVE:移动PFrom中指定的文件到pto中指定的位置。  
            FO_RENAME:给PFrom中指定的文件改名。
    pFrom:指定一个或多个源文件名的缓冲区地址。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'\0')来结束。
    pTo:目标文件或目录名缓冲区地址。 如果fFlags域指定FOF_MULTIDESTFILES,缓冲区可以包含多个目标文件名。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'\0')
    fFlags :控制操作的标志,可以是以下各值组合:
            FOF_ALLOWUNDO:保留Undo信息, 如果pFrom没有包含全的绝对的路径或文件名此值忽略。
            FOF_CONFIRMMOUSE:没有实现.
            FOF_FILESONLY:只有文件名使用通配符时(*.*)才对文件操作。
            FOF_MULTIDESTFILES:  pTo域指一定了多个目标文件.(一个对就一个源文件) 而不是指定一个目录来存放所有源文件  
            FOF_NOCONFIRMATION:所有显示的对话框全部选择yes to all
            FOF_NOCONFIRMMKDIR: 如果需要创建一个新目录不确认。
            FOF_NOCOPYSECURITYATTRIBS:  4.71. Microsoft&reg; Windows NT&reg; only. 安全属性不复制.
            FOF_NOERRORUI:发生错误时不提供用户接口。
            FOF_RENAMEONCOLLISION:  move,copy,rename操作时如目标文件存在,给操作的文件另起一个名字。
            FOF_SILENT:不显示进度对话框
            FOF_SIMPLEPROGRESS:显示进度对话框但不显示文件名。
            FOF_WANTMAPPINGHANDLE:如果指定了FOF_RENAMEONCOLLISION 当任何文件改名时将填写hNameMappings 域
    fAnyOperationsAborted:当用户在完成前取消任何文件操作时赋值TRUE,否则FALSE.
    hNameMappings:一个包含SHNAMEMAPPING结构数组的文件名映射对象句柄. 每一个(SHNAMEMAPPING)结构包括一个旧的或新的目录名为了每一个移动的复制的改名的文件。这个域仅在fFlags域包括FOF_WANTMAPPINGHANDLES标志时使用。句柄必须使用SHFreeNameMappings来释放(用完后)
    lpszProgressTitle :进程对话框的标题串地址。仅在fFlags中包括FOF_SIMPLEPROGRESS标志时使用。如果pFrom和pTo不是一个绝对目录时,当前目录从全局当前盘符和当前目录中取得,同时目录设置由GetCurrentDirectory 和SetCurrentDirectory 函数维护.另外附上:
    ExtractFileName//获得文件名
    ExtractFilePath//获得文件路径
    ExtractFileExt//获得文件扩展名
      

  5.   

    如果你使用了MoveFile就不用这么麻烦了。
    但上面的用法能让你获得更高级的控制。
    MoveFile(源文件路径和文件名,目的文件路径和文件名);
      

  6.   

    比如filename='c:\delphi\bin\abc.exe';怎样实现将文件夹bin剪切至‘c:\delphi\heart\'文件夹中,hearth文件夹没有则新建,bin文件夹中只有一个文件abc.exe急啊,可不可以帮我写段代码先谢了