Command prompt - combining files by name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Iceman123
Posts: 4
Joined: 22 Mar 2021 04:10

Command prompt - combining files by name

#1 Post by Iceman123 » 22 Mar 2021 04:25

Hi all,

I'm very new to all of this, so I'm probably asking the very basics :)

Using the Command Prompt, I want to combine a number of .txt files, ordered by name. For example:

- 0001.txt
- 0002.txt
- 0003.txt
- 0004.txt
- 0005.txt

I use the following command: Type 0* >> test.log (for my purpose it needs to be a .log file). Ideally, I would get one test.log file with first the content of 0001.txt, followed by 0002.txt, etc.

However, when doing this, it's not first taking 0001.txt, followed by 0002.txt, etc. For some reason it's always starting with 0003.txt.

Why is this the case? Based on which criteria is it taking the 0003.txt instead of 0001.txt? I presume it's because of the content of file 0003.txt? Should I perhaps use a different command?

I hope I made myself clear. Thanks in advance for your help!

Regards,

Bobby

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Command prompt - combining files by name

#2 Post by penpen » 22 Mar 2021 06:53

The order is based on a hashsum. Win10 (as XP and NT before) is able to use multiple Hashsums (based on filename, creation date, ...), that even can change from directory to directory; so you better don't expect any order.
You might use the dir-command to list all files you need and pipe the output to the sort-command to establish an alphanumeric order. The use a for/f-comand to loop through all files (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir "0*" ^| sort') do (
	>>"test.log" type "%%~a"
)
goto :eof
penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Command prompt - combining files by name

#3 Post by Squashman » 22 Mar 2021 07:39

Penpen I always thought if it was an NTFS drive, it would process them in ascii order.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Command prompt - combining files by name

#4 Post by miskox » 22 Mar 2021 08:06

Can this be used or something like that?

Code: Select all

copy *.txt outfile.out
or maybe

Code: Select all

copy /b *.txt outfile.out
Saso

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Command prompt - combining files by name

#5 Post by penpen » 23 Mar 2021 06:04

Squashman wrote:
22 Mar 2021 07:39
Penpen I always thought if it was an NTFS drive, it would process them in ascii order.
NTFS versions prior to 3.0 only use one index for filenames, so there you would be correct.
All versions since then (=3.0+) also use various other indexes (security, quota, ... and a hashsum based on all other indexes).
Technically Windows (XP+) may use any index if it likes, so my above statement was also (slightly) wrong.
However the most likely one is the hash value index (which in case your folder only contains your files, probably will result in a filelist ordered roughly alphabetically).
Beside that, Windows may or may not change the file system driver (accessed by FindFirst, FidnNext, FindClose), which ends up in different sort orders.
Just note that no behaviour (under NTFS 3.0+) is contractual, so the order might even change between two runs.


miskox wrote:
22 Mar 2021 08:06
Can this be used or something like that?
I don't know whether the copy-command orders the filelist alphabetically, or not.
If i had to guess, i would doubt that and would expect the same sort order the type-command uses.


penpen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Command prompt - combining files by name

#6 Post by miskox » 23 Mar 2021 09:26

@penpen: I've used this command many times - always as expected (in the correct sequential order). Of course it is up to the OP to decide.

Saso

Iceman123
Posts: 4
Joined: 22 Mar 2021 04:10

Re: Command prompt - combining files by name

#7 Post by Iceman123 » 25 Mar 2021 10:14

Hi all,

Thanks for all the help and apologies for my late reply. Your help really is appreciated.

@miskox I've tried your command but unfortunately, I get the same order as with my type command.. I did rename your .out file to a .log file, but I doubt that is the reason for it

@penpen Thanks. I've copied your command but it only gives me an empty test.log file and it opens the first .txt file (by name). I do get some sort of strange error message (something like: "%%a unexpected at this moment") though, so it's probably related to that?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Command prompt - combining files by name

#8 Post by Squashman » 25 Mar 2021 11:12

Iceman123 wrote:
25 Mar 2021 10:14
I do get some sort of strange error message (something like: "%%a unexpected at this moment") though, so it's probably related to that?
Were you running the code from the cmd prompt or from a .bat file?

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Command prompt - combining files by name

#9 Post by miskox » 26 Mar 2021 01:12

