Merging Two Text Files into One CSV File

2020-04-13 20:15发布

问题:

I'm working with a windows batch command to create a list of filepaths and filenames (without the ext) for processing and archival. I need to make a CSV file that will contain the path to the file and the filename.

I was able to use the 'DIR /A-D-S /D /S' command to output the list with the file paths, which is filelistA.txt. Then I use a vbscript (makelistB.vbs) to strip the path and extension and save that as filelistB.txt. I need to merge the two files row for row, putting the comma separator in between and that's where I need some sort of VBscript.

filelistA.txt looks like:
C:\Data\Clients\COLD\AC3060P.txt
C:\Data\Clients\COLD\AC3090P.txt
C:\Data\Clients\COLD\AC3100P.txt
C:\Data\Clients\COLD\AC3150P.txt
C:\Data\Clients\COLD\AC3200P.txt
C:\Data\Clients\COLD\AC3600P.txt
C:\Data\Clients\COLD\AC3652P.txt
C:\Data\Clients\COLD\AC5715P.txt
C:\Data\Clients\COLD\AC5720P.txt
C:\Data\Clients\COLD\AC5725P.txt

filelistB.txt looks like:
AC3060P
AC3090P
AC3100P
AC3150P
AC3200P
AC3600P
AC3652P
AC5715P
AC5720P
AC5725P

I want to make FileListCSV.txt, that looks like this:
C:\Data\Clients\FWBT\COLD\AC3060P.txt,AC3060P
C:\Data\Clients\FWBT\COLD\AC3090P.txt,AC3090P
C:\Data\Clients\FWBT\COLD\AC3100P.txt,AC3100P
C:\Data\Clients\FWBT\COLD\AC3150P.txt,AC3150P
C:\Data\Clients\FWBT\COLD\AC3200P.txt,AC3200P
C:\Data\Clients\FWBT\COLD\AC3600P.txt,AC3600P
C:\Data\Clients\FWBT\COLD\AC3652P.txt,AC3652P
C:\Data\Clients\FWBT\COLD\AC5715P.txt,AC5715P
C:\Data\Clients\FWBT\COLD\AC5720P.txt,AC5720P
C:\Data\Clients\FWBT\COLD\AC5725P.txt,AC5725P

I'm also open to using SED for windows if that can do all of this in one shot. However, I would imagine this should be something that can be whipped up in VBscript in a few minutes.

回答1:

This Windows batch file will do what you want without the need for the intermediate files.

@ECHO OFF

FOR %%i IN (*.txt) DO ECHO %%~fi,%%~ni

You can get the output of this batch into a text file by redirecting the output like this:

MyBatch.cmd>>Output.txt


回答2:

This could be a job for the SQLite shell.

C:\Temp> sqlite3.exe
create table paths(path text);
create table filenames(fname text);
.import fileListA.txt paths
.import fileListB.txt filenames
.separator ,
.output FileListCSV.txt
select * from paths p join filenames f on f.rowid = p.rowid;
.q

The SQLite shell is a single executable that will either create a persistent SQLite database in the form of a file, or create a database in memory (when, like here, without any argument in the command line).



回答3:

This can be done pretty fast in EasyMorph using Append transformation in "Append columns" mode.