logo elektroda
logo elektroda
X
logo elektroda

Script for Organizing Smartphone Photos into Folders by Date: Creating Directories & Copying Files

skorik 16611 16
Best answers

How can I write a .bat script that reads smartphone photo filenames or the Date Taken metadata and moves each photo into year/month/day folders?

Put the batch file in the folder that contains the photos, and let it process the files there so it can create the needed directories and move the images into them [#16692030][#16942379] The thread confirms the script works, and the folder-name format can be changed so year, month, and day are separated by dots instead of the original separators [#16692809]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16691715
    skorik
    Level 12  
    Posts: 48
    Help: 1
    Rate: 10
    I have this problem:
    I rip photos from smartphones to one folder.
    I need a script that puts files (photos) in the directories it creates.
    The directory names that would be: year. month. day.
    To create these folders, the script would have to use the image file property: Date Taken or get it by name. And one folder for a different number of files - sometimes for one and sometimes for several hundred.
    The most common file names are (example):
    20170325_104914 or
    IMG_20170825_204256316 (phone dependent)

    Is there anyone able to create such a script (the point is to save it in a .bat file)
  • ADVERTISEMENT
  • Helpful post
    #2 16692030
    clubs
    Level 38  
    Posts: 2219
    Help: 629
    Rate: 406
    Hello

    Put it in the photo catalog
    Code: Dos
    Log in, to see the code
  • #3 16692214
    skorik
    Level 12  
    Posts: 48
    Help: 1
    Rate: 10
    Cool.
    The script works fine.
    What needs to be changed to have dots separating the year, month and day in the folder name?
  • Helpful post
    #4 16692809
    clubs
    Level 38  
    Posts: 2219
    Help: 629
    Rate: 406
    Code: Dos
    Log in, to see the code
  • #5 16692924
    skorik
    Level 12  
    Posts: 48
    Help: 1
    Rate: 10
    Everything works fine.
    Thank you very much.
    For so many years, many hours lost in manual sorting ...
    And it was enough to ask on the electrode :)
  • ADVERTISEMENT
  • #6 16940055
    husk123
    Level 9  
    Posts: 15
    Rate: 18
    Hi,
    I will connect to the topic, because I have a similar problem and also need help writing the script.
    I have some example files in the directory:
    aaaa.xxx
    bbbb.xxx
    cccc.xxx
    cccc.xxy
    dddd.yyy
    dddd.xxy
    dddd.xxz
    eeee.zzz
    ffff.zzz
    ffff.xxy
    I would like the script to create the directories aaaa, bbbb, cccc, dddd, eeee and ffff based on the files, and copy the files to them to aaaa - aaaa.xxx; to bbbb - bbbb.xxx; to cccc - cccc.xxx and cccc.xxy; to dddd - dddd.xxx, dddd.xxy and dddd.xxz; to eeee - eeee.zzz; a to ffff - ffff.zzz and ffff.xxy.
    So far I have been able to spit all files with the DIR command into the csv file (dir / b> file.csv) and then in Excel I divided the file names into name and extension. I was removing takes and using the joins.texts function for the command, creating one to create a directory based on name and the other to copy files into a directory based on name. Unfortunately, this method is also time-consuming and not very versatile.
  • ADVERTISEMENT
  • #7 16940370
    clubs
    Level 38  
    Posts: 2219
    Help: 629
    Rate: 406
    Hello
    Should you copy or move?
  • #8 16941005
    husk123
    Level 9  
    Posts: 15
    Rate: 18
    I actually wrote that he copied, but I mean he would transfer them. If the script was similar to the above, I could fix xcopy / copy on move, but plus for perceptiveness. ;) Alternatively, if you could manage to include the four-character extensions and the fact that there are spaces in the filename and different lengths of the filenames. I started trying something myself and only made myself a bigger mess. I have created a lot of empty directories that are components of file names. A request to describe the script as well - I prefer a fishing rod to a fish.
  • #9 16942379
    clubs
    Level 38  
    Posts: 2219
    Help: 629
    Rate: 406
    Hello
    Put the bat in the directory where you have these files
    Code: Dos
    Log in, to see the code
  • #10 16945863
    husk123
    Level 9  
    Posts: 15
    Rate: 18
    Thanks,
    For those who would like to use the script for something else.
    @Echo. off - disable command display
    setlocal enabledelayedexpansion - enable variable scope referring only to this batch file

    for %% i in (*) to (- starting a FOR loop for the variable "i" which takes the values of individual files "*" from the directory
    if / i not "%% ~ xi" == ". bat" (- if the file extension name "%% ~ xi" is not equal to ".bat", run the commands
    set "f = %% ~ ni" & set "p = %% i" - assign the variable f the value of the file name and assign the variable p the value of the variable i
    if not exist "! f!" md "! f!" - if the directory with the name contained in the variable "f" does not exist, create it
    move "! p!" "! f!")) - move the file named in the variable "p" to the directory named in the variable "f", ending IF commands and ending the FOR loop

    If I have made a mistake, please correct it.
  • #11 16988278
    Pyra19
    Level 10  
    Posts: 8
    Hello,

    I would like to attach to the topic. I have a very similar problem with sorting documents. The files are in pdf format. named "Skan_20170116153912". I would like to sort the files in the folders YEAR> Month> Day> a file called "scan_20170116153912. More or less I would like the path to look like. I will add that the files arrive every day so this script would be used at least once a week.

    e.g.
    Folder named "2018"
    this folder would contain folders with the names of all months ("JANUARY", "FEBRUARY" etc.)
    and in each of the folders with the name of the month there would be folders with the name of the full date (e.g. 2018-01-01, 2018-01-02 etc.)
    And the corresponding documents would be moved to each dated folder.

    example: scans \ 2018 \ JANUARY \ 02-01-2018 \ scanned_20180102153912.pdf
  • #12 16989360
    dondu
    VIP Meritorious for electroda.pl
    Posts: 13906
    Help: 1292
    Rate: 809
    Pyra19 wrote:
    example: scans \ 2018 \ JANUARY \ 02-01-2018 \ scanned_20180102153912.pdf

    Good advice: Do not use Polish letters in the names of files and folders.
  • #13 16989374
    rajszym
    Level 21  
    Posts: 272
    Help: 48
    Rate: 18
    @ Pyra19 : self-study material
    Code: Dos
    Log in, to see the code
  • ADVERTISEMENT
  • #14 16990713
    Pyra19
    Level 10  
    Posts: 8
    Thank you, that's what I meant. :D
  • #15 17081442
    bugaj777nbl
    Level 2  
    Posts: 2
    Welcome.

    so I would gently like to attach to the topic. Everything above generally suits me :) hula beautifully.
    however, I have a question about one additional variant.
    I need something that will create for me and move files to created folders after the strings contained in it.

    I have something that creates a pdf with the employee's number: from 1 character to 9 inclusive and this is to be the key to creating the file.

    i.e. e.g.

    a0989a__1_kowalski_jan_za_2017.pdf
    a0989a__1_kowalski_jan_za_2016.pdf
    a9121a_21_kowalska_janina_za_2017.pdf
    a9121a_21_kowalska_janina_za_2016.pdf

    e.t.c.

    fixed key / element is here: a0989a__1 and a9121a_21 within these characters I would like directories to be created in sequence:

    a0989a__1 with files: a0989a__1_kowalski_jan_za_2017.pdf, a0989a__1_kowalski_jan_za_2016.pdf
    a9121a_21 with files: a9121a_21_kowalska_janina_za_2017.pdf, a9121a_21_kowalska_janina_za_2016.pdf

    thank you in advance.
  • #16 17081738
    clubs
    Level 38  
    Posts: 2219
    Help: 629
    Rate: 406
    bugaj777nbl wrote:
    I need something that will create and move files to the created folders by the string of characters in it


    Hello
    and where do you see the problem? by rewriting any of the above-mentioned script
  • #17 17082497
    bugaj777nbl
    Level 2  
    Posts: 2
    Hello.

    thanks for the answer. The problem is that I do not have any basic knowledge about what is written here. I admit that I need it, that it works, and when I sit down for a while, I more or less understand what's going on, but without basic knowledge, I honestly don't know how to move or in any direction.

    So I will be grateful for any tips where you can learn, not to talk about "cash" :)

    greeting, M.

