MATLAB: Renaming batch names removing extensions

MATLABrenaming batches

Hello, I have multiple file names that were accidently labeled with a '.' in them, and my other scripts will no longer recognize the file name correctly. I need to remove the '.' fromt he file names in large batches, about 400 files at a time in a folder.
For example, I need a way to make a batch like this:
1801_pH6.25-pos2-041-seg
1801_pH6.25-pos2-042-seg
1801_pH6.25-pos2-043-seg
1801_pH6.25-pos2-044-seg
into a batch with just the "." removed like this:
1801_pH625-pos2-041-seg
1801_pH625-pos2-042-seg
1801_pH625-pos2-043-seg
1801_pH625-pos2-044-seg
I am new to matlab and have no idea how to accomplish this, any and all help would be much appreciated.
Thank you!

Best Answer

  • Job for command. Make a .cmd batch file from the following--name DORENAME.CMD or somesuch...
    echo off
    setlocal
    for %f in (1801*.25*) do (
    set filename=%~xf%
    set newname=%filename:.=-%
    ren %f% %newname%
    )
    endlocal
    echo on
    Execute the above for each directory containing files...I built in a matching wildcard pattern to your example--if all the files in the subdirectory are affected, then *.* would work just as well. If there are differing names you could pass the appropriate wild card as a parameter and use it instead of hardcoded value.
    Each filename %f% returned from the directory search is save in temporary environment variable and then the new name built by character substitution of the hyphen for the period/dot. Then those are used to RENAME the file.
    You could write similar logic in ML using the DIR() structure but there isn't a direct support to the REN command built into ML so you have to COPYFILE then delete the old one which is an extra step.