# Help - Batch/script file - compare file names first few characters



## chat_patta (May 4, 2008)

Hi Guru (male/female):

Need help with script files (vbs/bat) to be run on XP. Want to compare the first 'X' characters of the file name with the rest of the file names in the same directory. X, paths for source directory and path to the matched directory will be entered by the user. So no particular string is being input, but the script itself reads the firs file's name, takes the first X characters and form a string to be compared.

All matching files will be moved to matched directory.

Please note there can be any number matches. Like there can be two, three or n files with the same X number of characters as file name.

I would be grateful if someone can help me write this or point to an application which can perform this particular action, somebody suggested filemonkey/filemonk, i could not figure out how to compare a partial set of characters which are position sesitive in file names.

Here is an example:

To be entered by user:
string length to compare X=3
source directory path = c:\temp1
matched directory path = c:\matched

DATA:
Files in c:\temp1 =
ABCAAA.txt, ABCBBBBB.txt, ABCD.txt, ABDAAA.txt, BCDEF.txt, BCDGHAW.txt, FGACD.txt and BABC.txt


After running the script/batch file

The first file is ABCAAA.txt, so the script take "ABC" from the files name and search the rest of the files first 3 characters with "ABC". Once match found it transfers the all the matched files.

files transfered since comparison ("ABC") was found, c:\matched = ABCAAA.txt, ABCBBBBB.txt, ABCD.txt, BCDEF.txt, BCDGHAW.txt

files left as no comparison found, c:\temp1 = ABDAAA.txt, FGACD.txt and BABC.txt
Note: Because the position (i.e. start of the file name) is important so BABC.txt was not a matched file even though it has ABC in its name.

Much obliged for any help in this regards

P.S. this is not my homework/assignment, i want to use this to sort the fonts and mp3 files. All my native (Hindi mp3s) file Hindi Titles/file names are written in English, and many variations of English characters are used to pronounce the Hindi words using English alphabets. Number of words translated as title varies too, hence almost no two file names are same, the file size differ too (audio quality wise)..... so the only way for me is to search limited characters between file names.


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

This should do what you want. I've only tested it with the sample names you gave, plus added some spaces so names with spaces should work as well.
Still a good idea to test on a copy of your data first.

It will overwrite an existing file in the destination without prompting. That shouldn't be a problem, unless a different file using the same name is created after the original is moved.

It checks that a valid length is entered (0 < length < 254) and checks that the source folder exists. It will create the destination folder if it doesn't exist.

If there is a file with a name shorter than the compare length, it will be skipped. It does not include the file extension in the comparison.

For example, if you had a file named AB.TXT with the ones you've listed, and use a length of 3, it will not be moved.


```
@Echo Off
Setlocal EnableDelayedExpansion
:_LenInvalid
Set /P _cmplen="Enter the string length to compare: "
If NOT %_cmplen% LSS 1 If %_cmplen% LSS 254 Goto _nosource
Echo Invalid length, please re-enter&Goto _LenInvalid
:_nosource
Set /P _srcdir="Enter the source directory path: "
If NOT Exist "%_srcdir%" Echo The source folder %_srcdir% does not exist, please Re-enter&Goto _nosource
Set /P _dstdir="Enter the matched directory path: "
If NOT Exist "%_dstdir%" MD "%_dstdir%"
:_Start
Set /A _cmpchk=_cmplen-1
PushD %_srcdir%
For /F "Tokens=*" %%I In ('dir /B /ON') Do (
Set _fnm=%%~nI
If NOT "!_fnm:~%_cmpchk%,1!"=="" (
  Set _mat=!_fnm:~0,%_cmplen%!
  For /F "Tokens=1 Delims= " %%A In ('dir /ON "!_mat!*.*" 2^>Nul^|Find "File(s)"') Do If %%A GTR 1 @Echo Moving&Move "!_mat!*.*" "!_dstdir!"
  )
)
PopD
```
HTH

Jerry


----------

