批处理文件重命名“This.is.a.FIlename.mp3”到“tiaf.mp3”

发布于 2024-10-12 18:09:04 字数 344 浏览 4 评论 0原文

好的,我有一个包含一些文件的目录。我想做一个特定的文件重命名脚本 我坚持这部分,只取文件名每个部分的第一个字母:

如果文件名是,

This.is.a.FIle.mp3

我想重命名它以

tiaf.mp3

注意到我希望它全部为小写。

字长是可变的,所以我不能将其作为局部变量进行引用!变量:~0,2!

有人可以帮忙吗? 谢谢!

编辑:我忘了问。如果您有想法进行测试,文件名是否符合我提到的格式。因为如果调用该文件。 'file.mp3' 那么我不希望它被重命名为 'f.mp3'

okay so I have a dir with some files. I want to do a specific file-renamingscript
i'm stuck with this part, taking only the first letter of each part of the filename:

if the filename would be

This.is.a.FIle.mp3

I would like to rename it to

tiaf.mp3

notice i want it to be all in lowercase.

The word length is variable so i cant take reference from it as a local variable !variable:~0,2!

anyone could help?
thanx!

edit: i forggot to ask. If you have an idea to make a test if the filename is of the format i mentioned. Because if the file is called. 'file.mp3' then i wouldn't want it to be renamed to 'f.mp3'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-10-19 18:09:04

这应该可行,但如果您也想允许“!”文件名中带有感叹号,则必须稍微扩展一下。

@echo off
setlocal EnableDelayedExpansion
for %%f in ("C:\temp\folder\*.*") do (
    call :createName "%%~f"
)
goto :eof


:: Compress a filename with more than one dot to only the first (lower) letters of each part
:: one.TWO.three.four.exe to ottf.exe
:createName <dot-filename>
setlocal
set "filename=#.%~n1"
set "ext=%~x1"
set "count=0"
set "short="

:createName.loop
for %%a in ("!filename!") do (
    set "part=%%~xa"
    set "filename=%%~na"

    if defined part (
        set /a count+=1
        set "char=!part:~1,1!"
        call :toLower char
        set "short=!char!!short!"
    ) ELSE (
        set "char="
    )   
    rem echo "%%~na"-"%%~xa" "!char!"  "!short!"
)
if defined part goto :createName.loop
set "short=!short!!ext!"
if !count! GTR 1 (
    echo ren "%~f1" "!short!"
)
(
    endlocal
    goto :eof
)


:: convert a char to the lower variant or leave it unchanged if it isn't a char
:: use the %var:*n=% syntax to remove the front of a string, to get the correct char
:toLower <variable to char>
setlocal EnableDelayedExpansion
(
set "char=!%~1!"
set "helper=##aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"
set "lower=!helper:*%char%=!"
set "lower=!lower:~0,1!"
if "!lower!"=="#" set "lower=!char!"
)
(
    endlocal
    set "%~1=%lower%"
    goto :eof
)

This should work, but if you want to allow also "!" exclamation marks in your filenames, it have to be a little bit extended.

@echo off
setlocal EnableDelayedExpansion
for %%f in ("C:\temp\folder\*.*") do (
    call :createName "%%~f"
)
goto :eof


:: Compress a filename with more than one dot to only the first (lower) letters of each part
:: one.TWO.three.four.exe to ottf.exe
:createName <dot-filename>
setlocal
set "filename=#.%~n1"
set "ext=%~x1"
set "count=0"
set "short="

:createName.loop
for %%a in ("!filename!") do (
    set "part=%%~xa"
    set "filename=%%~na"

    if defined part (
        set /a count+=1
        set "char=!part:~1,1!"
        call :toLower char
        set "short=!char!!short!"
    ) ELSE (
        set "char="
    )   
    rem echo "%%~na"-"%%~xa" "!char!"  "!short!"
)
if defined part goto :createName.loop
set "short=!short!!ext!"
if !count! GTR 1 (
    echo ren "%~f1" "!short!"
)
(
    endlocal
    goto :eof
)


:: convert a char to the lower variant or leave it unchanged if it isn't a char
:: use the %var:*n=% syntax to remove the front of a string, to get the correct char
:toLower <variable to char>
setlocal EnableDelayedExpansion
(
set "char=!%~1!"
set "helper=##aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz"
set "lower=!helper:*%char%=!"
set "lower=!lower:~0,1!"
if "!lower!"=="#" set "lower=!char!"
)
(
    endlocal
    set "%~1=%lower%"
    goto :eof
)
眼波传意 2024-10-19 18:09:04

这种逻辑对您有用吗:

@echo off

for /f "delims=|" %%f in ('dir /b C:\temp') do call :runsub %%f
goto EOF

:runsub
for /f "tokens=1,2,3,4 delims=." %%a in ("%~n1") do set a=%%a&set b=%%b&set c=%%c&set d=%%d

if not "%a%"=="" echo %a%
if not "%b%"=="" echo %b%
if not "%c%"=="" echo %c%
if not "%d%"=="" echo %d%

:EOF

您可以更改 echo %a%、echo %b% 等来设置并从中获取子字符串。这也只获得前 4 个分割,如果需要,您可以添加更多分割。还将 C:\temp 更改为适当的目录。

Would this kind of logic work for you:

@echo off

for /f "delims=|" %%f in ('dir /b C:\temp') do call :runsub %%f
goto EOF

:runsub
for /f "tokens=1,2,3,4 delims=." %%a in ("%~n1") do set a=%%a&set b=%%b&set c=%%c&set d=%%d

if not "%a%"=="" echo %a%
if not "%b%"=="" echo %b%
if not "%c%"=="" echo %c%
if not "%d%"=="" echo %d%

:EOF

You can change the echo %a%, echo %b%, etc. to sets and get the substring from these. This also only gets the first 4 splits, you can add more if you need. Also change C:\temp to the appropriate directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文