[英]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.