【发布时间】:2016-04-11 11:29:20
【问题描述】:
我正在尝试编写一个小的 .bat 脚本(编程知识有限),它允许用户输入当前版本号(例如 01)和新版本号(例如 02)的值,然后在里面使用这两个数字重命名命令。
echo off
cls
TITLE Version Updater
echo Use this script to update file names. This works similar to find and replace in other programs.
echo[
echo Enter the current version number (01 for example)
echo off
set /p current="Current Version: "
echo[
echo Enter the new version number (01 becomes 02 etc.)
echo off
set /p new="New Version: "
echo[
echo You will be updating all occurences of "%current%" in your file names to "%new%", if this is correct then continue, if not close this window.
pause
cd %CD%
rename *%current%* *%new%*
pause
我认为代码几乎是正确的,我只是在让它引用运行它的当前目录时遇到了麻烦。我正在与计算机技能有限的人一起使用此脚本,而且我不是网络管理员,所以我无法真正做任何太深入的事情。
如果有人可以帮我调整一下,那就太好了。
干杯,
垫子
编辑 1 - 评论回复 例如,test_01.tab 更改为 test_02.tab。它基本上将替换文件名的所有_01部分并将它们更改为_02。在这种情况下,current = 01 和 new = 02。但是文件名都不同,所以你可能要更改 test_01.bat、testing_01.bat,所以我认为只需将 01 重命名为 02 即可。
编辑 2 - 当前代码 这要感谢下面的 Stephan,我相信它唯一需要正常工作的就是从 REN 命令的第二部分中删除文件路径,以便它只有文件和扩展名,它应该可以工作。
@pushd %~dp0
@Echo off
CLS
setlocal enabledelayedexpansion
set /p current="Current Version: "
set /p new="New Version: "
for /R %%a in (*_%current%*) do (
set "Name=%%~a"
ren "%%~a" "!Name:_%current%=_%new%!"
)
pause
编辑 3 - 最终代码(工作,耶!)
@pushd %~dp0
@echo off
cls
TITLE CEF File Version Updater
echo This script will update CEF file versions for TLS rejections (E.G. 4XXX_XX_1 to 4XXX_XX_2). Backup files prior to running this script as a safety measure.
echo[
echo Enter the current version number (no underscore)
echo off
set /p current="Current Version: "
echo[
echo Enter the new version number (no underscore)
echo off
set /p new="New Version: "
echo[
echo You will be updating all occurences of "_%current%" in your file names to "_%new%", is this correct?
SET /P ANSWER=Do you want to continue (Y/N)?
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
goto :no
:yes
rename *_%current%.* *_%new%.*
CLS
echo Renaming is complete, please check the new file names are correct.
pause
exit /b 0
:no
exit /b 1
【问题讨论】:
-
你能举例说明它应该做什么吗?例如,它应该重命名的文件名是什么? %current% 和 %new% 可能是什么...?
-
例如 test_01.tab 更改为 test_02.tab。它基本上将替换文件名的所有_01部分并将它们更改为_02。在这种情况下,current = 01 和 new = 02。但是文件名都不同,所以你可能要更改 test_01.bat、testing_01.bat,所以我认为只需将 01 重命名为 02 即可。跨度>
-
请通过编辑您的帖子将此附加信息放入问题中!
-
进行了大量测试并为您找到了解决方案。将其发布为答案。
标签: batch-file command