I have around 100,000 files with names like:

  • completedCourses_2017-10-08 11 18-05-02.txt

  • emptyCoursesResults_2017-10-08 11 13-21-59.txt

All these files are in one directory, and I want to "split" it by date into directories, e.g. all files with date 2017-10-08 going into directory with name 2017-10-08, files with date 2018-01-02 going into directory with name 2018-01-02 - no matter what is on start of name and on end.

Is it possible using a Windows solution?


You can use a FOR /F loop and set the "USEBACKQ TOKENS=2 DELIMS=_ " like that to parse out and get the date string from the file name to reuse in subsequent commands accordingly for the operations you need.

You will then use a mixture of commands such as DIR, MD, XCOPY, and DEL to perform the file operations to get those moved over to the new date string folder.

Note: Be sure to set the Src= variable value to be the folder where these files exist, and be sure to set the Dest= variable value to be the folder where the files will be moved and the yyyy-mm-dd folder will be created for those files to reside once moved.


SET "Src=C:\Folder\Source"
SET "Dest=C:\Folder\Destination"

FOR /F "USEBACKQ TOKENS=2 DELIMS=_ " %%A IN (`DIR /B "%Src%\*.txt"`) DO (
    IF NOT EXIST "%%~A" MD "%%~A"
    XCOPY /F /Y "%Src%\*%%~A*.txt" "%Dest%\%%~A\" && DEL /Q /F "%Src%\*%%~A*.txt"


Further Resources

  • FOR /F
  • FOR /?

        delims=xxx      - specifies a delimiter set.  This replaces the
                          default delimiter set of space and tab.
        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
        usebackq        - specifies that the new semantics are in force,
                          where a back quoted string is executed as a
                          command and a single quoted string is a
                          literal string command and allows the use of
                          double quotes to quote file names in

  • DEL
  • DIR
  • @Paul .... Please note that this solution requires the file names to be standard as per your example too so emptyCoursesResults_2017-10-08 11 13-21-59.txt means the file name will always have the YYYY-MM-DD portion of the file name come after the first underbar i.e. "_" and also "after the date string there will always be a space". This logic was tested with those two things always being true for these files in the same one folder so "_YYYY-MM-DD ". – Pimp Juice IT Feb 11 at 14:20
  • @Paul ... Lastly and furthermore, I think you could add IF EXIST "%Src%\*%%~A*.txt" to the front of the XCOPY /F /Y "%Src%\*%%~A*.txt" "%Dest%\%%~A\" && DEL /Q /F "%Src%\*%%~A*.txt" line to take care of potentially false positive type errors per the way the logic iterates and it using the wild cards so if needed 8th link down in the script would be IF EXIST "%Src%\*%%~A*.txt" XCOPY /F /Y "%Src%\*%%~A*.txt" "%Dest%\%%~A\" && DEL /Q /F "%Src%\*%%~A*.txt" like that instead. – Pimp Juice IT Feb 11 at 14:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.