Algorithm to map integers so DOS processes them in proper order

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
If I issue this: copy *.tp /b [destination file]

and the source files are sequenced by integers, DOS processes them like this:

0 1 10 11 12 13 14 15 16 17 18 19 2 20 21 etc.

My source files have names including the integers, e.g. timeshift(27).tp

My destination file has the source files out of sequence unless I replace the subscripts.

I want to write a function that will take the embedded integer (i.e. subscript) for all the files and replace them with something that makes DOS process the files in the same order as the integers (i.e. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14... etc.)

Once I process the files into the replacements, the DOS command above will create a destination file that concatenates all the source file in the proper order.

How would you map the integers into those things?

Something that would work for zero to ten thousand would be sufficient, but without limit would be best, of course.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,277
125
106
So, to get this straight, You have files that looks something like this

a(4).x
b(1).x
c(2).x

and a batch script that goes through each of these files, one by one, and processes them in some way.

However, the order, a(4), b(1), c(2), is undesirable. You actually want b(1), c(2), a(4). Correct?

The easiest thing you could do is simply rename the files to something like this

004_a.x
001_b.x
002_c.x

Writing a program that does that should be pretty simple. Then the batch file you want to use wouldn't have to change at all.
 
  • Like
Reactions: Ken g6

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> Something that would work for zero to ten thousand would be sufficient, but without limit would be best, of course.

You need to decide on the maximum in advance, to set the number of leading zeroes.

If you have one set of files with 3 digit numbers 000 - 999 and another set with 4 digit numbers 1000 - 9999, (DOS / Windows / most any other OS) will not maintain the order that you want.
 

mv2devnull

Golden Member
Apr 13, 2010
1,498
144
106
The problem in
Code:
1
11
9
is that they are in lexicographic order. All "words" that begin with character '1' are "before" words that begin with '9'. You want numeric order, where numeric value 9 is less than value 11.

You could rename the files, as stated already. 0<1 in "09" vs "10".

OR

Sort by numeric values. Take the filenames, sort, and then construct the concatenation command. However, is there a limit on how many characters/arguments the CMD/copy can have?

SO

Iterate over a range of numbers. For each number, if a file with that value exists, append it to the destination. Windows 10 anniversary did made bash and some GNU tools available. If you don't know how to/can't loop in DOS/CMD/batch (I don't), then check the bash.
 

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
So, to get this straight, You have files that looks something like this

a(4).x
b(1).x
c(2).x

and a batch script that goes through each of these files, one by one, and processes them in some way.

However, the order, a(4), b(1), c(2), is undesirable. You actually want b(1), c(2), a(4). Correct?

The easiest thing you could do is simply rename the files to something like this

004_a.x
001_b.x
002_c.x

Writing a program that does that should be pretty simple. Then the batch file you want to use wouldn't have to change at all.
To clarify:

The files are all like this:

timeshift(0).tp
timeshift(1).tp
timeshift(2).tp
etc.

The only thing that differentiates the file names is the integer subscript. DOS (and a freeware program I have used, HDTVtoMPEG2) don't order the files as the integers are ordered.

Yeah, maybe I should establish a ceiling. 5 digits should be sufficient. IOW, 99999 is more than I'll need.
> Something that would work for zero to ten thousand would be sufficient, but without limit would be best, of course.

You need to decide on the maximum in advance, to set the number of leading zeroes.

If you have one set of files with 3 digit numbers 000 - 999 and another set with 4 digit numbers 1000 - 9999, (DOS / Windows / most any other OS) will not maintain the order that you want.
So, I suppose I process the files, renaming them like this:

timeshift(00000).tp
timeshift(00001).tp
timeshift(00002).tp
.
.
.
timeshift(00009).tp
timeshift(00010).tp
timeshift(00011).tp
etc.

Correct?
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,277
125
106
Correct, that should work.

I'm not sure what you are doing with these files, but you may look into alternatives to HDTVtoMPEG2. It is a pretty old program and MPEG2 is a pretty old standard. A lot of these things are built on top of opensource software anyways.

I would suggest starting at doom9 and moving from there. They should have lots of tips and tricks on what the latest and greatest transcoders and transcoding workflows are.
 

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
Correct, that should work.

I'm not sure what you are doing with these files
, but you may look into alternatives to HDTVtoMPEG2. It is a pretty old program and MPEG2 is a pretty old standard. A lot of these things are built on top of opensource software anyways.

I would suggest starting at doom9 and moving from there. They should have lots of tips and tricks on what the latest and greatest transcoders and transcoding workflows are.
Here's what I'm doing:

Each of these files represents 20 seconds of an ongoing DVR (timeshift) of HDTV produced by my midtower PC. I usually simply erase a recording after I watch whatever portions interest me. But occasionally there's something I want to preserve for later. The HTPC application of the capture card can play that but adds more files as it plays. So, I prefer to take the files I want to preserve and convert them into a single transport stream file which my HTPC application will play without altering it. That's where HDTVtoMPEG2 comes in. However, I discovered a few days ago that HDTVtoMPEG2 has a bug where it reports a spurious error when dealing with external HDs. There's a goofy workaround (seed with a file from an internal HD, then erase that file after adding the ones from the external HD), but it's just easier to concatenate the files using the DOS copy command like this:

copy *.tp /b [destination file]

I'll check out Doom9, as you suggest, I dimly remember having seen that site some years ago and the home page looking like a video game/pinball machine! :)

I think I can write a single function in Foxpro that will let me select a work directory, specify the files I want to process, specify a file name for the resulting concatenation transport stream file, choose a destination folder, click OK.
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,250
3,845
75
Hm, now I'm thinking something along the lines of:

Code:
FOR /l %i in ([first number],1,[last number]) DO type timeshift(%i).tp >> output.tp
 

quikah

Diamond Member
Apr 7, 2003
4,073
652
126
PowerShell:

$files = get-childitem timeshift* | Sort-Object { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }

foreach ($file in $files) { Get-Content $file >> timeshift-concat.tp }
 

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
Hm, now I'm thinking something along the lines of:

Code:
FOR /l %i in ([first number],1,[last number]) DO type timeshift(%i).tp >> output.tp
This code is to be executed in what environment? :confused:
 

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
PowerShell:

$files = get-childitem timeshift* | Sort-Object { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }

foreach ($file in $files) { Get-Content $file >> timeshift-concat.tp }
OMG, I have to try this too. Thank you!
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,250
3,845
75
This code is to be executed in what environment? :confused:
At a command prompt. If you put it in a batch file, be sure to double the number of "%"s before the "i"s. And, of course, replace "[first number]" and "[last number]" with numbers.
 

Muse

Lifer
Jul 11, 2001
37,502
8,098
136
At a command prompt. If you put it in a batch file, be sure to double the number of "%"s before the "i"s. And, of course, replace "[first number]" and "[last number]" with numbers.
Thanks. I'll give it a shot.