1

I have a bunch of filenames that are timestamped (down to the nanoseconds) and I only want to keep one file per second. For example, here are some file names:

11-14-29-033.jpg

(which means that the file was recorded at 11th hour 14th minute 29th second and 33rd nanosecond).

11-14-29-602.jpg
11-14-29-839.jpg
11-14-29-953.jpg
11-14-30-406.jpg
11-14-30-847.jpg
11-14-31-091.jpg
11-14-31-419.jpg
11-14-31-667.jpg
11-14-32-364.jpg
11-14-32-619.jpg
11-14-32-913.jpg
11-14-33-150.jpg
11-14-33-485.jpg
11-14-33-708.jpg
11-14-34-052.jpg
11-14-34-336.jpg
11-14-35-174.jpg
11-14-36-563.jpg
11-14-36-788.jpg
11-14-37-278.jpg
11-14-37-584.jpg
11-14-38-050.jpg
11-14-38-841.jpg
11-14-39-196.jpg
11-14-39-736.jpg
11-14-39-870.jpg
11-14-40-091.jpg
11-14-40-766.jpg
11-14-41-052.jpg
11-14-41-676.jpg

Is there a way that I can write a batch file (in Windows) to remove all but 1 file per each second?

3
  • 1
    It is possible. Start with for, use a sorted output of dir. In the loop, save first 8 letters of a filename. When it comes again => delete, else => save new prefix.
    – Melebius
    Commented Feb 6, 2018 at 13:44
  • This doesn't change anything in the gist of the question, but I seriously doubt the "nanoseconds" part. Milliseconds – this I will believe. Commented Feb 6, 2018 at 14:00
  • you're right Kamil
    – Hayley
    Commented Feb 7, 2018 at 15:33

1 Answer 1

2

This should do the job:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET "source=C:\adjust\path"

FOR /R "%source%" %%F IN ("??-??-??-???.jpg") DO (
    SET "file_name=%%~nF"
    SET "file_name=!file_name:~0,-4!"
    REN "%%F" "!file_name!%%~xF" >nul 2>&1
)

FOR /R "%source%" %%G IN ("??-??-??-???.jpg") DO (
    DEL "%%G"
)

ECHO Done^^!
PAUSE

This FOR /R loop will rename every .jpg file to from HH-MM-SS-MMM to HH-MM-SS by simply removing the last 4 characters, unless the file name already exists. The REN command will not rename the file then and it will get delete afterwards.

3
  • Your code seems to delete all files except the first file which was renamed to "!file_name!.jpg". In the list of files that I provided as an example, the files below would be the ones that are not deleted: 11-14-29-033.jpg 11-14-30-406.jpg 11-14-31-091.jpg 11-14-32-364.jpg 11-14-33-150.jpg 11-14-34-052.jpg 11-14-35-174.jpg 11-14-36-563.jpg 11-14-37-278.jpg 11-14-38-050.jpg 11-14-39-196.jpg 11-14-40-091.jpg 11-14-41-052.jpg I have thousands of files to process and I want to keep one file per second and discard the rest of the images that occur in the same second.
    – Hayley
    Commented Feb 7, 2018 at 15:42
  • @Hayley I tested it just now on my machine (Windows 10) and it works. I adjusted it a bit to prevent errors. Are you sure you have the line SETLOCAL EnableDelayedExpansion in the script? Commented Feb 7, 2018 at 15:48
  • You're right, I was missing the SETLOCAL EnableDelayedExpansion. It works. Thanks so much for your help!!
    – Hayley
    Commented Feb 7, 2018 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .