Page 1 of 1
Command prompt - combining files by name
Posted: 22 Mar 2021 04:25
by Iceman123
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
Re: Command prompt - combining files by name
Posted: 22 Mar 2021 06:53
by penpen
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
Re: Command prompt - combining files by name
Posted: 22 Mar 2021 07:39
by Squashman
Penpen I always thought if it was an NTFS drive, it would process them in ascii order.
Re: Command prompt - combining files by name
Posted: 22 Mar 2021 08:06
by miskox
Can this be used or something like that?
or maybe
Saso
Re: Command prompt - combining files by name
Posted: 23 Mar 2021 06:04
by penpen
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
Re: Command prompt - combining files by name
Posted: 23 Mar 2021 09:26
by miskox
@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
Re: Command prompt - combining files by name
Posted: 25 Mar 2021 10:14
by Iceman123
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?
Re: Command prompt - combining files by name
Posted: 25 Mar 2021 11:12
by Squashman
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?
Re: Command prompt - combining files by name
Posted: 26 Mar 2021 01:12
by miskox
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
Re: Command prompt - combining files by name
Posted: 26 Mar 2021 04:14
by miskox
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
Re: Command prompt - combining files by name
Posted: 26 Mar 2021 06:30
by penpen
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.
Re: Command prompt - combining files by name
Posted: 29 Mar 2021 06:35
by Iceman123
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.
Re: Command prompt - combining files by name
Posted: 29 Mar 2021 06:45
by Iceman123
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!
Re: Command prompt - combining files by name
Posted: 29 Mar 2021 07:08
by Squashman
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.