# Solved: Batch file: File sorting



## xibit1987 (Oct 14, 2009)

Hi Guys and Girls 

I'm currently working on a very simple batch sorting "program" with drag and drop.

But I have a very strange problem. i can't drag and drop more than 38 files. then it says that i do not have permission to the drive, path or file.

i have checked every thing about the permission problem, and i have all rights to the drive, path and files.

Here is the code:

```
[SIZE=4]@echo off
if [%1]==[] goto :eof
:loop
IF NOT EXIST %~dp1SORTERET\ ( MKDIR %~dp1SORTERET )
IF NOT EXIST %~dp1SORTERET\file%~x1\ ( MKDIR %~dp1SORTERET\file%~x1 )
MOVE /Y %1 %~dp1SORTERET\file%~x1\
shift
if not [%1]==[] goto loop[/SIZE]
```
Basically i want the batch file to create a folder called "SORTERET" in the folder where all the files are.

And in that folder create a new folder with the extension name of the file it is handling, ect. exe.
The the folder must be called "file.exe" or even just "exe", why i want it to be called "file" first, is becaus of some of my files have no extension  so when it is handling the "no extension file" it will just create a folder called "file"
Then i want to move the file to that folder.

It is working as planed. but only for 38 files at a time.
What i want it to do is sorting my mixed files folder with more than 1000 files in it.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

You are limited to 8191 characters + a CR in a command line. The files you drop will include the full path to the file, plus 2 quotes, and a space between each filename. Plus you have to add in the length of the batch file path/name as well.
Here's the result of dropping 4 files from *My Documents\Tech Info* on a *test.cmd* batch file in the *C:\Test Dir* folder. The line length is 313 characters.

```
C:\Test Dir\Test.cmd "C:\Documents and Settings\TheOutcaste\My Documents\Tech Info\DIMM0.txt" "C:\Documents and Settings\TheOutcaste\My Documents\Tech Info\DIMM1.txt" "C:\Documents and Settings\TheOutcaste\My Documents\Tech Info\DIMM2.txt" "C:\Documents and Settings\TheOutcaste\My Documents\Tech Info\DIMM3.txt"
```
If you are several folders deep under My Documents it gets long quite quickly.

Would probably be better to drop the parent folder on the batch file, then use a For loop to run through each file in the folder.
Something like this should work:

```
@Echo Off
If [%1]==[] Goto _Usage
If Not Exist "%~1" Goto _Usage2
If Not Exist %~s1\nul Goto _Usage1
Set _Source=%1
PushD "%_Source%"
For /F "Tokens=1 Delims=" %%A In ('Dir /A-D /B %_Source%') Do (
IF NOT EXIST "%%~dpASORTERET" MKDIR "%%~dpASORTERET"
IF NOT EXIST "%%~dpASORTERET\file%%~xA" MKDIR "%%~dpASORTERET\file%%~xA"
MOVE /Y "%%A" "%%~dpASORTERET\file%%~xA"
)
PopD
Goto :EOF
:_Usage2
Echo Error: %1 does not exist.
Goto _Usage
:_Usage1
Echo Parameter Error: %1 is a file name.
Echo                : Please specify a folder name
:_Usage
Echo.
Echo %~n0 [drive:][path]foldername
Echo.
Echo.  foldername  : Name of the folder to process.
Echo.              : It must be quoted if it contains spaces and is entered
Echo.              : on the Command Line. If dropped on the batch file or it's
Echo.              : shortcut, it will be quoted automatically
Echo.
Pause
```
HTH

Jerry


----------



## xibit1987 (Oct 14, 2009)

You are the man 

Thx so much, now i will go right ahed and test it. And i can see I have much to learn. i was looking into the FOR LOOP, but didn't quite understand it. hope i will some day 

I will return as quickly as i can, and mark it as solved if it works as i planned 

EDIT: It's working like a charm, i can only dream of being just as good as you


----------



## TheOutcaste (Aug 8, 2007)

You're welcome!

Thanks for letting us know it worked.

Jerry


----------



## xibit1987 (Oct 14, 2009)

It was the least i could do, after such a great service from your side


----------

