2

I want to be able to isolate and search using the windows DIR command for only the files that contain an extra numeric at the end. How do I do this? I have attached a picture that shows an example of one of my directories the link is below at the end.

"Track 01 1.m4a" and some are named "Track 01.m4a"? How can I list just the ones that were added with the numeric at the end? Also while we are at it? I have multiple duplicates sometimes 4 or five so I'm expecting to have to search for files with a range of numbers like "* [1-4].m4a"

If I can be clearer in my question please ask.

List of files:

enter image description here

Dr. Dan
  • 123

2 Answers2

1
dir /b | findstr

will let you do what you want to do.

0

I'm combining several answers and comments with my own thoughts to make a clearer answer.

To find m4a files in a directory where filenames can be recognized as duplicates (or just can be recognized as those we want to print) by having a number, a space, ' ' followed by an extra numeric – [1-4] – before the extension, the following command will work.

dir /b | findstr /e /r /c:" [1-4].m4a"

From my command line, you'll see I've made a directory with the same filenames as those visible in the image/screenshot from the OP. I'll run the command, so you can see the results. Note that there is a more expanded (more complete / useful-in-more-scenarios) version after.

C:\Users\bballdave025\tmp_1>dir /b
Track 01 1.m4a
Track 01.m4a
Track 02 1.m4a
Track 02.m4a
Track 03 1.m4a
Track 03.m4a
Track 04 1.m4a
Track 04.m4a
Track 05 1.m4a
Track 05.m4a
Track 06 1.m4a
Track 06.m4a
Track 07 1.m4a
Track 07.m4a
Track 08 1.m4a
Track 08.m4a
Track 09 1.m4a
Track 09.m4a
Track 10 1.m4a
Track 10.m4a
Track 11 1.m4a
Track 11.m4a
C:\Users\bballdave025\tmp_1>dir /b | findstr /e /r /c:" [1-4].m4a"
Track 01 1.m4a
Track 02 1.m4a
Track 03 1.m4a
Track 04 1.m4a
Track 05 1.m4a
Track 06 1.m4a
Track 07 1.m4a
Track 08 1.m4a
Track 09 1.m4a
Track 10 1.m4a
Track 11 1.m4a

C:\Users\bballdave025\tmp_1>

