Question

I am looking at trying to do a mass merge of txt files from different directories.

So i have

C:/data/Folder 1/Test.txt
c:/data/Folder 2/Test.txt
c:/data/folder2/testfolder/test.txt

Now my problem is being able to move them all into one directory while renaming them, as unfortunately they all have the same name and then merge them together.

From what i can work out so far,

I can use the following to move them all to one directory

For /r c:/data %F in (*.txt) do copy %f C:/exporteddata 

which will take all the text files from the data directory and put them into the exporteddata directory but this only works if each file name is unique which in my case they are all the same name

I know once i have all the .txt file in the same directory i can use the

copy *.txt output.txt 

running this from within the exported data directory

But what i cant work out is how to put in a rename which will give all the txt files a unique ID before copying them.

Any help would be great.

Many Thanks

Was it helpful?

Solution

You can actually copy all files to one without moving to a new location. You can choose to copy and concatenate all files by removin echo before desired command. I set to copy all files to new directory "AllFilesFolder". Please place the script in the base dir. Also - to avoid name colision (file with same name copied to AllFilesFolder), an id is added to filenames whe copiing. id is incremented for each file.

@echo off
setlocal enabledelayedexpansion

set id=0
if not exist AllFilesFolder mkdir AllFilesFolder

for /f %%a in ('dir /B /S /A-D *.txt') do (
    echo copy "%%a" "AllFilesFolder\%%~dpna_!id!%%~xa"
    echo type "%%a" >> allFiles.txt
    set /a id+=1
)

OTHER TIPS

This cmd prompt line will merge your files.

For /r "c:\data" %f in (*.txt) do type "%f" >> C:\exporteddata 

Double % to %% for use in a batch file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top