Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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

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

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

Is that going to work on my f: drive?
How would I control where this is going to sort to or from?
Thanks

:: get fourth char of filename and move to folder
@echo off & setLocal EnableDelayedExpansion
pushd c:\files
set dest=f:\some\other\placefor /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

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

Did you modify it to match your directories and filenames?
=====================================
If at first you don't succeed, you're about average.M2

i did it using the move command and wildcards
ex. move /y f:\???0.mp3 f:\0
works great thanks for your help

![]() |
![]() |
![]() |

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