Looking for a BAT file to MOVE files into new folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

Looking for a BAT file to MOVE files into new folders

#1 Post by Kerr Avon » 24 Dec 2014 11:08

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.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#2 Post by Yury » 24 Dec 2014 17:26

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#3 Post by foxidrive » 24 Dec 2014 20:44

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\

Kerr Avon
Posts: 29
Joined: 24 Dec 2014 11:00

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

#4 Post by Kerr Avon » 25 Dec 2014 08:02

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.

Post Reply