Windows 脚本 FOR 命令重命名目录中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/191351/
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 scripting FOR command to rename all files in a directory
提问by goldenmean
I am trying to rename all the files present in a Windows directory using FORcommand as follows at the command prompt:
我正在尝试在命令提示符下使用FOR命令重命名 Windows 目录中存在的所有文件,如下所示:
for %1 in (*.*) do ren %1 test%1
E.g. This renames a file enc1.ctlto testenc1.ctlenc2.ctlto testenc2.ctl
例如,这会将文件enc1.ctl重命名为testenc1.ctl enc2.ctl到testenc2.ctl
Thats not what i want. What i want is enc1.ctlrenamed to test1.ctlenc2.ctlrenamed to test2.ctl
那不是我想要的。我想要的是 enc1.ctl重命名为test1.ctl enc2.ctl重命名为test2.ctl
How do i do that?
我怎么做?
@Akelunuk: Thanks, that w kind of works but i have files names as
@Akelunuk:谢谢,那种工作,但我有文件名
h263_enc_random_pixels_1.ctl , h263_enc_random_pixels_2.ctlwhich i want to rename to
h263_enc_random_pixels_1.ctl , h263_enc_random_pixels_2.ctl我想重命名为
test1.ctl and test2.ctlrespectively
test1.ctl 和 test2.ctl分别
Then how?
那怎么办?
采纳答案by David Nehme
If you know the number of files, (say 10), you can use
如果你知道文件的数量,(比如 10),你可以使用
for /L %1 in (1,1,10) do ren enc%1.ctl test%1.ctl
回答by akalenuk
I've got it!
我懂了!
for %1 in (.) do ren %1 t%1
and then:
进而:
ren tenc*.* test*.*
回答by PhiLho
I am not sure if it is possible in batch, but then again I never mastered this primitive language... :-P
我不确定是否可以批量处理,但是我再一次从未掌握过这种原始语言...... :-P
If CMD isn't mandatory, but you can't use a good file renamer, you can do that with WSH:
如果 CMD 不是强制性的,但你不能使用一个好的文件重命名器,你可以用 WSH 来做到这一点:
var path= "E:/tmp";
var fso = WScript.CreateObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(path);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext())
{
var file = files.item();
var fileName = file.Name;
var p = /^enc(\d+)\.ctl$/.exec(fileName);
if (p != null)
{
var newFileName = "test" + p[1] + ".ctl";
// Optional feedback
WScript.echo(fileName + " -----> " + newFileName);
file.Move(newFileName);
}
}
Of course, put that in a file.js
I actually tested with file.Copy(file.ParentFolder + "/SO/" + newFileName);
to avoid loosing files...
当然,把它放在
我实际测试过的 file.js 中file.Copy(file.ParentFolder + "/SO/" + newFileName);
以避免丢失文件......
HTH.
哈。
回答by Yousef Rabby RJ
This renames all files in directory for filter file types with PREFIX and today's date and time
这将重命名目录中的所有文件,以使用 PREFIX 和今天的日期和时间过滤文件类型
@echo ON
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo.
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
PAUSE
This echos successfuly the date modified and time as a postfix but doesnt parse the info into the rename. I cant figure out why, but it is very close. Maybe someone cant tweak to suit your purpose.
这将成功地回显修改日期和时间作为后缀,但不会将信息解析为重命名。我不知道为什么,但它非常接近。也许有人无法调整以适合您的目的。
@echo ON
setlocal
cls
for %%a in (*.pdf) do (set myfiledate=%%~ta echo !myfiledate!)
:DATETIME
echo Date format = %myfiledate%
echo dd = %myfiledate:~0,2%
echo mm = %myfiledate:~3,2%
echo yyyy = %myfiledate:~6,4%
echo Time format = %myfiledate%
echo hh = %myfiledate:~11,2%
echo mm = %myfiledate:~14,2%
echo AM = %myfiledate:~17,2%
= %myfiledate:~17,2%
echo.
echo Timestamp = %myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%
ECHO "TEST..." > "test-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%.txt"
for /f "delims=" %%a in ('dir *.pdf /t:a /a:-d /b /s') do call :RENAME "%%a"
:RENAME
REM for /f "tokens=1-6 delims=/ " %%a in ('dir %%a /t:w^|find "/"') do (
ren %%a "3DC-test-OFF-ELE-%myfiledate:~0,2%_%myfiledate:~3,2%_%myfiledate:~6,4%-TIME-%myfiledate:~11,2%_%myfiledate:~14,2%_%myfiledate:~17,2%~x1")
PAUSE
GOTO :EOF