VOGONS


First post, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie

Sorry if this is in the wrong forum, but can someone write me a BATch file that creates folders (inside the current folder) and moves a given number of files into each one.

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 answers.

Reply 1 of 11, by Kreshna Aryaguna Nurzaman

User metadata
Rank l33t
Rank
l33t

md .\1
move file1.ext .\1
move file2.ext .\1
move file3.ext .\1
move file3.ext .\1
(or whatever file name you want to move, up to file25)

md .\2
move file26.ext .\1
move file27.ext .\1
move file28.ext .\1
move file29.ext .\1
(or whatever file name you want to copy, up to file50)

So there you go. Note I'm not using absolute folder name.

Never thought this thread would be that long, but now, for something different.....
Kreshna Aryaguna Nurzaman.

Reply 2 of 11, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie
Kreshna Aryaguna Nurzaman wrote:
md .\1 move file1.ext .\1 move file2.ext .\1 move file3.ext .\1 move file3.ext .\1 (or whatever file name you want to move, up t […]
Show full quote

md .\1
move file1.ext .\1
move file2.ext .\1
move file3.ext .\1
move file3.ext .\1
(or whatever file name you want to move, up to file25)

md .\2
move file26.ext .\1
move file27.ext .\1
move file28.ext .\1
move file29.ext .\1
(or whatever file name you want to copy, up to file50)

So there you go. Note I'm not using absolute folder name.

Yes, but that means not only an awful lot of typing, but that the files have to be named exactly as they are in the BAT file. It would be much quicker to manually move the files than to write such a BATch file, and I want a BATch file that I can use again and again for any files of any type.

I'm sure this can be done using the FOR command, to access each file individually, but it's so long since I've use FOR (or any DOS except to get games working in DOSBox) that I can't remember how to do it, and I'm hoping someone on here can help.

Reply 3 of 11, by Jepael

User metadata
Rank Oldbie
Rank
Oldbie

Why not use some kind of file manager (Norton Commander lookalikes) to move files around?

I have to ask what is it that you are trying to do here? Split bunch of files to fit on a floppy or something?

Reply 4 of 11, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie
Jepael wrote:

Why not use some kind of file manager (Norton Commander lookalikes) to move files around?

You mean my manually moving the files with Norton Commander, or using an automatic function in NC that Windows explorer doesn't have? Sorry, but I can't remember anything about Norton Commander, though I'm pretty sure I have used it, long ago?

I have to ask what is it that you are trying to do here? Split bunch of files to fit on a floppy or something?

No, my download folders (and my I-might-need-this-file-someday) folders are a total mess (my own fault) with thousands of files in various folders, and it would be a start towards sorting them if I can at least get the larger folders sorted into subfolders, and I thought this would be easy to do, but I can't work out how to get this done via a batch file.

Reply 5 of 11, by bristlehog

User metadata
Rank Oldbie
Rank
Oldbie

Made this in Turbo C++ 1.0.

The only thing is: this one uses copy instead of move, since Dosbox doesn't support 'move' command and I developed this in Dosbox.

Hope it helps. Note that this is for DOS without long file names, if you need the functionality for Windows, it has to be rewritten.

Also, it ignores directories found in current directory and works only with files.

Code:

#include <dir.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
struct ffblk ffblk;
char* myname;
char cmd_string[255];
char dir_name[12];
unsigned int dir_counter;
unsigned int files_per_dir;
unsigned int file_counter;
unsigned int dir_created;

fprintf(stderr, "\nSCATTER v.1.0 - this is for Kerr Avon.\n\n");

if(argc < 2)
{
fprintf(stderr, "Syntax: SCATTER <number of files per directory>\n\n");
fprintf(stderr, "Breaks files from current directory into directories 1, 2, etc.\n\n");
return(-1);
}

files_per_dir = atoi(argv[1]);

if(files_per_dir == 0)
{
fprintf(stderr, "\nNumber of files per directory cannot be equal to zero.\n\n");
return(-1);
}

// What is this program file's name?

if((myname = strrchr(argv[0], '\\')) == NULL)
{
myname = argv[0];
}
else
{
myname++;
}

dir_counter = 1;
dir_created = 0;
file_counter = 0;

fprintf(stderr, "Working...\n");

if(findfirst("*.*", &ffblk, 0) == 0)
{
do
{
if((strcmp(ffblk.ff_name, myname) != 0) && !(ffblk.ff_attrib & 0x10))
{ // current entry is not the program itself and not a directory
if(dir_created == 0)
{
sprintf(dir_name, "%u", dir_counter);
sprintf(cmd_string, "mkdir %s >nul", dir_name);
system(cmd_string);
Show last 25 lines
					dir_created = 1;
}

sprintf(cmd_string, "copy %s %s\\ >nul", ffblk.ff_name, dir_name);
system(cmd_string);
file_counter++;

if(file_counter == files_per_dir)
{
file_counter = 0;
dir_counter++;
dir_created = 0;
}
}
} while(findnext(&ffblk) == 0);
}
else
{
fprintf(stderr, "Error opening current directory.\n");
}

fprintf(stderr, "Done. Used %u directories.\n\n", dir_counter);
return 0;
}

