I have a list of files like this:
879061063811
879177411209
879177646578
879177927458
879177999903
879196145727
879272322985
879273522242
879273522946
879273527486
879279316113
879297532666
879373182204
879374889040
879378314075
879378411602
879603962878
879874713020
What do I need is an automatic BAT-file which will rename them into a list like this:
89061063811
89177411209
89177646578
89177927458
89177999903
89196145727
89272322985
89273522242
89273522946
89273527486
89279316113
89297532666
89373182204
89374889040
89378314075
89378411602
89603962878
89874713020
As you can see, all I want is get rid of second letter "7". I've cracked my head, then cracked the Google search for a very long time and didn't found the answer...
As I understand cmd doesn't have wildcards for first two letters, that's why I'm not restricting myself to using any cmd utilities. I know in Linux it could be much easier, but cmd is the only choice I have right now.
How would you solve this task?
dir /b /a-d 87*|sed -r "/^[0-9]7[0-9]+$/s/^(.)7(.*)/echo ren \"^&\" \"\1\2\"/e"
Windows的sed解决方案,删除echo
以使其正常运行。
Adjust the regex search string as needed to make sure only the correct files are renamed. FOR /F buffers the entire command output before iterating, unlike simple FOR, so it will never rename the same file twice.
@echo off
setlocal enableDelayedExpansion
pushd someDirectory
for /f "delims=" %%F in ('dir /b /a-d^| findstr /rx 87[0-9]*') do (
set "name=%%a"
ren "%%a" "!name:~0,1!!name:~2!"
)
popd
@echo off
for /F "tokens=1* delims=7" %%a in ('dir 87* /B /A-D') do ren %%a7%%b %%a%%b
For example:
> dir /b /a-d
879061063811
879177411209
879177646578
879177927458
879177999903
MyRen.bat
>MyRen.bat
>dir /b /a-d
89061063811
89177411209
89177646578
89177927458
89177999903
MyRen.bat
If you want to eliminate several different characters in second position, just assemble a list of they and use it at the rigth place. For example, to eliminate digits 0..7 in second position:
@echo off
for /L %%i in (0,1,7) do (
for /F "tokens=1* delims=%%i" %%a in ('dir 8%%i* /B /A-D') do ren %%a%%i%%b %%a%%b
)
However, in this case you should use move
instead of ren
in order to move the renamed files out of the directory, so the next cycles don't rename they again.
Just a few simple steps to follow: -
Run CMD
Paste all txt files which do you want to rename on C:\\
Type the rename command on CMD - REN C:\\89*.txt 87*.txt
I Hope this will solve your problem within a minute.
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.