Thursday, April 22, 2021

Batch script commands to combine text(.csv) files 使用命令提示字元合併多個文字檔案

Code Preview

copy *.csv all.csv #For flatten file structure 單一資料夾檔案合併

for /R %f in (*.txt) do type "%f" >> all.txt #For merging sub-folder files 多層資料夾檔案合併

There are lots of use cases that we have to handle numbers of fragment files . One of the the common operation is to combine text files into a single file. You may find section below helpful if you have to merge multiple text files. 

Scenario 

Case A : Combine csv data feed into single Excel sheet for filtering and analysis.

Case B : Collect all intranet system log for investigation .

The task is easy but can be time consuming , and the task actually can be done using command line within 3s. There are mainly two ways to combine text files in command line.

Method 1 
The simple way is using copy command for NO sub-directory file case   

Original file structure
Flatten file structure


The command 

copy *.csv all.csv

Result 

Result of command : copy *.csv all.csv






The Command above mainly choose the files to merge in the folder by selecting file extension .

It tells window to merge all .csv file in folder "C:/Users/Local User/Documents" into a file named as "all.csv".

Alternatively, you may also use the wildcard symbol * in file name like below :

The command  

copy b*.csv b_all.csv

Result 

Result of command : copy b*.csv b_all.csv

The command merge .csv files with prefix letter "b" in filename into file named as "b_all.csv".


Method 2
The customized way is using for /R to handle recursive selection in sub-directory   

Folder with sub-folders
There are certain text log files in the sub-folders , those files need to be combined as well. 
Hence the folder has to be searched recursively in order to find out all the files inside sub-folders.

The command 

for /R %f in (*.txt) do type "%f" >> allLog.txt

Result 







All .txt file inside the folder "C:/Users/Local User/Documents" and its sub-folders will be merged into a file named as "allLog.txt".

Summary
- Text files could be merged using command "copy" or "for /R".
- The first method only apply to situation with flatten file structure while the second could      be applied to sub-folder structure.
- Wildcard symbol * could be used for filename or file extension selection.

No comments:

Post a Comment

Something about Renpy For loop error : expected statement.

 It takes me over hour to debug. The simple fact is that under label, we cannot use For loop. One while is valid to be used under label. To ...