简体   繁体   English

VBA使用cmd.exe合并当前目录中的文件

[英]VBA merge files in current dir using cmd.exe

I have an Excel sheet in one folder and try to merge certain files within this folder to one file using the following code: 我在一个文件夹中有一个Excel工作表,并尝试使用以下代码将此文件夹中的某些文件合并到一个文件中:

Private Sub CommandButton1_Click()
  Dim RET As Variant
  RET = Shell("cmd.exe copy  files1.txt + file2.txt out.txt", 0)
End Sub

As a return value for RET i get 1560. No error while debugging, but no "out.txt" either. 作为RET的返回值,我得到1560.调试时没有错误,但也没有“out.txt”。 What's wrong with my code? 我的代码出了什么问题? Thanks 谢谢

I think you miss the /C in the cmd arguments and the path. 我想你错过了cmd参数和路径中的/ C.

Private Sub CommandButton1_Click()
  Dim RET As Variant
  RET = Shell("cmd.exe /C copy  C:\Data\files1.txt + C:\Data\file2.txt C:\Data\out.txt", 0)
End Sub

The return value not equal 0 indicates the process is started (it is the actual process id) 返回值不等于0表示进程已启动(它是实际进程ID)

The VBA way; VBA的方式;

Function readFile(path) As String
    On Error GoTo ERR_IO
    Dim hF As Integer: hF = FreeFile
    Open path For Input As #hF
    readFile = Input$(LOF(hF), hF)
ERR_IO:
    Close #hF
End Function

Function writeFile(path, buffer) As Boolean
    On Error GoTo ERR_IO
    Dim hF As Integer: hF = FreeFile
    Open path For Output As #hF
    Print #hF, buffer
    writeFile = True
ERR_IO:
    Close #hF
End Function

Sub merge()
    Dim buffer As String
    buffer =          readFile("C:\xxx\files1.txt")
    buffer = buffer & readFile("C:\xxx\files2.txt")

    writeFile "c:\xxx\out.txt", buffer
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cmd.exe for循环:如何删除文件名中有空格的文件 - cmd.exe for loop: how to delete files with white space in filename 无法在cmd.exe上运行此程序 - cannot run this program on cmd.exe 在cmd.exe中以秒为单位获取文件的修改日期时间 - Getting modification date-time of a file with seconds in cmd.exe 将文件拖放到快捷方式中,以路径为参数调用 CMD.EXE - Drag and Drop a file into a shortcut to call a CMD.EXE with the path as a parameter 将VBA文件转换为.EXE - Converting VBA files to .EXE (傻瓜)在安装第三方模块之前更改cmd.exe中的目录会导致该模块存储在该目录中吗? - (Dumb noob) Will changing directory in cmd.exe prior to installing a third party module, cause that module to be stored in that directory? c#从文本文件中读取包含CMD.EXE comand的每一行并正确处理 - c# read each line containing CMD.EXE comand from text file and process it correctly VBScript-通过cmd.exe使用用户定义的名称和扩展名创建文件 - VBScript - Create file with user defined name and extension via cmd.exe Windows CMD:列出dir和subdir中没有给定扩展名的文件 - Windows CMD: List files in dir & subdir WITHOUT given extensions 在cmd中使用带有可运行jar的文件 - using files with runnable jars in cmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM