Page 1 of 1

Looking for a BAT file to MOVE files into new folders

Posted: 24 Dec 2014 11:08
by Kerr Avon
It's been a long time since I've used DOS (except for getting games running via DOSBox), so I've forgotten almost everything I knew about DOS, but I'm hoping that someone on here will be kind enough to write a BAT file for me (to be run in a DOS window (i.e. not the games program DOSBox, but a Windows prompt box), please.

I'm looking for a BAT file that creates folders (inside the current folder) and moves a given number of files into each sub folder.

What I mean is, say I have a folder

d:\download\Doom\

with lots of .zip files in it, then I'd like a BAT file that I could put into that folder, run it and it would create a folder called

d:\download\Doom\1

and move twenty-five (or any other number, which is editable in the BATch file) files into that folder, the it would create

d:\download\Doom\2

and move twenty-five more files in to that folder, etc, until all of the files have been moved at which point the BATch file ends.

Thanks for any help.

Re: Looking for a BAT file to MOVE files into new folders

Posted: 24 Dec 2014 17:26
by Yury
Kerr Avon wrote:<...> to be run in a DOS window (i.e. not the games program DOSBox, but a Windows prompt box) <...>



:)




Kerr Avon wrote:I'm looking for a BAT file that creates folders (inside the current folder) and moves a given number of files into each sub folder.

Kerr Avon wrote:<...> I'd like a BAT file that I could put into that folder, run it and <...>



Code: Select all

@echo off
setlocal enableextensions

set number=25

for /f "delims=" %%i in ('"2>nul dir/a-d/b"') do (
 if not "%%i"=="%~nx0" (
  set/a x+=1
  for /f %%j in ('"set/a (x-1)/number+1"') do>nul 2>&1 (
   md %%j
   move "%%i" %%j\
   )
  )
 )

endlocal
exit /b

Re: Looking for a BAT file to MOVE files into new folders

Posted: 24 Dec 2014 20:44
by foxidrive
I had this for my own tasks and am just including it here:

Code: Select all

@echo off
::  moves all files into numbered folders of 25 in each
setlocal enabledelayedexpansion
set num=25
set c=999
for %%a in (*) do (
   if not "%%~nxa"=="%~nx0" (
      set /a c=c+1
      set /a d=c %% %num%
      if !d! equ 0 set f=!c!&md !f!
      move "%%a" "!f!" >nul
   )
)
echo moved
pause
goto :EOF


It will create folders like the following:

1025\
1050\
1075\
1100\
1125\
1150\
1175\
1200\
1225\
1250\
1275\
1300\

Re: Looking for a BAT file to MOVE files into new folders

Posted: 25 Dec 2014 08:02
by Kerr Avon
Yury, that is fantastic! I've just tried it and it did exactly what I wanted.Thanks so much for that.

Foxidrive, I've not tried your program, as Yury's worked, but I'll keep yours too, in case Yury's fails (maybe if the filename's of some files are too long, or the extensions are too long, or if Yury's can't cope with more than a given number of files, etc - with PCs you can never be sure of success!).

Thanks both of you for your time and effort, I really appreciate it.