批量使用循环重命名文件 [英] Batch rename file using for loop

查看:244
本文介绍了批量使用循环重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个for循环来重命名文件

I am trying to use a for loop to rename a file

::@Echo Off
setlocal enableDelayedExpansion
::Set Date
set mydate=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
::Rename file
for %%F in (D:\Data\*.bak) do (
    ren %%F D:\Data\prod_live_Full_%mydate%0000.Lts.bak
)

我不断收到无效命令错误。任何帮助是AP preciated。

I keep getting invalid command error. Any help is appreciated.

推荐答案

在目录中修改文件从使用一个静态的文件列表 DIR ,而不是从一个动态的文件列表。否则,你会得到滞留在一个无限循环。

When modifying files in a directory use a static file list from dir instead of a dynamic file list from for. Otherwise, you will get stuck in an infinite loop.

@echo Off
setlocal
set "mydate=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%"
pushd "D:\Data\" && for /f "delims=" %%A in ('dir /a-d /b *.bak') do (
    ren "%%~fA" "prod_live_Full_%mydate%0000.Lts.bak"
)
popd
endlocal
exit /b 0

错误是由您的第二个参数命令造成的。它只能期待一个新的文件名,而不是完整的路径和文件名。在命令不能移动文件。

The error was caused by your second parameter to the ren command. It is only expecting a new file name, not a full path and file name. The ren command cannot move files.

这篇关于批量使用循环重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