Reputation: 121
I have tried to rename all the filenames using batch script. But the file names are changed partially with previous filename when I am using the below script.
Batch:
@ECHO OFF
Title Renamer
color 1a
setlocal enabledelayedexpansion
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
echo Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN%
ECHO.
ECHO.
set /p input=Enter Your Path:
set /p ext=Ext:
set /p rename=Rename with:
cd /d %input%
set i=0
FOR %%F IN (%ext%) DO (
set /a i=i+1
ren %%F %rename%_!i!%ext%
)
ECHO Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN% on Date: %date:~7,2%-%date:~4,2%-%date:~10,4% Time: %HR%:%time:~3,2%:%time:~6,2% Source Folder: %input% Extension: %ext% Rename with: %rename% >> renamed_by-%USERNAME%-%ComputerName%_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%%time:~6,2%.log
Output:
Enter Your Path: d:\pathtochange\
Ext: *.xhtml
Rename with: chapter
Input filenames are:
chap_01_intro.xhtml
chap_01.xhtml
chap_02.xhtml
chap_03.xhtml
chap_04_intro.xhtml
chap_04.xhtml
chap_05.xhtml
But after the execute the script, the output shows
chapter_1.xhtml
chapter_2ntro.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6ntro.xhtml
chapter_7.xhtml
But I want the filenames should be changed
chapter_1.xhtml
chapter_2.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6.xhtml
chapter_7.xhtml
Why it's renamed like this?
How to find the filenames rename it by user prompt?
Upvotes: 1
Views: 112
Reputation: 2710
Here is a complete sample:
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
setlocal enabledelayedexpansion
set increment=0
FOR /R %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo rename %%a %prefix%_!increment!%%~xa
)
endlocal
Note: remove echo
command in front of rename
if it's Okay
@echo off
rem :: set undo to true if you need to generate a reverse batch file.
set "undo=true"
set "logfile=true"
for /F "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
set "timestamp=%YYYY%%MM%%DD%-%HH%%Min%%Sec%"
if /i "%undo%"=="true" (
echo @echo off
echo rem :: user: %USERNAME%
echo rem :: Date: %DATE% %TIME%
echo rem :: Source folder: %input%
echo rem :: Extension: %suffix:.=%
echo rem :: renamed to: %prefix%
echo rem :: to undo file renaming: undo-renaming-%timestamp%.bat
)>undo-renaming-%timestamp%.bat
setlocal enabledelayedexpansion
set increment=0
(
for /r %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo The file: "%%a" was renamed to "%prefix%_!increment!%%~xa"
rename "%%a" "%prefix%_!increment!%%~xa"
if "%undo%"=="true" (
echo rename "%%~dpa%prefix%_!increment!%%~xa" "%%~nxa"
)>>undo-renaming-%timestamp%.bat
)
)>reverse-rename-%timestamp%.log
if /i "%logfile%" neq "true" del reverse-rename-%timestamp%.log
endlocal
Note: In the final script, we can push the scenario farther and check if the file Undo is present and then make restoration automatically.
Upvotes: 0
Reputation: 23721
It seems likely to be caused by the fact that the destination name in your ren
command contains a wildcard (from `%ext%).
I'd suggest having the user enter just the extension (i.e. xhtml
rather than *.xhtml
) then add the necessary wildcard in the FOR
command only.
Alternatively you could use %%~xF
instead of %ext%
in the ren
command which should achieve the same effect.
Upvotes: 1