The Notebook Review forums were hosted by TechTarget, who shut down them down on January 31, 2022. This static read-only archive was pulled by NBR forum users between January 20 and January 31, 2022, in an effort to make sure that the valuable technical information that had been posted on the forums is preserved. For current discussions, many NBR forum users moved over to NotebookTalk.net after the shutdown.
Problems? See this thread at archive.org.

    Copying every 50th file from one folder to another

    Discussion in 'Windows OS and Software' started by killkenny1, Aug 26, 2016.

  1. killkenny1

    killkenny1 Too weird to live, too rare to die.

    Reputations:
    8,268
    Messages:
    5,256
    Likes Received:
    11,609
    Trophy Points:
    681
    So basically like the thread title says, I need to copy every 50th image from one folder to another.
    I would write a Python script, but unfortunately I'm too stupid for that... And manually copying those images is not a solutions as well...

    So, any ideas?
     
  2. TreeTops Ranch

    TreeTops Ranch Notebook Deity

    Reputations:
    330
    Messages:
    904
    Likes Received:
    124
    Trophy Points:
    56
    Have you looked at the various switches in Xcopy? I know it can copy files that have a date you specify and it can copy 'read only' files with the proper switch. So you could make every 50th a read only file then it will copy that one only. Check out the other switches here: http://pcsupport.about.com/od/commandlinereference/p/xcopy-command.htm

    Maybe if you explained why you want to copy the 50th file, then someone can come up with a script to do it. As it stands now, your request seems a bit odd.
     
    killkenny1 likes this.
  3. Kaze No Tamashii

    Kaze No Tamashii Notebook Evangelist

    Reputations:
    24
    Messages:
    373
    Likes Received:
    112
    Trophy Points:
    56
    maybe change every file's name into number in ascending order then write a script to copy file whose name is a multiple of 50?
     
  4. killkenny1

    killkenny1 Too weird to live, too rare to die.

    Reputations:
    8,268
    Messages:
    5,256
    Likes Received:
    11,609
    Trophy Points:
    681
    Why is it odd? Is copying something in a specific order an odd thing?
    But sure, if it will make easier for you and the others to understand I can explain the reason for my request.

    I did some testing, and it involved high speed camera taking lots of pictures, so in the end I have more than 30000 (that's right, I said thirty thousand) files. It was decided that using every 50th image will be enough for evaluation purposes. But as you can see, with such high number of date going through them manually is not an option.
    And to make things more interesting, they aren't labelled 1, 2, ..., n, although they are still in an ascending order.

    I'll look into Xcopy.
     
  5. Jarhead

    Jarhead 恋の♡アカサタナ

    Reputations:
    5,036
    Messages:
    12,168
    Likes Received:
    3,132
    Trophy Points:
    681
    Well, I don't have a Python interpreter on hand at the moment, though this should be close to what you want to do in a Python script (I'm assuming you mean every 50th by timestamp):

    Code:
    import os
    from shutil import copyfile
    
    def sortedListing(pathtodir):
        mtime = lambda f: os.stat(os.path.join(pathtodir, f)).st_mtime
        return list(sorted(os.listdir(pathtodir), key=mtime))
    
    filesToCopy = sortedListing("C:\path\to\dir")[0::50] # Selects every 50th file by timestamp
    
    for file in filesToCopy:
        copyfile(file, "C:\path\to\copy\to")
    
     
    killkenny1 likes this.
  6. killkenny1

    killkenny1 Too weird to live, too rare to die.

    Reputations:
    8,268
    Messages:
    5,256
    Likes Received:
    11,609
    Trophy Points:
    681
    Thanks for getting me started, much appreciated!
    I did a bit of Googling and ultimately went with a bit of different code.

    Code:
    import os, shutil
    
    src_files = os.listdir('E:\\Desktop\\Cam')
    for file_name in src_files:
      if file_name.endswith('50.bmp'):
      full_file_name = os.path.join('E:\\Desktop\\Cam', file_name)
      if (os.path.isfile(full_file_name)):
      shutil.copy(full_file_name, 'E:\\Desktop\\Cam\\New folder')
    Basically copy-pasted the entire thing and added one line, and now script copies every file which has "50" in its name, which leads to copying every 43th file. Close enough :D
    I might try something more more later at work.

    BTW, if anyone is interested, this is how file names look:
    [​IMG]

    Obviously for testing purposes I only had a small number of images present in that folder.
     
  7. Jarhead

    Jarhead 恋の♡アカサタナ

    Reputations:
    5,036
    Messages:
    12,168
    Likes Received:
    3,132
    Trophy Points:
    681
    Glad it worked out for ya. :)
     
  8. katalin_2003

    katalin_2003 NBR Spectre Super Moderator

    Reputations:
    14,958
    Messages:
    5,671
    Likes Received:
    1,519
    Trophy Points:
    331
    The script from @Jarhead is more portable for your situation.
    I would tweak that to your needs.
     
  9. Jarhead

    Jarhead 恋の♡アカサタナ

    Reputations:
    5,036
    Messages:
    12,168
    Likes Received:
    3,132
    Trophy Points:
    681
    Shutil.copy is also a fair bit slower than copyfile, though if it works for you then it works. :)

    Not sure if the sorted listing can be faster. Still new to Python and I basically ripped that straight from StackOverflow.
     
    killkenny1 and katalin_2003 like this.
  10. killkenny1

    killkenny1 Too weird to live, too rare to die.

    Reputations:
    8,268
    Messages:
    5,256
    Likes Received:
    11,609
    Trophy Points:
    681
    Shutil.copy worked fine for my case.

    But just in case, I have commented the code Jarhead has posted in the program :D