简体   繁体   中英

Create .BAT file to rename files in current directory with input

I am trying to write a small .bat script (with limited programming knowledge) that will allow users to enter a value for current version number (eg 01) and new version number (eg 02) and then use those two numbers inside a rename command.

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

I think the code is almost right, I am just having troubles with making it reference the current directory that it is run from. I am using this script with people who have limited computer skills, and I am not the network administrator so I cannot really do anything too in depth.

If someone could please help me tweak this then that would be great.

Cheers,

Mat

Edit 1 - Comment response For example, test_01.tab changing to test_02.tab. It basically will replace all _01 parts of the file name and change them to _02. In this case, current = 01 and new = 02. The filenames are all different though, so you may have test_01.bat, testing_01.bat to change, so just renaming the 01 to 02 is how I assumed it would work.

Edit 2 - Current code This is thanks to Stephan below, I believe the only thing it needs to be working correct is for the file path to be removed from the second part of the REN command so that it just has the file and extension and it should work.

@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

Edit 3 - Final Code (works, yay!)

@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

This should do the trick.

@Echo off
set /p current="Current Version: "
set /p new="New Version: "

for /R "c:\your location" %%a in (*_%current%.*) do (
ren *_%current%.* *_%new%.*
)
pause

Below is before and after running the script on a couple of test-files.

在此处输入图片说明 在此处输入图片说明

@Echo off
setlocal enabledelayedexpansion
set /p current="Current Version: "
set /p new="New Version: "

for /R %%a in (*_%current%.*) do (
  set "Name=%%~nxa"
  ECHO ren "%%~a" "!Name:_%current%=_%new%!"
)
pause

(remove ECHO , if the output satisfies you)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM