Computing.Net > Forums > Programming > Batch file to sort by filename

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to sort by filename

Reply to Message Icon

Name: jbravo
Date: May 1, 2009 at 13:12:17 Pacific
OS: Windows XP pro
CPU/Ram: P4/512
Product: Dell / OPTIPLEX
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 2, 2009 at 01:53:57 Pacific
Reply:

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


0

Response Number 2
Name: jbravo
Date: May 2, 2009 at 09:06:11 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: May 3, 2009 at 05:47:05 Pacific
Reply:

:: 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


0

Response Number 4
Name: jbravo
Date: May 4, 2009 at 06:45:11 Pacific
Reply:

Is that going to work on my f: drive?

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

Thanks


0

Response Number 5
Name: Mechanix2Go
Date: May 4, 2009 at 07:15:26 Pacific
Reply:

:: 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


0

Related Posts

See More



Response Number 6
Name: jbravo
Date: May 4, 2009 at 10:58:15 Pacific
Reply:

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


0

Response Number 7
Name: Mechanix2Go
Date: May 4, 2009 at 11:34:45 Pacific
Reply:

Did you modify it to match your directories and filenames?


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

M2


0

Response Number 8
Name: jbravo
Date: May 4, 2009 at 12:10:56 Pacific
Reply:

yes i did


0

Response Number 9
Name: jbravo
Date: May 4, 2009 at 13:13:58 Pacific
Reply:

i did it using the move command and wildcards

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

works great thanks for your help


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to sort by filename

Batch file to import filenames and search www.computing.net/answers/programming/batch-file-to-import-filenames-and-search/19748.html

Batch file to append created date www.computing.net/answers/programming/batch-file-to-append-created-date/14993.html

batch file to call reg files? www.computing.net/answers/programming/batch-file-to-call-reg-files/8298.html