Iceman123 wrote:
25 Mar 2021 10:14
@miskox I've tried your command but unfortunately, I get the same order as with my type command.. I did rename your .out file to a .log file, but I doubt that is the reason for it
Interesting. So I was lucky because I generated all my files in a sequence - this must be the reason that everything has worked for me.

Saso

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Command prompt - combining files by name

#10 Post by miskox » 26 Mar 2021 04:14

I did a test (Windows 10):

Sequence_test.cmd contains:

Code: Select all

@echo off
title This will take a while...
echo 3 >003.txt
timeout /t 120
echo 4 >004.txt
timeout /t 120
echo 5 >005.txt
timeout /t 120
echo 1 >001.txt
timeout /t 120
echo 2 >002.txt
timeout /t 120

copy /b 0*.txt 0_b.log
copy    0*.txt 0__.log
copy /b  *.txt __b.log
copy     *.txt ___.log

When executed:

Code: Select all

c:\>sequence_test.cmd

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...

Waiting for   0 seconds, press a key to continue ...
001.txt
002.txt
003.txt
004.txt
005.txt
        1 file(s) copied.
001.txt
002.txt
003.txt
004.txt
005.txt
        1 file(s) copied.
001.txt
002.txt
003.txt
004.txt
005.txt
        1 file(s) copied.
001.txt
002.txt
003.txt
004.txt
005.txt
        1 file(s) copied.
c:\>
FIles contain what is shown above (of course those commands without /B have EOF at the end of the file). We can see that files were added in the correct order.

120 seconds waiting period is in there so the hash (hash also based on time? - penpen help) would be different (my guess). No luck.

Saso

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Command prompt - combining files by name

#11 Post by penpen » 26 Mar 2021 06:30

Well, i only can say what Microsoft has published within their documentation and have no additional insights into their exact code of (course).
Therefore i can't say how you reliably cause a non alphabetical order under NTFS 3.0+.
Maybe to distort the order, you could use some files with short names different from long names,
some files with only long names and some files with only short names - but that's only a guess.
Or you could add files or parent directories from some different users,
some users with and others without quota informations and so on.

Beside that, Iceman123 didn't claim his files to be stored in a NTFS 3.0+ formated disk.
So we even can't be sure that this is an example.

Iceman123
Posts: 4
Joined: 22 Mar 2021 04:10

Re: Command prompt - combining files by name

#12 Post by Iceman123 » 29 Mar 2021 06:35

Squashman wrote:
25 Mar 2021 11:12
Iceman123 wrote:
25 Mar 2021 10:14
I do get some sort of strange error message (something like: "%%a unexpected at this moment") though, so it's probably related to that?
Were you running the code from the cmd prompt or from a .bat file?
Hi,

I was running the code from the cmd prompt.

Iceman123
Posts: 4
Joined: 22 Mar 2021 04:10

Re: Command prompt - combining files by name

#13 Post by Iceman123 » 29 Mar 2021 06:45

penpen wrote:
26 Mar 2021 06:30
Well, i only can say what Microsoft has published within their documentation and have no additional insights into their exact code of (course).
Therefore i can't say how you reliably cause a non alphabetical order under NTFS 3.0+.
Maybe to distort the order, you could use some files with short names different from long names,
some files with only long names and some files with only short names - but that's only a guess.
Or you could add files or parent directories from some different users,
some users with and others without quota informations and so on.

Beside that, Iceman123 didn't claim his files to be stored in a NTFS 3.0+ formated disk.
So we even can't be sure that this is an example.
Thanks for your help anyway!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Command prompt - combining files by name

#14 Post by Squashman » 29 Mar 2021 07:08

Iceman123 wrote:
29 Mar 2021 06:35
Squashman wrote:
25 Mar 2021 11:12
Iceman123 wrote:
25 Mar 2021 10:14
I do get some sort of strange error message (something like: "%%a unexpected at this moment") though, so it's probably related to that?
Were you running the code from the cmd prompt or from a .bat file?
Hi,

I was running the code from the cmd prompt.
From the help file of the FOR command.

Code: Select all

FOR %variable IN (set) DO command [command-parameters]

To use the FOR command in a batch program, specify %%variable instead
of %variable.  Variable names are case sensitive, so %i is different
from %I.

Post Reply