Batch file to sort by filename

Dell / OPTIPLEX
May 1, 2009 at 13:12:17
Specs: Windows XP pro, P4/512
I am trying to organize files on an SD card in to a hash table by the last character in the filename, before the extension. Each filename only contains numbers and is 4 digits long. What I want to do is move each file into the correct directory of the hash.

I am looking at this for the process.
Create the directories that make up the hash table. > copy the files I want to the SD card > sort them into the proper directory by filename.

How would I filter out the first 3 digits of the filename to sort them correctly. I have seen a switch for sort that looks like this sort /+n where n is the number of characters to skip before sorting.

Does sort even work on filenames or extensions?

Thanks in advance for the help.


See More: Batch file to sort by filename

Report •


#1
May 2, 2009 at 01:53:57
I'm not with you on the hash. As to SORT, to sort on 4th char in filename:

dir/b | sort /+4

As to sending them to different folders, you can easily get the 4th char of filename with substring handling.

For your files where all have 4 digit names like xxxx.ext where x is a number:

:: get fourth char of filename

@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
set name=%%~Na
echo !name!
set fourth=!name:~3,1!
echo !fourth!
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
May 2, 2009 at 09:06:11
For the hash, basically I am using 10 directories named 0-9 to sort the files into.

ex. filename 1234.ext would go to folder 4.

This shortens the time it takes to locate the file. The batch is necessary because I am dealing with hundreds of files. I will try that when I get back to work on monday.

If you were wonderig this is for an embedded system using a risc processor but the sd card prep would be done on a PC hence the batch file.

Thanks


Report •

#3
May 3, 2009 at 05:47:05
:: get fourth char of filename and move to folder

@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
set name=%%~Na
set fourth=!name:~3,1!
move %%a !fourth!
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
May 4, 2009 at 06:45:11
Is that going to work on my f: drive?

How would I control where this is going to sort to or from?

Thanks


Report •

#5
May 4, 2009 at 07:15:26
:: get fourth char of filename and move to folder

@echo off & setLocal EnableDelayedExpansion

pushd c:\files
set dest=f:\some\other\place

for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
set name=%%~Na
set fourth=!name:~3,1!
move %%a !dest!\!fourth!
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
May 4, 2009 at 10:58:15
It doesnt work I get:

the system cannot find the file specified
the system cannot find the file specified
the batch file cannot be found


Report •

#7
May 4, 2009 at 11:34:45
Did you modify it to match your directories and filenames?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
May 4, 2009 at 12:10:56
yes i did

Report •

#9
May 4, 2009 at 13:13:58
i did it using the move command and wildcards

ex. move /y f:\???0.mp3 f:\0

works great thanks for your help


Report •


Ask Question