在 Windows 批处理编程中使用循环移动文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6761186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:30:43  来源:igfitidea点击:

Move Files using loop in Windows Batch Programming

windowsbatch-file

提问by Mareena

I am using below code to transfer files, it is showing on each movement of file that 1 file(s) moved, 1 file(s) moved and so on...but it is not showing at the end that total number of files moved ? it was working for my first code even echo %%i was placed in the same location as placed below...plz help...?

我正在使用下面的代码来传输文件,它在文件的每次移动中显示移动了 1 个文件,移动了 1 个文件等等......但它最后没有显示文件总数感动?它适用于我的第一个代码,甚至 echo %%i 被放置在与下面放置的相同位置...请帮助...?

setlocal enabledelayedexpansion
if exist C:\Hi\*.pdf (goto COPYFILES) else (goto NOFILES)

:COPYFILES
for /f %%i in ('DIR /b C:\Hi\*_*.*') do (
    echo %%i
    set fn=%%i
    set fn=!fn:~11,8!
    move C:\Hi\%%i E:\!fn!\
)
echo complete

:NOFILES
echo There are no files to move

采纳答案by Alex K.

The variable %%iwill only ever contain partof the file name, so you try to

该变量%%i将只包含文件名的一部分,因此您尝试

move C:\Hi072011.pdf 

instead of

代替

move c:\hi00225013_30072011.pdf

Alternative:

选择:

setlocal enabledelayedexpansion
if exist C:\Hi\*.pdf (goto COPYFILES) else (goto NOFILES)

:COPYFILES
for /f %%i in ('DIR /b C:\Hi\*_*.*') do (
    echo %%i
    set fn=%%i
    set fn=!fn:~11,8!
    move C:\Hi\%%i E:\!fn!\
)
echo complete
goto:eof

:NOFILES
echo There are no files to move