I want to write a batch script to rename folders in a directory.
我想编写一个批处理脚本来重命名目录中的文件夹。
The way that would work is, I would have a file that contains names that I would like each folder to be renamed with. So basically the batch script would just pick names from the file (that contains names) and use it to rename each folder.
可行的方法是,我将有一个文件,其中包含我希望每个文件夹重命名的名称。所以基本上批处理脚本只会从文件中选择名称(包含名称)并使用它来重命名每个文件夹。
So if I have 20 folders, 20 names would exist in file to rename each folder.
因此,如果我有20个文件夹,则文件中将存在20个名称以重命名每个文件夹。
What I have so far:
到目前为止我所拥有的:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
The above script didn't give me the desired result.
上面的脚本没有给我想要的结果。
2 个解决方案
#1
Not tested:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
#2
The problem is that the dir /b %old%
command generate a list of files with .txt extension. If you want to rename folders, then include /AD
switch and eliminate the wild-card:
问题是dir / b%old%命令生成扩展名为.txt的文件列表。如果要重命名文件夹,请包含/ AD开关并消除通配符:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))
#1
Not tested:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=*.txt
SET new="c:\Users\user\Desktop\testing.txt"
set counter=0
(for /f "tokens=*" %%f in ('dir /b %old%') do (
ren Read the next name from the redirected input file
set /a counter=counter+1
for /f "tokens=1* delims=:" %%a in ('findstr /R /N "^" "%new%"^|find "!counter!"') do set "newname=%%b"
ren "%%f" "!newname!"
)
#2
The problem is that the dir /b %old%
command generate a list of files with .txt extension. If you want to rename folders, then include /AD
switch and eliminate the wild-card:
问题是dir / b%old%命令生成扩展名为.txt的文件列表。如果要重命名文件夹,请包含/ AD开关并消除通配符:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET new="c:\Users\user\Desktop\testing.txt"
< %new% (for /f "tokens=*" %%f in ('dir /b /AD') do (
ren Read the next name from the redirected input file
SET /P newname=
ren "%%f" "!newname!"
))