1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Sort files in new folders by date

Discussion in 'All Other Software' started by wrep, Jan 17, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    Hello!

    I´m looking for a program or batch file to sort files in new folders according to the files date.

    I have a huge folder with pictures that I want to sort in subfolders by date.
    The subfolders names should be same as files date.
    I´m running windows xp.

    I´ve googled for this but couldn´t find anything?
    So now I need your help :)

    // Regards Per W Sweden
     
  2. ComputerHelpme

    ComputerHelpme Banned

    Joined:
    Jan 17, 2010
    Messages:
    95
  3. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    If you have 1000 files each with a different date I'm sure you don't want 1000 folders each with just one file.
    So how do you want them sorted; by day, by week, by month?
    By year, then into subfolders by month, then subfolders by day?
    By year, then into subfolders by week?
    Doing Month then calendar week would get complex, but month then 7 day periods would be fairly easy.

    How would you want the folders named:
    01
    01Jan
    01-Jan
    01_Jan
    01January
    01 January
    Week - 01
    01 - Week
    There are two dates you can used, Last Modified or Date Created. Which would you want to use?
    Both have potential issues:
    If you edit a picture taken Christmas 1999 today to remove red-eye, the modified date will be today.
    If they were moved from another PC or memory card, the created dates may all be the same.
     
  4. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    I have usually more than one file per date so
    to solve my problem I think it would be enough if the files are sorted by
    Date created named YYYY-MM-DD , but if it isn´t a very big problem the best structure would be:

    YYYY
    YYYY-MM
    YYYY-MM-DD


    Regards // Per W
     
  5. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
  6. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    The most "sexy" batchfile would be one that do both sorting as above and renaming as this one: :cool:

     
  7. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    One more thing, the sorting should be recursive.

    // Per W:)
     
  8. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    So use Date Created, not Date Modified, and prefix the file name with the Date Created (but not the time)
    Sort a folder recursively (i.e., including subfolders)
    Move files to folders in this format:
    Code:
    C:.
    \Sorted
        +---2006
            +---01
            |   \---01
            |   \---02
        ...
            |   \---31
            \---02
            |   \---01
            |   \---02
        ...
            |   \---28
        ...
            \---12
            |   \---01
            |   \---02
        ...
            |   \---31
        +---2007
            +---01
            |   \---01
            |   \---02
        ...
            |   \---31
            \---02
            |   \---01
            |   \---02
        ...
            |   \---28
        ...
            \---12
            |   \---01
            |   \---02
        ...
            |   \---31
        +---2008
            +---01
            |   \---01
            |   \---02
        ...
            |   \---31
            \---02
            |   \---01
            |   \---02
        ...
            |   \---28
        ...
            \---12
            |   \---01
            |   \---02
        ...
            |   \---31
    
    OK, Give this a try.
    This will copy files, not move them, unless you set the Set _Move line to true.
    Specify the Root folder for the sorted tree in the Set _Dest line.
    If will prompt if there is an existing file with the same name.
    Code:
    :: Sort a tree into dated folders using Date Created
    :: Prefix the  Date Created to the file name
    :: Drag and drop folders, specify on command line, or
    :: Put this batch file in the parent folder
    @Echo Off
    Setlocal EnableDelayedExpansion
    :: Set the next line to true if you want to move files.
    :: If not set to true the files will be copied
    [COLOR=Red]Set _Move=False[/COLOR]
    [COLOR=Blue]Set _Dest=C:\Sorted[/COLOR]
    Set _Src=%~dp0
    If Not "%~1"=="" Set _Src=%~1
    Set _DirTok=4
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iTime') Do If "%%B"=="1" Set _DirTok=3
    :: Call GetDate one time to setup variables to speed up processing
    Set _iDate=
    For /F "Tokens=1" %%I In ('Dir /AD "%SystemRoot%"^|Findstr /L ".."') Do Call :_GetDate %%I
    Set _CMCmd=Copy /B
    If /I "%_Move%"=="True" Set _CMCmd=Move
    For /R "%_Src%" %%A In (.) Do (
    PushD "%%A"
    Echo Processing Folder %%A
    For /F "Tokens=1,2,%_DirTok%* Skip=4" %%I In ('Dir /A-D-H-S /OD /TC') Do (
      If /I Not "%%J"=="Dir(s)" If /I Not "%%J"=="File(s)" (
    :: Don't move or rename Desktop.ini or this batch file
        If /I NOT "%%L"=="Desktop.ini" If /I NOT "%%~fL"=="%~f0" (
        Call :_GetDate%_FYDF%%_iDate% "%%I"
        If Not Exist "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!" MD "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!"
        Set _CurName=%%L
        If NOT "!_CurName:~,8!"=="!_fdate!" Set _CurName=!_Fdate!-%%L
        >Nul %_CMCmd% /-Y "%%~fL" "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!\!_CurName!"
       )))
    PopD
    )
    Goto :EOF
    :_GetDate
    :: This batch file will always display the same results,
    :: independent of "International" settings.
    :: This batch file uses REG.EXE from the NT Resource Kit
    :: (already installed with WinXP and Vista)
    :: to read the "International" settings from the registry.
    :: Date is returned as yyyymmdd in variable _fdate
    :: Modified by TheOutcaste http://forums.techguy.org from
    :: SortDate Written byRob van der Woude http://www.robvanderwoude.com
    :: to check for two digit years
    Set _Date=%~1
    If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
    :: Default Delimiter of TAB and Space are used
    If NOT Defined _iDate (
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
    )
    Set _TDYM=
    Set _FYDF=1
    IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
    If "%_fdate:~7,1%"=="" (Set _FYDF=0) & For /F "Tokens=3 skip=3" %%I In ('reg query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do (Set _TDYM=%%I)
    If "%_fdate:~7,1%"=="" For /F "Tokens=3 skip=3" %%I In ('reg query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do Set _TDYM=%%I
    Set _MaxY=29
    Set _Cent=20
    If Defined _TDYM (Set _MaxY=%_TDYM:~2%&Set _Cent=%_TDYM:~0,2%)
    Set /A _Cm1=_Cent-1
    If %_FYDF%==0 If %_fdate:~0,2% LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate00
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%B%%C
    If %%D LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate01
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%C%%B
    If %%D LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate02
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%B%%C%%D
    If %%B LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate10
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%B%%C
    Goto :EOF
    :_Getdate11
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%C%%B
    Goto :EOF
    :_Getdate12
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%B%%C%%D
    Goto :EOF
     
  9. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    Hello!

    It seems to work almost perfect!
    The only thing is that it is creating a empty folders in C:/sorted/ that is named for example 262 witch is the number of files processed per folder...
    Not a big problem they are easy to delete manually.

    The second problem is that I didn´t look carefully enough in my messy folders..... many files seem to have the same "date created" probably depending on if I have copyed or moved them from the memorycards..
    Is it possible to use "Date Picture Taken"(see picture below)?

    If not where do I change the code to get "Date Modified"?

    Thank you for all help so far!:) // Per

    [​IMG]
     
  10. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Is this by chance a Non-English version of Windows?
    The batch file checks the output of the Dir command for the last two lines containing XXX File(s) and XXX Dir(s) as shown here:
    Code:
    01/18/2010  06:14 PM             4,173 Sorttodated.cmd
                  40 [B]File(s)[/B]      4,457,157 bytes
                   6 [B]Dir(s)[/B]  26,325,188,608 bytes free
    If Files(s) or Dir(s) is not the 2nd item on those lines, and is not spelled exactly that way (It's not case sensitive though), it will read the number of files (40 in the above) as another file to process, instead of a line to skip.
    File(s) is in line 25, Dir(s) in line 27. If you edit that to match whatever your system shows there, that should take care of the extra folders.

    The Date Picture Taken is not available to the command line, at least not without other tools. I don't see it on my list of properties that VBScript can access either, so I don't know offhand how to access it..

    To use date modified, change /TC to /TW in line 24, though there's actually a much easier way to do this with date modified; would use the same method as the code you quoted in post 6, which also avoids the need to check for the two summary lines. I can throw that together is just a bit here.
     
  11. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    This will sort by the modified date, and won't create the extra folders.
    I should point out that when moving files, this only moves the files but does not remove the folders. This way you can use this to move just certain extensions and not have to worry about deleting files that remain.

    This won't list each folder, as it can process the entire tree at once instead of doing each folder one at a time as the previous version does
    Code:
    :: Sort a tree into dated folders using Date Modified
    :: Prepend Date Modified to the file name
    :: Drag and drop folders, specify on command line, or
    :: Put this batch file in the parent folder
    @Echo Off
    Setlocal EnableDelayedExpansion
    :: Set the next line to true if you want to move files.
    :: If not set to true the files will be copied
    Set _Move=False
    Set _Dest=C:\Sorted
    Set _Src=%~dp0
    If Not "%~1"=="" Set _Src=%~1
    Set _DirTok=4
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iTime') Do If "%%B"=="1" Set _DirTok=3
    :: Call GetDate one time to setup variables to speed up processing
    Set _iDate=
    For /F "Tokens=1" %%I In ('Dir /AD "%SystemRoot%"^|Findstr /L ".."') Do Call :_GetDate %%I
    Set _CMCmd=Copy /B
    If /I "%_Move%"=="True" Set _CMCmd=Move
    Echo Processing the %_Src% Folder tree, please wait.
    PushD "%_Src%"
    For /F "Tokens=*" %%I In ('Dir /A-D-H-S /B /S') Do (
      If /I NOT "%%I"=="Desktop.ini" If /I NOT "%%~fI"=="%~f0" (
        For /F "Tokens=1,2" %%D In ('Echo %%~tI') Do Call :_GetDate%_FYDF%%_iDate% "%%D"
        Set _CurName=%%~nxI
        If NOT "!_CurName:~,8!"=="!_fdate!" Set _CurName=!_Fdate!-%%~nxI
        If Not Exist "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!" MD "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!"
        >Nul %_CMCmd% /-Y "%%~fI" "%_Dest%\!_Fdate:~0,4!\!_Fdate:~4,2!\!_Fdate:~6,2!\!_CurName!"
    ))
    PopD
    Goto :EOF
    :_GetDate
    :: This batch file will always display the same results,
    :: independent of "International" settings.
    :: This batch file uses REG.EXE from the NT Resource Kit
    :: (already installed with WinXP and Vista)
    :: to read the "International" settings from the registry.
    :: Date is returned as yyyymmdd in variable _fdate
    :: Modified by TheOutcaste http://forums.techguy.org from
    :: SortDate Written byRob van der Woude http://www.robvanderwoude.com
    :: to check for two digit years
    Set _Date=%~1
    If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
    :: Default Delimiter of TAB and Space are used
    If NOT Defined _iDate (
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
    )
    Set _TDYM=
    Set _FYDF=1
    IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
    If "%_fdate:~7,1%"=="" (Set _FYDF=0) & For /F "Tokens=3 skip=3" %%I In ('reg query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do (Set _TDYM=%%I)
    If "%_fdate:~7,1%"=="" For /F "Tokens=3 skip=3" %%I In ('reg query "HKCU\Control Panel\International\Calendars\TwoDigitYearMax" /V 1 2^>Nul') Do Set _TDYM=%%I
    Set _MaxY=29
    Set _Cent=20
    If Defined _TDYM (Set _MaxY=%_TDYM:~2%&Set _Cent=%_TDYM:~0,2%)
    Set /A _Cm1=_Cent-1
    If %_FYDF%==0 If %_fdate:~0,2% LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate00
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%B%%C
    If %%D LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate01
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%C%%B
    If %%D LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate02
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%B%%C%%D
    If %%B LEQ %_MaxY% (Set _fdate=%_Cent%%_fdate%) Else (Set _fdate=%_Cm1%%_fdate%)
    Goto :EOF
    :_Getdate10
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%B%%C
    Goto :EOF
    :_Getdate11
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%D%%C%%B
    Goto :EOF
    :_Getdate12
    For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%~1") Do Set _fdate=%%B%%C%%D
    Goto :EOF
     
  12. ComputerHelpme

    ComputerHelpme Banned

    Joined:
    Jan 17, 2010
    Messages:
    95
    must be missing some code
     
  13. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Nope, it's all there
     
  14. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    The last one worked just perfect :)

    You where right about the language!
    I´m using swedish windows and it should be fil(er) and katalog(er)
    I havn´t tried that one yet but I will try it later on and report how it worked out.

    "Date picture taken" is probably from exif-data? and that would need a special program to read?

    I´m very happy for your help! Thanks again! :) :)
    // Per
     
  15. wrep

    wrep Thread Starter

    Joined:
    Jan 17, 2010
    Messages:
    11
    About TC and TW :

    I tought it was that one I should change but I didn´t know what to change it to... My best guess was TM for time modified.. :) ....

    Is it possible to change Time Modifed to Time created in this new version as well? What is the real differens between the two methods?

    Regards // Per
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/894988

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice