0

I have a batch that's merging some files into one file called all.txt. The batch code is OK, but what I want is to concatenate the file contents in numerical order. Currently the command will start merging files in this order.

1.txt
2.txt
3.txt
100,001.txt
3000.txt

What I want is merge 001.txt to 100,001.txt in numerical order:

 1.txt
 2.txt
 3.txt
 3000.txt
 100,001.txt

This is my current code:

(for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
    type "%%~a"
    echo(
  )
)>all.txt
mustaccio
  • 18,234
  • 16
  • 48
  • 57
GUESTOSS
  • 13
  • 2
  • _me_ thinks that 001 - 300 000 is not alphabetical, rather numerical order. – Gerhard May 31 '17 at 13:14
  • For your `dir` options choose to sort by name `('DIR/A-D-S-L/B/ON *.txt')`. Type and enter `DIR/?` into a Command prompt window for an explanation of the options available. – Compo May 31 '17 at 13:23

2 Answers2

1

Based on a 2014 answer by Magoo, Link pasted in a comment on his answer below. This takes the files and adds an number to them and writes it to a tempfile, the temp file is then sorted numerically and the amount is deducted and read in that order.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a IN ('dir /b /a-d *.txt') DO (
  set /a "seq=1000000000+%%~na"
  echo !seq!) 
)>"%temp%\tempfile"

for /f %%a IN ('sort "%temp%\tempfile"') DO (
 set /a "seq=%%a-1000000000"
 type !seq!.txt
 echo(
)>> all.log

This will write the files in sequence: 001.txt 002.txt 3000.txt 100001.txt

Please note the following! I named the output file all.log. The reason being that you are reading all text files and if you name it all.txt it will be read as well.

Also, if any other files exists in the directory which are not numerically named, it will attempt to do the same calculation. This calculation will only work on files that are numerically named.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • This is exactly what i want , thank you very much Gerhard !!! But please i want just to modify something : the files that i've are nammed like this : Gerhard.1.txt , Gerhard.2.txt ..... so with this code it can't works with files that begins with a name then a number .... because this code works only with numbers files ... is there any modification in the script BATCH ? (i don't want to rename files with date of creation ...) . thank's . – GUESTOSS Jun 01 '17 at 08:37
  • will all names be the same? only numbers change? so all files will be `filename001` `filename002` etc? or will the file alphabetical names change as well? – Gerhard Jun 01 '17 at 09:20
  • anyway, if the file names are going to be the same, only numbers changing, then you will strip away the name and leave only the numbers, do the same as above and then add the name again after the sort. If however it is not a static name with numeric changes, then it will be a little more complicated. – Gerhard Jun 01 '17 at 13:51
0

Are you sure you want alphabetically?

'dir /b /a-d /on *.txt'

should do that - but it's the default on NTFS so unless you're sourcing your files from a FAT drive where they'd be in random order, you should have the files alphabetically without the modification.

What do YOU mean by alphabetically?

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • think he means numerical order :) – Gerhard May 31 '17 at 13:26
  • ok, so when I re-read it, he says that he is getting file 100001 before 3000 which should be other way around. so this answer you posted back in 2014 was for a similar question. I will give you the honors. https://stackoverflow.com/questions/23544268/sorting-a-list-in-progressive-numeric-order – Gerhard May 31 '17 at 14:09
  • I edited his question to clarify, waiting on peer review. – Gerhard May 31 '17 at 14:19
  • @GUESTOSS So did you perhaps look at my answer yet? I took the link and modified the content of that script by Magoo which is working. – Gerhard Jun 01 '17 at 06:40
  • The Verified answer : This is exactly what i want , thank you very much Gerhard !!! But please i want just to modify something : the files that i've are nammed like this : Gerhard.1.txt , Gerhard.2.txt ..... so with this code it can't works with files that begins with a name then a number .... because this code works only with numbers files ... is there any modification in the script BATCH ? (i don't want to rename files with date of creation ...) . thank's . – GUESTOSS Jun 01 '17 at 08:38