【问题标题】:Replace string with another string用另一个字符串替换字符串
【发布时间】:2019-05-14 09:28:07
【问题描述】:

我想将当前月份替换为另一个月份,而其他一切都保持不变。 我有一百多个文件,我想在 CMD 中使用重命名功能。

我之前的文件是apple Oct'18 banana.xlsx,我想改成'apple Nov'18 banana.xlsx'

我得到的最接近的是使用Ren *oct*.xlsx "* Nov'18 banana*".xlsx 但我得到的回报是apple Oct Nov banana.xlsx

但是,我无法完全替换 Oct。

【问题讨论】:

  • this 有帮助吗?
  • 哦,这让我想起了这个问题:Rename multiple files name in CMD。我认为仅使用ren 命令是不可能的,我相信您必须进行一些显式的字符串操作...

标签: replace cmd rename


【解决方案1】:

试试ren *.xlsx ??????Nov??????????.*

* 是匹配每个“.xlsx”扩展名的通配符。

? 是一个通配符,可以让原文件中的每个字符都保持不变。

【讨论】:

  • 我在这个文件apple octbanana.xlsx上试过了,它确实有效,但是当我尝试我的其他文件时,它没有。它们前面有不同数量的字符。
  • @chee-seng-ng 查看我在link 找到的这个答案,它可能也会有所帮助。
  • @cheesengng 说只能重命名一个文件。在这里,您重命名所有 xlsx 文件。
【解决方案2】:

由于批处理只知道变量类型的字符串广告不擅长日期时间计算,
我建议为此使用 PowerShell。

随着

  • [日期时间]类型和
  • .AddMonths() 方法
  • 基于正则表达式的-match运算符

它甚至可以跨越年份:

## Q:\Test\2018\12\13\SO_53757192.ps1
$CI = [cultureinfo]::InvariantCulture
$dt = ' MMM\''yy '
Get-ChildItem '* ???''[0-9][0-9] *.xlsx' |
  Where-Object BaseName -match ' (Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)''(\d{2}) ' |
    Rename-Item -NewName {$_.Name -replace $Matches[0],
      [datetime]::ParseExact($matches[0],$dt,$CI).AddMonths(1).ToString($dt)} # -WhatIf

您可以从批处理中作为文件调用它

powershell -NoP -Ex bypass -File ".\SO_53757192.ps1"

或者塞进一行

powershell -NoP -C "$CI=[cultureinfo]::InvariantCulture;$dt=' MMM\''yy ';Get-ChildItem '* ???''[0-9][0-9] *.xlsx'|Where-Object BaseName -match ' (Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)''(\d{2}) '|Rename-Item -NewName {$_.Name -replace $Matches[0],[datetime]::ParseExact($matches[0],$dt,$CI).AddMonths(1).ToString($dt)}"

【讨论】:

    【解决方案3】:

    我认为没有办法通过单个 ren 命令行来完成您的任务。

    您可以使用以下代码编写一个批处理文件并将其命名为ren_date.bat

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "_ROOT=."   & rem // (root directory; `.` is current, `%~dp0.` is parent)
    set "_FROM=%~1" & rem // (name portion to be replaced; `%~1` is 1st argument)
    set "_TO=%~2"   & rem // (name portion to be inserted; `%~2` is 2st argument)
    
    rem // Enumerate all matching files (note the SPACE behind the asterisk):
    for /F "delims=| eol=|" %%J in ('dir /B /A:-D "%_ROOT%\* %_FROM%*.xlsx"') do (
        rem // Store current file name, initialise variable for new name:
        set "FILE=%%J" & set "NAME="
        rem // Toggle delayed expansion to avoid loss of exclamation marks:
        setlocal EnableDelayedExpansion
        rem /* Replace each SPACE in current file name by `" "` and enclose the
        rem    whole string in between `""`, so we get a list of quoted items,
        rem    which reflect all the SPACE-separated file name parts: */
        for %%I in ("!FILE: =" "!") do (
            endlocal
            rem // Store current file name part:
            set "PART=%%~I"
            setlocal EnableDelayedExpansion
            rem /* Assemble new name, performing the intended substitution of the
            rem    predefined sub-strings; the `for /F` loop is needed to transport
            rem    the resulting new file name over the `endlocal` barrier: */
            for /F "delims=| eol=|" %%K in ("!NAME! !PART:%_FROM%=%_TO%!") do (
                endlocal
                rem // Store the assembled name portion:
                set "NAME=%%K"
                setlocal EnableDelayedExpansion
            )
        )
        rem // Now rename file to the assembled new name:
        ren "!_ROOT!\!FILE!" "!NAME:~1!"
        endlocal
    )
    
    endlocal
    exit /B
    

    然后使用以下命令行在cmd 中运行它,假设您位于包含要重命名的文件的目录中:

    ren_date.bat "Oct'18" "Nov'18"
    

    参考my answer to a former question of yours 中的示例,这应该重命名文件:

    and some pears Oct'18_xyz.xlsx
    apples Oct'18.xlsx
    bananas Oct'18.xlsx
    more fruit Oct'18_xyz.xlsx
    oranges Oct'18.xlsx
    plus peaches Oct'18_xyz.xlsx
    strawberries Oct'18 abcdef.xlsx
    

    致这些人:

    and some pears Nov'18_xyz.xlsx
    apples Nov'18.xlsx
    bananas Nov'18.xlsx
    more fruit Nov'18_xyz.xlsx
    oranges Nov'18.xlsx
    plus peaches Nov'18_xyz.xlsx
    strawberries Nov'18 abcdef.xlsx
    

    (我认为直接在cmd 中将上述代码写入命令行是不可能的,特别是由于延迟扩展的切换以使代码安全。)

    【讨论】:

      猜你喜欢
      • 2013-12-03
      • 2017-01-28
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2021-06-15
      • 1970-01-01
      相关资源
      最近更新 更多