我想将当前月份替换为另一个月份,而其他所有内容保持不变。 我有一百多个文件,我曾考虑过在CMD中使用重命名功能。
我之前的文件是apple Oct'18 banana.xlsx
,我想更改为'apple Nov'18 banana.xlsx'
我最近得到的是使用Ren *oct*.xlsx "* Nov'18 banana*".xlsx
但是我得到的结果是apple Oct Nov banana.xlsx
但是,我无法完全替换Oct。
答案 0 :(得分:2)
尝试ren *.xlsx ??????Nov??????????.*
*
是通配符,用于匹配每个“ .xlsx”扩展名。
?
是通配符,用于使原始文件中的每个字符保持不变。
答案 1 :(得分:2)
我认为无法通过单个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
中将上述代码写到命令行中,尤其是由于切换了延迟扩展以使代码安全。)
答案 2 :(得分:0)
由于批次仅知道变量类型字符串广告在日期时间计算上不好,
我建议为此使用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)}"