If any of the filenames won't always have a number before the space-then-numeric, e.g.   Track whatever 1.m4a   , then you can only use such a regex to match a space followed by a single-digit number. In the case of the last numeric after the space and before the dot-extension being greater than 9, you can match any of the types of filenames you've just shown in your screenshot by adding   [0-9]   after the first double quote ("). Basically, if you want this to work for anything other than <base-filename&gt<space&gt<number&gt<space&gt<number>, you would have to give us more information about the types of filenames you have. To see an example where there would be a problem, try using   dir /b | findstr /e /r /c:" [0-9].m4a"   on your directory (or on my copy of it, see Note 2 for details on how to create the directory with empty files). You'll get   Track 10.m4a   and   Track 11.m4a   included. The command you would need – as long as there is always a number-space-numeric-dot-filename – is   dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a"   , as described below.

More complete

I would change the command so that you can find <number><space><any-number>.m4a. To do this,

dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a"

Explanation:

'dir' -> Displays a list of files and subdirectories in a directory
'/b' -> Get only filename or subdirectory name

'|' -> Pipes the output of the previous (dir /b) command, giving
it to the next command as input

'findstr' -> Finds any line (filename) containing the pattern which
follows the command and any tags
'/e' -> Matches pattern if at the end of a line, making
sure the 'm4a' is really the end of the filename
'/r' -> Uses search strings as regular expressions
'/c:' -> Uses specified string as a literal search string
(meaning you don't have to use escape characters)
'" [1-9][0-9]*.m4a"' -> Matches any line (filename) as
you have described:
with the numeric at the end

I'll give some more detail about the pattern

'"' -> Begins the literal search string. Useful if there are
spaces in the pattern/search string.
'[0-9]' -> Any of the characters in 1-9, meaning any of
{0,1,2,3,4,5,6,7,8,9}
' ' -> a space
'[1-9]' -> Any of the characters in 1-9, meaning any of
{1,2,3,4,5,6,7,8,9}, i.e. any digit character
other than 0 (it won't work if any of your
numeric-added filenames finish with something
like ' 03.m4a'). If you want any leading
zeros, use [0-9] instead of [1-9]
'[0-9]*' -> zero or more of any numeric character,
this time any of {0,1,2,3,4,5,6,7,8,9}.
Example matches for this part include '0',
'6', '307', '234690'. 'Zero or more (matches)'
means you can have no matches, 1 match,
2 matches, 3 matches, ..., up to as many matches
as your computer can handle
'.' -> a single dot, in this case coming right before
the extension. Note that you don't need escapes
'm4a' -> your file extension
'"' -> Ends the literal search string

Showing how the more complete version could be used

This more complete version gives us the same result as the one before, if we use it in the same directory.

C:\Users\bballdave025\tmp_1>dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a"
Track 01 1.m4a
Track 02 1.m4a
Track 03 1.m4a
Track 04 1.m4a
Track 05 1.m4a
Track 06 1.m4a
Track 07 1.m4a
Track 08 1.m4a
Track 09 1.m4a
Track 10 1.m4a
Track 11 1.m4a

C:\Users\bballdave025\tmp_1>

I've now created a directory called tmp_2 with all the same files as are in tmp_1 as well as the additional files1 that follow,

Track 12.m4a
Track 12 1.m4a
Track 12 2.m4a
Track 12 3.m4a
Track 12 4.m4a
Track 12 5.m4a
Track 12 6.m4a
Track 12 7.m4a
Track 12 8.m4a
Track 12 9.m4a
Track 12 10.m4a
Track 12 11.m4a
Track 12 12.m4a
Track 12 13.m4a
Track 12 137.m4a

of which all but Track 12.m4a should be matched by the check.

Into the same tmp_2 directory, I'm also going to add two files which should not be matched by the check,

Track 13 01.m4a
Track fourteen 1.m4a

Let's try it

C:\Users\bballdave025\tmp_2>dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a"
Track 01 1.m4a
Track 02 1.m4a
Track 03 1.m4a
Track 04 1.m4a
Track 05 1.m4a
Track 06 1.m4a
Track 07 1.m4a
Track 08 1.m4a
Track 09 1.m4a
Track 10 1.m4a
Track 11 1.m4a
Track 12 1.m4a
Track 12 10.m4a
Track 12 11.m4a
Track 12 12.m4a
Track 12 13.m4a
Track 12 137.m4a
Track 12 2.m4a
Track 12 3.m4a
Track 12 4.m4a
Track 12 5.m4a
Track 12 6.m4a
Track 12 7.m4a
Track 12 8.m4a
Track 12 9.m4a

The other commands won't give us this response2, which I assume is the response desired by the OP

Deleting the files you've found

The OP mentioned wanting to do this in a comment (you can find it in the archived page by searching for "What if next I want to delete those files I just searched for?").

I'll just give the bare bones. My plan would be to send the output of the command to a file, e.g.

tmp_1>dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a" > to_del.txt

Then use a for loop over the lines in that file. I would try the options in this SU answer (archived). and see which one works without just getting rid of to_del.txt and without any other problems.

I found the right command.

C:\Users\bballdave025\tmp_1>for /f "delims=" %f in (to_del.txt) do del "%f"
Do not do any of the following without first making a backup copy of the directory with all the media files! The method hasn't been tested. If it works, you can them delete the backup.

I could also use some of the ideas in the same Q&A, especially with Notepad++ and the regexes included to create a batch file that would delete everything. For example (in Notepad++), open the to_del.txt file, save it as del_them.bat, Ctrl+h, select the use-regular-expressions radio button (no on the '. includes newline' option), search ^(.*)$, replace with del /f "\1", probably go through the replacements one-by-one, though 'replace all' might be okay (this is all untested), then run the batch file with

tmp_1>.\del_them.bat

Notes

[1] I take care of adding these files, not using my favorite strategy (archived) (since there can be big problems if you're not absolutely sure the non-existing command you use doesn't exist), but another out of many (archived), many (archived) strategies(archived) for making empty files.

For example,

C:\Users\bballdave025\tmp_2>echo. >NUL 2>"Track 12 13.m4a"
C:\Users\bballdave025\tmp_2>echo. >NUL 2>"Track 13 01.m4a"

Oh, I think I found a safe way to do my favorite.

C:\Users\bballdave025\tmp_2>.>"Track fourteen 1.m4a"

which gives an error,

'.' is not recognized as an internal or external command,
operable program or batch file.

but it also gives an empty file.


(You can add on 2>NUL if you don't want to see the error.)



[2]Here are the results of the other commands in the tmp_2 directory

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:" [1-4].m4a"
Track 01 1.m4a
Track 02 1.m4a
Track 03 1.m4a
Track 04 1.m4a
Track 05 1.m4a
Track 06 1.m4a
Track 07 1.m4a
Track 08 1.m4a
Track 09 1.m4a
Track 10 1.m4a
Track 11 1.m4a
Track 12 1.m4a
Track 12 2.m4a
Track 12 3.m4a
Track 12 4.m4a
Track fourteen 1.m4a

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:"[0-9] [1-4].m4a" Track 01 1.m4a Track 02 1.m4a Track 03 1.m4a Track 04 1.m4a Track 05 1.m4a Track 06 1.m4a Track 07 1.m4a Track 08 1.m4a Track 09 1.m4a Track 10 1.m4a Track 11 1.m4a Track 12 1.m4a Track 12 2.m4a Track 12 3.m4a Track 12 4.m4a

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:" [1-9].m4a" Track 01 1.m4a Track 02 1.m4a Track 03 1.m4a Track 04 1.m4a Track 05 1.m4a Track 06 1.m4a Track 07 1.m4a Track 08 1.m4a Track 09 1.m4a Track 10 1.m4a Track 11 1.m4a Track 12 1.m4a Track 12 2.m4a Track 12 3.m4a Track 12 4.m4a Track 12 5.m4a Track 12 6.m4a Track 12 7.m4a Track 12 8.m4a Track 12 9.m4a Track fourteen 1.m4a

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:"[0-9] [1-9].m4a" Track 01 1.m4a Track 02 1.m4a Track 03 1.m4a Track 04 1.m4a Track 05 1.m4a Track 06 1.m4a Track 07 1.m4a Track 08 1.m4a Track 09 1.m4a Track 10 1.m4a Track 11 1.m4a Track 12 1.m4a Track 12 2.m4a Track 12 3.m4a Track 12 4.m4a Track 12 5.m4a Track 12 6.m4a Track 12 7.m4a Track 12 8.m4a Track 12 9.m4a

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:" [1-9][0-9]*.m4a" Track 01 1.m4a Track 02 1.m4a Track 03 1.m4a Track 04 1.m4a Track 05 1.m4a Track 06 1.m4a Track 07 1.m4a Track 08 1.m4a Track 09 1.m4a Track 10 1.m4a Track 10.m4a Track 11 1.m4a Track 11.m4a Track 12 1.m4a Track 12 10.m4a Track 12 11.m4a Track 12 12.m4a Track 12 13.m4a Track 12 137.m4a Track 12 2.m4a Track 12 3.m4a Track 12 4.m4a Track 12 5.m4a Track 12 6.m4a Track 12 7.m4a Track 12 8.m4a Track 12 9.m4a Track 12.m4a Track fourteen 1.m4a

C:\Users\bballdave025\tmp_02>dir /b | findstr /e /r /c:"[0-9] [1-9][0-9]*.m4a" Track 01 1.m4a Track 02 1.m4a Track 03 1.m4a Track 04 1.m4a Track 05 1.m4a Track 06 1.m4a Track 07 1.m4a Track 08 1.m4a Track 09 1.m4a Track 10 1.m4a Track 11 1.m4a Track 12 1.m4a Track 12 10.m4a Track 12 11.m4a Track 12 12.m4a Track 12 13.m4a Track 12 137.m4a Track 12 2.m4a Track 12 3.m4a Track 12 4.m4a Track 12 5.m4a Track 12 6.m4a Track 12 7.m4a Track 12 8.m4a Track 12 9.m4a

C:\Users\bballdave025\tmp_02>


I only mentioned some of them, briefly, in passing.