1.

Solve : Batch to move files in a single directory into separate folders in groups of 10?

Answer»

I have a directory with almost 500 pdfs in it.  They are all named/numbered sequentially with a 3 digit numeral in the 8th, 9th, and 10th place in the file NAME.  I would like to create a batch that moves those files into separate folders in groups of 10.  So when it was over, I'd have 50 or so folders named "001-010", "011-20", etc. with the corresponding files placed in each.  I could obviously do this manually, but I'll have a new batch of files every week that I will need to do this for.  The other variable is that there won't necessarily be the same number of files every week.  This week I may have 488, next week I may have 506 and so on and so forth.  I'd appreciate any help!  Thanks.Could you give some typical file names, obfuscating non-essential parts if you like?

******_001_**_****_***_*****_*****.pdf

This is an example of a typical file name.  Like I said, the 8th to 10th spot is the counter.  The rest are all variable, but the files will all follow this format, including number of characters.  Thanks.So in theory the 3 digit section could go from 000 up to 999? Or is it more restricted than that?
And you want to classify them into decades potentially from "001-010" ... "991-999" is going to have only 9 possible members...

You are correct, except there will be no 000.  The first number will always be 001.  And while in theory it will go up to 999, most likely it will top out in the 500 range. The number scheme is easy enough, I show the first 3 and the last 3 possible decades; Directories only to be created if there are files to go in them? Is that right?

Directory name: 001-010
Files [if they are found]:
******_001_**_****_***_*****_*****.pdf
******_002_**_****_***_*****_*****.pdf
******_003_**_****_***_*****_*****.pdf
******_004_**_****_***_*****_*****.pdf
******_005_**_****_***_*****_*****.pdf
******_006_**_****_***_*****_*****.pdf
******_007_**_****_***_*****_*****.pdf
******_008_**_****_***_*****_*****.pdf
******_009_**_****_***_*****_*****.pdf
******_010_**_****_***_*****_*****.pdf
Directory name: 011-020
Files [if they are found]:
******_011_**_****_***_*****_*****.pdf
******_012_**_****_***_*****_*****.pdf
******_013_**_****_***_*****_*****.pdf
******_014_**_****_***_*****_*****.pdf
******_015_**_****_***_*****_*****.pdf
******_016_**_****_***_*****_*****.pdf
******_017_**_****_***_*****_*****.pdf
******_018_**_****_***_*****_*****.pdf
******_019_**_****_***_*****_*****.pdf
******_020_**_****_***_*****_*****.pdf
Directory name: 021-030
Files [if they are found]:
******_021_**_****_***_*****_*****.pdf
******_022_**_****_***_*****_*****.pdf
******_023_**_****_***_*****_*****.pdf
******_024_**_****_***_*****_*****.pdf
******_025_**_****_***_*****_*****.pdf
******_026_**_****_***_*****_*****.pdf
******_027_**_****_***_*****_*****.pdf
******_028_**_****_***_*****_*****.pdf
******_029_**_****_***_*****_*****.pdf
******_030_**_****_***_*****_*****.pdf


...


Directory name: 971-980
Files [if they are found]:
******_971_**_****_***_*****_*****.pdf
******_972_**_****_***_*****_*****.pdf
******_973_**_****_***_*****_*****.pdf
******_974_**_****_***_*****_*****.pdf
******_975_**_****_***_*****_*****.pdf
******_976_**_****_***_*****_*****.pdf
******_977_**_****_***_*****_*****.pdf
******_978_**_****_***_*****_*****.pdf
******_979_**_****_***_*****_*****.pdf
******_980_**_****_***_*****_*****.pdf
Directory name: 981-990
Files [if they are found]:
******_981_**_****_***_*****_*****.pdf
******_982_**_****_***_*****_*****.pdf
******_983_**_****_***_*****_*****.pdf
******_984_**_****_***_*****_*****.pdf
******_985_**_****_***_*****_*****.pdf
******_986_**_****_***_*****_*****.pdf
******_987_**_****_***_*****_*****.pdf
******_988_**_****_***_*****_*****.pdf
******_989_**_****_***_*****_*****.pdf
******_990_**_****_***_*****_*****.pdf
Directory name: 991-999
Files [if they are found]:
******_991_**_****_***_*****_*****.pdf
******_992_**_****_***_*****_*****.pdf
******_993_**_****_***_*****_*****.pdf
******_994_**_****_***_*****_*****.pdf
******_995_**_****_***_*****_*****.pdf
******_996_**_****_***_*****_*****.pdf
******_997_**_****_***_*****_*****.pdf
******_998_**_****_***_*****_*****.pdf
******_999_**_****_***_*****_*****.pdf



That is exactly what I'm looking for.Note: code below is a quick hack, it seems to work, please try it out on test data! I have to go now and 1. wash the dishes 2. shower 3. get a whisky and sit with the family and watch The Walking Dead 4. go to bed so give it a whirl and report back any issues. Back tomorrow evening in about 20 HOURS I am in Europe and it is now 8:56 PM. Place it in the folder where the PDFs are, which is where the new folders will be created. If you re-run it later with different files it will place them in these folders according to the 3 digit number and create more folders if needed. It does not check for name collisions in the destination folder or anything else fancy like that.

I created a folder and put in it 567 files with this format