Topic summary

✨ A user sought assistance in creating a batch script to organize smartphone photos into directories based on the date taken, formatted as year.month.day. Initial responses provided a basic script that successfully moved files into newly created folders. Subsequent discussions refined the script to include the desired date format and addressed variations in file naming conventions. Additional users contributed similar requests for organizing files based on different criteria, such as document types and naming patterns. The community provided various script examples and modifications, emphasizing the importance of understanding batch scripting for effective file management.
Generated by the language model.

FAQ

TL;DR: A simple 1-file .bat can auto-sort your photos by date; “Put the bat in the directory where you have these files.” [Elektroda, clubs, post #16942379]

Why it matters: Fast, repeatable sorting saves hours when you rip mixed smartphone photos into one folder—no manual dragging required. This FAQ is for Windows users who want quick scripts to create date-based folders and move files automatically.

Quick Facts

How do I sort smartphone photos into YYYY.MM.DD folders with a batch file?

Use the posted .bat that parses dates from names and moves each JPG into a folder like 2017.03.25. How-To: 1. Put the .bat next to your photos. 2. Double-click it. 3. Check created folders named Year.Month.Day containing the moved images. [Elektroda, clubs, post #16692809]

Will this handle both IMG_YYYYMMDD... and 20170325_... filenames?

Yes. The script checks if a name starts with “IMG” and adjusts the substring offsets. It extracts YYYY, MM, and DD correctly for both patterns, then creates the matching date folder and moves the file. [Elektroda, clubs, post #16692030]

How do I get dots between year, month, and day in folder names?

Use the updated substring expression that builds folders as YYYY.MM.DD. The shared revision sets n to YYYY.MM.DD for both IMG-prefixed and plain date-prefixed files before running MOVE. [Elektroda, clubs, post #16692809]

How do I prevent the .bat file itself from being moved?

Use the condition that skips files with the .bat extension inside the FOR loop. The example excludes the script and moves everything else into a folder based on each filename’s stem. [Elektroda, clubs, post #16942379]

Can I group files by their base name (prefix) into same-named folders?

Yes. The helper script creates a folder from each file’s name (without extension) and moves the file there. It safely handles spaces using quoted paths. Example: dddd.xxx, dddd.xxy, dddd.xxz all go into \dddd. [Elektroda, clubs, post #16942379]

Move vs copy: what does the shared solution use?

MOVE is used in the working examples. If you need copies instead, replace MOVE with COPY, but the showcased workflows transfer files to their new folders by default. [Elektroda, clubs, post #16942379]

How do I sort PDF scans named skan_20170116153912 into Year/Month/Day?

Use the PDF-oriented .bat that extracts year, month, and day from the filename. It builds nested folders: Year\Year.Month\Year.Month.Day, then moves the PDF into the dated folder. “Self-study material” shows the exact loop. [Elektroda, rajszym, post #16989374]

Any naming gotchas I should avoid when creating folders?

Yes. “Good advice: Do not use Polish letters in the names of files and folders.” Avoid diacritics to reduce cross-system and script compatibility issues. [Elektroda, dondu, post #16989360]

Will it handle files with spaces and four-character extensions?

Yes. The grouping script quotes variables like "!p!" and "!f!" in MOVE and MD commands, which preserves spaces and supports extensions beyond three characters. [Elektroda, clubs, post #16942379]

What happens if a filename doesn’t contain a usable date?

Date-sorting variants derive the date from fixed offsets in the filename. Files that don’t match expected patterns won’t produce correct YYYY.MM.DD values and should be handled separately or renamed first. [Elektroda, clubs, post #16692809]

Can these scripts process hundreds of files at once?

Yes. They loop over all matching files in the folder in one run. The original need described one folder holding “sometimes … several hundred” photos, which the batch approach addresses. [Elektroda, skorik, post #16691715]

Where should I place the .bat before running it?

Place the .bat in the directory that contains the files you want to organize. Then run it so the FOR loop iterates over the local files without extra paths. [Elektroda, clubs, post #16942379]

How can I sort documents by an employee ID embedded in the filename?

Extract the stable key before the next underscore, then create a folder named after that key and move matching files. The use case cites IDs from 1 to 9 digits, which batching accommodates with substring/token logic. [Elektroda, bugaj777nbl, post #17081442]

Can I adapt the PDF script to use Year\Year.Month\Year.Month.Day structure for images too?

Yes. Copy the folder-creation pattern from the PDF example and replace the filename parsing with the JPG offsets. The MD commands then build nested date folders before MOVE. [Elektroda, rajszym, post #16989374]

Any quick way to learn what each line in the grouping script does?

See the user’s annotated breakdown explaining @echo off, setlocal enabledelayedexpansion, FOR variables, IF filtering, MD creation, and MOVE. It’s a clear, line-by-line guide. [Elektroda, husk123, post #16945863]

What’s a safe test workflow before I run this on all photos?

Start in a copy of your folder, place the .bat there, and run it. Verify new folders and moved files look right, then repeat in the real location. Keep names without diacritics. [Elektroda, clubs, post #16942379]
Generated by the language model.
ADVERTISEMENT