1.

Solve : Batch remove - and . fixed length many suffix?

Answer»

I have to rename thousands of ???files with the nonstandard characters "." and "-". Fixed length, nothing else in folder, only the number changes, same DATE & system per folder, can be folder specificic, and multiple suffix, ie:
2011-05-21sys-03-99.new.test_0001.scene.fixed.doc
2011-05-21sys-03-99.new.test_0001.scene.fixed.xls
2011-05-21sys-03-99.new.test_0002.scene.fixed.doc
2011-05-21sys-03-99.new.test_0002.scene.fixed.xls
need to be
2011_05_21sys_03_99_new_test_0001_scene _fixed.doc
2011_05_21sys_03_99_new_test_0001_scene _fixed.xls
2011_05_21sys_03_99_new_test_0002_scene _fixed.doc
2011_05_21sys_03_99_new_test_0002_scene _fixed.xls
or with no spaces instead of "_".

ren 2011-??-??-??-??..test_*.scene.fixed.doc 2011_??_??_??_??_new_test_*_scene_fixed.doc
ren 2011-??-??-??-??..test_*.scene.fixed.doc 2011_??_??_??_??_new_test_*_scene_fixed.xls

seems to work, but does not like the number wild card, and only works on one file. I tried a couple FOR loops, something like

FOR %%*doc IN (C:\Temp) DO (ren "%%*.doc" 2011-??-??-??-??..test_*.scene.fixed.doc 2011_??_??_??_??_new_test_*_scene_fixed.doc)
but I am sure I have butchered this code.

Any pointers on how to get the number wild card to work or how to get the batch to work? Do you think the FOR loop is appropriate for this? Thanks.So you want the renamed filenames to be the same except that all dots and dashes BECOME underscores? Not including the dot which starts the extension?

Quote

Do you think the FOR loop is appropriate for this? Thanks.

Absolutely. What I would do is enable delayed expansion and in a loop, READ in each filename, then using the ~n and ~x variable modifiers, split it into base name and extension, take the base name and use string replace (twice, once to replace . with _ and again to replace - with _) then add the extension back and use the old name and new name in a REN command. Unfortunately it is 7 AM here and I am gong to work so I won't be able to actually post some example code until around 9 hours time. Maybe somebody else will beat me to the punch? (Not you, Bill!)



Quote from: Salmon Trout
Maybe somebody else will beat me to the punch?

Nobody would dare!!!

Ugh, I don't know why the little smiley faces showed up all over my post!

Thanks, Salmon Trout, that is the solution. I actually posted the same question on another forum and code using the filename parameter extensions (ie, %~n and %~a) were used in an FOR loop. I'll have to brush up on the use of extensions.

I give credit to Salmon Trout and the other person with code for the solution. I'd post the other person's website and username, but I don't know how kosher that is with you all or him/her. Either way, you all have created a true believer in DOS-Batch. Thanks again.Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ( ' dir /b ' ) do (
set oldbasename=%%~nA
set extension=%%~xA
set newbasename=!oldbasename:.=_!
set newbasename=!newbasename:-=_!
echo Renaming "!oldbasename!!extension!" to "!newbasename!!extension!"
REN "!oldbasename!!extension!" "!newbasename!!extension!"
)
echo.
echo All done
pause

Quote from: AndrewJW on June 13, 2011, 08:22:54 AM
I don't know why the little smiley faces showed up all over my post!

Unless you show text as code, this forum shows VARIOUS character combinations as EMOTICONS to all guests and all registered users who have not have selected "No Smileys" as the "Current Smiley Set" in their Profile Look and Layout Preferences. These are 3 question marks: If you see that as that *censored* stupid bewildered emoticon, that is why. This is a figure 8 and an end bracket

I really hate emoticons.




Discussion

No Comment Found