Sort folders by date in .bat

Solved
Silk7499 Posted messages 5 Status Member -  
Silk7499 Posted messages 5 Status Member -
Bonjour,

I have a folder that is created automatically every day (which is named with the date of the day it was created in this format, for June 14, 2018: 14.06.2018) and which contains folders and backups.
So far, so good, and I end up with 365 folders at the end of the year in my main directory C:\FTP (as there are 365 days).

I would like to create a .Bat script, so that, each month following the 30 or 31 folders of the previous month, (For example, as soon as we move into July (month number 7) and only at that moment, all the folders with the date of the month of June (month number 6) are redirected into a folder named 06-2018.

And I'm really struggling with this; I tried with tokens or delimiters, but each time there's a problem. If you could help me, that would be really great..!

Thank you in advance!

1 answer

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    Tell me if this works for you:

    @echo off
    setlocal enableextensions enabledelayedexpansion
    chcp 1252 >nul

    set Folder=C:\FTP

    cd /d %Folder% || exit /b 1

    set today=%date:~0,2%
    set month=%date:~3,2%

    set /a previous=!month!-1
    set previous=0!previous!
    set previous=!previous:~-2!

    if !today! EQU 01 (call :move)

    echo. & pause
    exit /b 0

    :move
    for /f "tokens=1-3 delims=." %%A in ('dir /b /ad') do (
    set destination=!previous!-%%C\%%A.%%B.%%C
    if "%%B"=="!previous!" (
    echo. & echo ### %%A.%%B.%%C ==^>^> !previous!-%%C
    if not exist "!destination!" (mkdir "!destination!")
    >nul robocopy "%%A.%%B.%%C" "!destination!" /E /DCOPY:T /SEC /MOVE)
    )
    goto :eof

    rem

    If you have any questions, feel free to ask.

    ps: during the week I am only available in the evening.

    “Artificial intelligence is defined as the opposite of natural stupidity.”
    0
    1. Silk7499 Posted messages 5 Status Member
       
      Hello,

      First of all, thank you very much, your script does exactly what I wanted and it's great!

      But I noticed that it doesn't overwrite files when it moves them; for example, if I run the script on 30/06/2018 at 8 am, it will put the folder and everything in the created folder, and if I run it again on the same day at 2 pm, it will add them, which is nice because when I tried to create something, it would overwrite them every time. That's why I chose a fairly complicated reasoning...

      So, at my level, the complexity of your program is quite high for me :/ But could you modify it so that I can run it at any time, and not just on the 30th/31st? Since it ultimately works just as well every day of the week?

      Thank you in advance for your response, and thanks again for the program you've already provided!!
      0
    2. Silk7499 Posted messages 5 Status Member
       
      I finally found the answer to my problem on my own after diving into the program and clarifying the main points! What I asked you in the second part wasn't complicated ;) Everything is good for me, thank you so much! :)
      0