RayRay
RayRay

Reputation: 1

Using CMD to produce txt file list of all files in a directory but Sequentially

I have found that the following puts the file names in a text document but with no identifiable sorting.

dir /b "*.mp4" > files
for "tokens=*" %%i in (files) do move "%%i" !random!.mp4 

I would want them sequential from a list such as this:

i_ch3_main_20230401030500_20230401030600.mp4
i_ch3_main_20230401004900_20230401005000.mp4
i_ch3_main_20230401023700_20230401023800.mp4
i_ch3_main_20230401050900_20230401051000.mp4
i_ch3_main_20230401040900_20230401041000.mp4  

So the second one on this list would actually go first, and the 2nd to last would be last etc. (these are 2023, April, 1st, and then military time to explain how it would be sequentially.

My problem is I don't want 5:09 AM showing before 4:09 AM etc.). I can't find a suitable CMD answer to this.

Also I want to plug this into ffmpeg concat, so is there a way to add the required "file" to each line of the text document within the code?

Upvotes: -1

Views: 502

Answers (1)

Sash Sinha
Sash Sinha

Reputation: 22473

Here's a method in PowerShell which should achieve that for you:

  1. Open PowerShell in the directory containing the .mp4 files.

  2. Enter this command to generate the list:

Get-ChildItem *.mp4 | Sort-Object {$_.Name.Split('_')[3]} | ForEach-Object {"file '$($_.FullName)'" } > files.txt

Here's what it does:

  • Get-ChildItem *.mp4 gets a list of all .mp4 files in the current directory.

  • Sort-Object {$_.Name.Split('_')[3]} sorts the files based on the 4th section of the file name (starting from 0) which is the timestamp you are interested in.

  • ForEach-Object {"file '$($_.FullName)'" } prefixes each file name with "file" and includes the full file path.

  • > files.txt outputs the list to a file named files.txt.

After you've done this, you should have a file called "files.txt" in the same directory, with each line looking something like this:

file 'C:\path\to\i_ch3_main_20230401004900_20230401005000.mp4'
file 'C:\path\to\i_ch3_main_20230401023700_20230401023800.mp4'
file 'C:\path\to\i_ch3_main_20230401030500_20230401030600.mp4'
file 'C:\path\to\i_ch3_main_20230401040900_20230401041000.mp4'
file 'C:\path\to\i_ch3_main_20230401050900_20230401051000.mp4'

These can be directly used in ffmpeg concat.

Upvotes: 0

Related Questions