windows Windows批处理给文件名添加前缀,为什么要添加两次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27963060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch to add prefix to file names, why adds twice?
提问by Marco Demaio
In order to add a simple "hello"prefix to all pdf files in a folder I'm using this batch file:
为了给文件夹中的所有 pdf 文件添加一个简单的“hello”前缀,我使用了这个批处理文件:
FOR %%F IN (*.pdf) DO (RENAME "%%F" "hello%%F")
Saved this into a "rename.bat" file and placed it into the folder I need the files to be renamed. Then I just double click on "rename.bat".
将其保存到“rename.bat”文件中并将其放入我需要重命名文件的文件夹中。然后我只需双击“rename.bat”。
This almost works but the 1st file gets the prefix added twice.
这几乎有效,但第一个文件添加了两次前缀。
Let's say in the folder I have: A.pdf, B.pdf, C.pdf, they get converted into:
假设在我有的文件夹中:A.pdf、B.pdf、C.pdf,它们被转换为:
- hellohelloA.pdf
- helloB.pdf
- helloC.pdf,
- 你好你好A.pdf
- 你好B.pdf
- 你好C.pdf,
Do you know what's wrong in the batch file?
你知道批处理文件有什么问题吗?
I noticed it always does this when files are more than one. It works ok when there is only one file in the folder, but it is not very useful :-).
我注意到当文件不止一个时它总是这样做。当文件夹中只有一个文件时它工作正常,但它不是很有用:-)。
回答by Alex K.
回答by Keybrd Cowboy
@echo off
echo.
echo. Add Whatever Prefix...
echo.
echo. You Want To Add...
echo.
echo. To The Filename...
echo.
set /p variable=" > "
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' dir /b /a-d *.pdf') do (
set oldName=%%a
Set newName=%variable%!oldName!
Ren "!oldName!" "!newName!"
)
exit
This works well..... Try It Out ... No Double Prefix... Ever.
这很好用..... 试试看...没有双前缀...永远。