0

I have 16 files that I would like to group together so that there are 3 files per folder (the remainder would be placed in a separate folder). Essentially the files have a date string that I would like to use to group the files. For example, 201301 is January 2013. Is there a way to create a batch program or otherwise to do something like the following:

1) Create list of all strings of the same form as 201301. This can be done by choosing a starting point (in this case 11 characters into the title) to start counting from and then count to 5.

Would something like this fit the bill?

2) Sort these strings from smallest to largest using something like:

:startSort                            // Set our upper "array bound"
 set /a total=count-1

:RestartSort                          // Restart the sort from the beginning
 set /a count=1

:sortLoop
 set /a next=%count%+1                // Swap n and n+1
 call :swap %count% %next%
 set /a count=count+1
 if "%swapped%" == "true" goto :RestartSort // If the variables were swapped,
                                            // start again
 if "%count%" == "%total%" goto :output     // If we're done,
                                            // output the results
 goto :sortLoop                             // Back to the start to
                                            // swap the next two

3) Count to three and then create a folder in which to store the three files

4) store the three files

5) continue until no more files remain

The only issue I have with this setup is that it will probably not know what to do when it gets to the last file that has no other files to be grouped with. Is there a way to get it to account for this - some way it knows it's hit the end of the list?

For reference, the file names are of the form 12345_ABCDE_20130101_20130101, and the folder names would simply be something like 201301-201303 (to use the same example values).

Community
  • 1
  • 1
114
  • 856
  • 3
  • 21
  • 48
  • @Alex I've mainly been searching around to see if pieces like the code for 'sort' have already been placed somewhere. My main concern is more to do with how to create an array that recognizes these 'strings' in the file names as numbers. – 114 May 23 '14 at 18:27
  • can you show example file names and proposed folder names? – Alex May 23 '14 at 19:23
  • @Alex Sure thing, I updated my original question. – 114 May 23 '14 at 19:52

1 Answers1

0

code example:

@ECHO OFF &SETLOCAL disableDelayedExpansion
SET /a Counter=0
SET /a FilesPerFolder=3
FOR /f "delims=" %%a IN ('DIR /b /a-d /on "%cd%\test\*"') DO (
    SET /a Test=Counter%%FilesPerFolder
    SET /a TargetFolder=Counter/FilesPerFolder
    SETLOCAL enableDelayedExpansion
    MD "%cd%\test\Folder!TargetFolder!" 2>nul
    MOVE "%cd%\test\%%~a" "%cd%\test\Folder!TargetFolder!" >nul
    ENDLOCAL
    SET /a Counter+=1
)
Endoro
  • 34,892
  • 8
  • 45
  • 61
  • Thanks! I know this might be excessive, but would it be possible for you to explain by line or in parts what the code does? I'm pretty new to batch. – 114 May 26 '14 at 14:03