Here you can get fantastic wallpapers created by a friend of mine: patreon.com/Unpocodrillo

Reply 6 of 11, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie
bristlehog wrote:
Made this in Turbo C++ 1.0. […]
Show full quote

Made this in Turbo C++ 1.0.

The only thing is: this one uses copy instead of move, since Dosbox doesn't support 'move' command and I developed this in Dosbox.

Hope it helps. Note that this is for DOS without long file names, if you need the functionality for Windows, it has to be rewritten.

Also, it ignores directories found in current directory and works only with files.

Thanks for that mate. It's great, but I'm afraid most of the downloads I have are in long filename format (it's all sort of stuff - game mods, various programs and utilities, etc).

Reply 7 of 11, by bristlehog

User metadata
Rank Oldbie
Rank
Oldbie

It's a problem (Turbo C++ 1.0 cannot work with LFN), but it can be solved. Do other conditions (DOS platform, copy instead of move and skipping the directories) suit you? Would you prefer Windows console version?

Here you can get fantastic wallpapers created by a friend of mine: patreon.com/Unpocodrillo

Reply 8 of 11, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie

Someone over at http://www.dostips.com/forum/viewtopic.php?f=3&t=6138 wrote a batch file that does what I wanted. To use it, simply copy and paste the test from between the two rows of asterisks (but not the rows of asterisks themselves, of course) into a new (empty) text file, rename the text file to something like

BATCHMOVE.BAT

(call it what you like, but the extenstion must be .BAT), alter the line

set number=25

to whatever number of files you want each folder to contain, and copy the .BAT file to the folder that contains the files you want sorted, and run the .BAT file.

*************************************************************************************

@echo off
rem
rem This program creates folders and moves n number of
rem files into each folder.
rem
rem Alter the variable 'number' to your choice.
rem
rem Copyright Yury

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

*************************************************************************************

Thanks and credit to Yury for the program.

bristlehog wrote:

It's a problem (Turbo C++ 1.0 cannot work with LFN), but it can be solved. Do other conditions (DOS platform, copy instead of move and skipping the directories) suit you? Would you prefer Windows console version?

Many thanks, mate, but it's no longer necessary. It is very good of you to offer, though, and I really appreciate it.

Reply 9 of 11, by RacoonRider

User metadata
Rank Oldbie
Rank
Oldbie

Heh two best solutions both from the Russian guys 😀

Reply 10 of 11, by SKARDAVNELNATE

User metadata
Rank Oldbie
Rank
Oldbie
Kerr Avon wrote:

Yes, but that means not only an awful lot of typing, but that the files have to be named exactly as they are in the BAT file.

If you ever need the names of all the files in a folder quickly and easily you can do this.

REM replace <path> with desired folder
dir "<path>" /S > c:\List.txt
notepad c:\List.txt
del c:\List.txt

Reply 11 of 11, by Kerr Avon

User metadata
Rank Oldbie
Rank
Oldbie
SKARDAVNELNATE wrote:
If you ever need the names of all the files in a folder quickly and easily you can do this. […]
Show full quote
Kerr Avon wrote:

Yes, but that means not only an awful lot of typing, but that the files have to be named exactly as they are in the BAT file.

If you ever need the names of all the files in a folder quickly and easily you can do this.

REM replace <path> with desired folder
dir "<path>" /S > c:\List.txt
notepad c:\List.txt
del c:\List.txt

Thanks, that's great to know!