Abcdef_555_gH_ijkl_mno_pqrst_uvWXy.pdf

I ran this batch

echo off
setlocal enabledelayedexpansion
for /l %%A in (10,10,1000) do (
    set n1=%%A
    set /a n1=n1-9
    set n2=%%A
    set dn1=00!n1!
    set dn2=00!n2!
    set dn1=!dn1:~-3!
    set dn2=!dn2:~-3!
    if "!dn2!"=="000" set dn2=999
    Set DirName=!dn1!-!dn2!
    for /L %%B in (!n1!,1,!n2!) do (
        set fn=00%%B
        set fn=!fn:~-3!
        if not "!fn!"=="000" (
            for %%C in (*.pdf) do (
                set fname=%%C
                set fnum=!fname:~7,3!
                if "!fnum!"=="!fn!" (
                    if not exist !Dirname! md !dirname!
                    echo Moving file: !fname! to Folder: !Dirname!
                    move !fname! !Dirname!
                    )
                )
            )
        )
    )

and it moved those files into 57 folders, names starting at 001-010 and ending at 561-570, in 20 seconds, screen output like this

They were only small files, bigger ones may TAKE longer overall

Moving file: Abcdef_548_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 541-550
        1 file(s) moved.
Moving file: Abcdef_549_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 541-550
        1 file(s) moved.
Moving file: Abcdef_550_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 541-550
        1 file(s) moved.
Moving file: Abcdef_551_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 551-560
        1 file(s) moved.
Moving file: Abcdef_552_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 551-560
        1 file(s) moved.
Moving file: Abcdef_553_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 551-560
        1 file(s) moved.
Moving file: Abcdef_554_gH_ijkl_mno_pqrst_uvWXy.pdf to Folder: 551-560

note that this post was edited:

This is another way to do it: like Salmon Trout's solution it will have problems with filenames that contain ! characters.

Code: [Select]echo off
setlocal enabledelayedexpansion
set c=0
for /L %%a in (1001,1,1999) do (
   set /a counter=c %% 10, num2=c+10, c=c+1
   set "num=%%a"
   set "num=!num:~-3!"
   set "num2=000!num2!"
   set "num2=!num2:~-3!"
         if !counter! EQU 0 rd "!folder!" 2>nul & set "folder=!num!-!num2!" & MD "!folder!" 2>nul
         if exist "??????_!num!_*.pdf" (
            echo moving file !num! to !folder!
    move "??????_!num!_*.pdf" "!folder!" >nul
         )
)
rd "!folder!" 2>nul
Quote from: GWJ3 on August 25, 2014, 11:36:23 AM

******_001_**_****_***_*****_*****.pdf

This is an example of a typical file name.  Like I said, the 8th to 10th spot is the counter.  The rest are all variable, but the files will all follow this format, including number of characters.  Thanks.
Weird.  That looks exactly like the file naming we use for our PDF's where I work..The OP has not indicated if he/she still needs a solution. Anyhow, this is how I would do it these days; I filled a folder with 999 pdf files, of mixed sizes from 29 KB to 14 MB, named them with the number part from 001 to 999. It should be saved as a .vbs file in the folder to be processed. DOUBLE click to run, or create a shortcut somewhere



strHost = Right(WScript.FullName, 11)
strHost = LCase(strHost)
If strHost <> "wscript.exe" Then
        Set objShell = CreateObject("WScript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strCmdLine = Chr(34) & objFSO.BuildPath(WScript.Path, "wscript.exe") & Chr(34)
        strCmdLine = strCmdLine & " "
        strCmdLine = strCmdLine & Chr(34) & WScript.ScriptFullName & Chr(34)
        objShell.Run strCmdLine
        Set objShell = Nothing
        Set objFSO = Nothing
        WScript.Quit
End If
Set fso        = CreateObject("Scripting.FileSystemObject")
ThisFolder     = fso.GetParentFolderName(Wscript.ScriptFullName)
Set objFolder  = fso.GetFolder(ThisFolder)
Set AllFiles   = objFolder.Files
FilesMoved     = 0
FoldersCreated = 0
Ssec=DateDiff("s", "01/01/1970 00:00:00", Now)
For Each File in AllFiles
    FileName = File.Name
    FileExt  = fso.GetExtensionName(File)
    If lcase(FileExt) = "pdf" Then
        NumPart = Mid(Filename, 8,3)
        DS      = 1+(Int((NumPart-1)/10)*10)
        DE      = DS + 9
        If DE = 1000 Then DE = 999
        FolderName   = Right(DS + 1000, 3) & "-" & Right(DE + 1000, 3)
        FolderExists = fso.folderExists(FolderName)
        If Not folderExists Then
           fso.CreateFolder(FolderName)
           FoldersCreated = FoldersCreated + 1
        End If
        fso.movefile FileName, FolderName & "\"
        FilesMoved = FilesMoved + 1
    End If
Next
Esec=DateDiff("s", "01/01/1970 00:00:00", Now)
Elapsed = Esec - Ssec
MsgText = FilesMoved & " Files moved " & vbcrlf & FoldersCreated & " Folders created" & vbcrlf
MsgText = MsgText & "Elapsed: " & Elapsed & " second(s)" & vbcrlf
MsgBox MsgText, 0, "Script finished"



Discussion

No Comment Found