【问题标题】:batch add a character(s) in the middle of a filename using cmd使用 cmd 在文件名中间批量添加一个字符
【发布时间】:2020-04-10 08:48:48
【问题描述】:

所以我的文件(主要是文档)的文件名以“YYYYMMDD - 文件的标题”开头 我想将日期格式更改为 YYYY-MM-DD 我想做一个脚本来批量重命名,因为我会时不时地这样做。

我的蝙蝠代码经过 2 天的研究无济于事:

for %%a in ("*.*") do ren *.* ????-??-??*.*
pause

任何帮助或指向我要查看的正确方向都会非常有帮助。谢谢

最终

rem // Loop through all matching files:
for /F "eol=| delims=" %%F in ('
    dir /B /A:-D-H-S "*.*"
') do (
    rem // Store the current file name and extension to variables:
    set "NAME=%%~F"
    rem // Enable delayed expansion to avoid trouble with `!`:
    setlocal EnableDelayedExpansion
    rem // Rename file by applying sub-string expansion:
    ren "!NAME!" "!NAME:~,4!-!NAME:~4,2!-!NAME:~6!!EXT!"
    endlocal
)
pause

【问题讨论】:

  • Do部分内,将元变量%%a保存到变量var,启用延迟扩展,然后将%%a重命名为@987654328 @ 带有日期分隔符 -,插入到第五个位置,然后是第八个位置,然后是 endlocal。
  • 谢谢你告诉我应该怎么写。我不是程序员,只是对事物进行研究。据我从您的评论中了解到的.. 并帮助@wellington 的答案 setlocal EnableDelayedExpansion for %%a in (".") do (set var=%%a) & ren (idk where从这里开始) endlocal 暂停,感谢您清除其他帮助我回答问题的人。是的。我希望文件以“YYYY-MM-DD - File.etc 的标题”结尾
  • 请参阅this answer,它使用相同的方法,请勿使用惠灵顿答案中的任何内容。

标签: date batch-file rename


【解决方案1】:

您正在使用 for 循环,但没有在正文中使用其元变量 %%a,这没有任何意义。

不幸的是,您不能只使用ren,因为新名称中的每个? 都代表该位置上旧名称的字符。

我会这样做:

rem // First change to the target directory:
cd /D "D:\Target\Dir"
rem // Loop through all matching files:
for /F "eol=| delims=" %%F in ('
    dir /B /A:-D-H-S "*.*" ^| findstr /I "^[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]"
') do (
    rem // Store the current file name and extension to variables:
    set "NAME=%%~nF" & set "EXT=%%~xF"
    rem // Enable delayed expansion to avoid trouble with `!`:
    setlocal EnableDelayedExpansion
    rem // Rename file by applying sub-string expansion:
    ren "!NAME!!EXT!" "!NAME:~,4!-!NAME:~4,2!-!NAME:~6!!EXT!"
    endlocal
)

findstr 在这里用于过滤以八位十进制数字开头的文件名,这些数字可能代表一个日期。如果您不想这样做,只需删除从 ^| findstr 到该行末尾的所有内容。

这里需要Delayed variable expansion,因为您在同一个代码块中写入和读取变量。在固定字符位置拆分原始名称由sub-string expansion完成。

【讨论】:

  • 我想如果他们使用由八个字符组成的FindStr 序列,则没有理由删除扩展名并在最后重新应用它,因此只有通过的任何文件名才需要这样做到 Do,它的 BaseName 可以少于六个字符。
  • 真的,@Compo;您可能已经假设我添加了 findstr 过滤器很晚;无论如何,我决定保留其余部分,因为可以选择删除代码下面的句子中提到的过滤器......
  • 谢谢您。我正在努力解决这个问题,但不知何故仍然收到错误消息说The system cannot find the file specified.我有点迷路了。
  • 那么,您是否在代码中指定了您的实际目标目录?您是否尝试过按照说明删除findstr 过滤器?
  • 是的。我什至尝试将 .bat 文件放在目录中,然后从那里跳过你提到的前 2 行代码。
【解决方案2】:

您可以使用此函数将文件重命名为您想要的格式。如果需要,您可以在 FOR 循环中调用此函数。

set old_filename=YYYYMMDD - Title of file.etc
:: Extract only the first 4 characters
SET yyyy=%old_filename:~0,4%
:: Skip 4 characters and then extract the next 2
SET mm=%old_filename:~4,2%
:: Skip 6 characters and then extract the next 2
SET dd=%old_filename:~6,2%
:: Skip 8 characters and then extract everything else
SET everything_else=%old_filename:~8%
:: Rename de old filename to the new
rename "%old_filename%" "%yyyy%-%mm%-%dd%%everything_else%"

【讨论】:

  • 这个问题对我来说似乎很清楚! I have files (mostly documents) with a file name beginning with "YYYYMMDD - Title of file.etc" I'm wanting to change the date format to YYYY-MM-DD 我希望,从我在问题下的评论中可以看出,他们只是希望在每个文件名的两个位置插入一个字符,"YYYY-MM-DD - Title of file.etc"
  • 感谢您向我展示如何操作,但我只想在文件名中插入字符(在本例中为 2 个破折号)。就像@compo 的评论所说的那样。欣赏这一点,其中某处是我正在寻找的一种简单方法
  • 好的,我之前修改了代码来满足这个要求。
【解决方案3】:

Powershell 会更容易做到这一点。使用这个:

@echo off
pushd "The Directory"
powershell -command "& {get-childitem | foreach {$NewName = $_.Name -replace '\d{8}.*', '\d{4}-\d{2}-\d{2}.*'; rename-item -path '.\$_.Name' -newname '$NewName'}}"
popd
goto :eof

【讨论】:

    【解决方案4】:

    右撇子,

    也许你应该看看 Powershell 命令。

    以下powershell命令用下划线替换当前目录下所有文件的空格:

    dir | rename-item -NewName {$_.name -replace " ","_"}

    来源:https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/

    您可以在此处查看如何修改 Powershell 字符串,例如:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convert-string?view=powershell-5.1

    【讨论】:

    • 我不确定这如何回答这个问题,不管是否缺少 [powershell] 标签。 OP 没有用下划线替换空格字符,它们实际上并没有替换任何东西,它们正在添加!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2018-07-12
    相关资源
    最近更新 更多