使用Windows cmd从文件名中删除字符串 [英] Remove string from file name, using windows cmd

查看:630
本文介绍了使用Windows cmd从文件名中删除字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢你们, Magoo提示修复了该问题,我使用以下代码:

Thank you all, Magoo tips fix it , I using the following code:

echo off
cls
setlocal enabledelayedexpansion

for %%f in (*.mov) do (
    set name=%%~nf
    set new_name=!name:*_= !
    echo File renamed from: !name!.mov to:!new_name!.mov
    rename %%f !new_name!.mov
)

这仅在文件名是something_file_name.mov时有效.如果原始文件只有file_name.mov,那么我只能以name.mov结尾 因此,使用REGEX是最佳选择. 我使用的是Ben Personick和Squashman建议的经过调整的版本.

This only works if the file name is something_file_name.mov. If original file is only file_name.mov, I end up it only name.mov So using REGEX is the best option. I'm using a tweaked version of Ben Personick and Squashman suggestions.

@echo off
cls
SET "_Regex=^[0-9]*_"

FOR %%F IN (*.mov) DO (
    ECHO.%%~nF | findstr /R "%_Regex%" >nul && (
        FOR /F "Tokens=1* Delims=_" %%f IN ("%%~nxF") DO (
            MOVE /Y "%%F" "%%g"
        )
    )
)

我有一些名为3424_file_name.mov的文件,需要删除数字直到第一个_,以便获取file_name.mov 最好设置一个要删除的范围,例如[0-9] 喜欢在cmd Windows 7中进行操作.这是我到目前为止所获得的,但是无法正常工作.

I have some files named 3424_file_name.mov and need to remove the numbers until the first _ so to get file_name.mov Even better would be to set a range to remove like [0-9] Like to do it in cmd windows 7. This is what i got so far, but not working.

cls
setlocal enabledelayedexpansion

for %%f in (*.mov) do (
 set name=%%~nf
 set new_name=%name%:*_=x
 echo %new_name%)
 rename %%f %new_name%.mov
 )

谢谢 亚历克斯

推荐答案

这将满足需要.

请注意在文件为只读的情况下如何使用MOVE/Y,因为重命名无法处理重命名只读文件.

Note how I am using MOVE /Y in case the file is read-only, as Rename cannot handle renaming Read-only files.

SETLOCAL
ECHO OFF

SET "_Regex=^[0-9][0-9]*_.*"
SET "_FileGlob=*_*.Mov"
SET "_FilePath=C:\Path"

FOR %%F IN ("%_FilePath%\%_FileGlob%") DO (
    ECHO.%%~nF | FINDSTR /R "%_Regex%" >NUL && (
        FOR /F "Tokens=1* Delims=_" %%f IN ("%%~nxF") DO (
            MOVE /Y "%%~F" "%%~dpF%%~g"
        )
    )
)
ENDLOCAL

这篇关于使用Windows cmd从文件名中删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