Moving a large number of files in one directory to multiple directories

Posted by Axsuul on Stack Overflow See other posts from Stack Overflow or by Axsuul
Published on 2010-03-29T23:14:33Z Indexed on 2010/03/29 23:23 UTC
Read the original article Hit count: 342

Filed under:
|

I am looking to create a Windows batch script to move about 2,000 files and splitting them up so that there are 10 files per folder. I have attempted creating a batch script but the syntax really boggles my mind. Here is what I have so far

@echo off

:: Config parameters
set /a groupsize = 10
:: initial counter, everytime counter is 1, we create new folder
set /a n = 1
:: folder counter
set /a nf = 1

for %%f in (*.txt) do (
:: if counter is 1, create new folder
if %n% == 1 (
    md folder%nf%
    set /a n += 1
)

:: move file into folder
mv -Y %%f folder%nf%\%%f

:: reset counter if larger than group size
if %n% == %groupsize% (
    set /a n = 1
) else (
    set /a n += 1
)
)
pause

Basically what this script does is loop through each .txt file in the directory. It creates a new directory in the beginning and moves 10 files into that directory, then creates a new folder again and moves another 10 files into that directory, and so on. However, I'm having problems where the n variable is not being incremented in the loop? I'm sure there's other errors too since the CMD window closes on me even with pause. Any help or guidance is appreciated, thanks for your time!

© Stack Overflow or respective owner

Related posts about batch

Related posts about batch-file