为什么一个额外的文件被创建在批处理程序的循环? [英] Why is an extra file being created in FOR loop of batch program?

查看:139
本文介绍了为什么一个额外的文件被创建在批处理程序的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的批处理文件的创建使用FOR循环多个文件:

I've written the following batch file to create multiple files using FOR loop:


@echo off  
cls  
FOR /L %%i IN (1 1 10) DO (  
    echo.> file%%i.txt  
    IF ERRORLEVEL 0 echo Successfully created file 'file%%i.txt'.  
)  
dir /b *.txt  
FOR %%i IN (*.txt) DO (  
    echo.> file%%i.txt
    IF ERRORLEVEL 0 echo Successfully created file 'file%%i.txt'.  
)

在这里,10个文件(即 FILE1.TXT .... file10.txt )的创建第一个for循环。结果
而在第二个for循环我使用这些文件的框架下一个新的文件名。 (即 filefile1.txt.txt ... filefile10.txt.txt

Here, 10 files (viz. file1.txt .... file10.txt) are created in the first FOR loop.
And in the second FOR loop I've used these files to frame the name of next new files. (viz.filefile1.txt.txt ... filefile10.txt.txt)

不过,正在创建一个额外的文件: filefilefile1.txt.txt.txt 结果
什么逻辑问题导致了此额外文件的创建?

But, an extra file is being created : filefilefile1.txt.txt.txt
What logical issue is causing the creation of this extra file ?

推荐答案

编辑 - 看来我还没有正确地解释它,人们没有看到它是如何工作。我的错。我要去尝试解释更好。

EDITED - Seems i've not explained it properly and people doesn't see how it works. My fault. I'm going to try to explain it better.

原因是为命令内部的工作方式。

The reason is the way the for command works internally.

在该行对于VAR(文件)到达,该目录检查,看是否有文件相匹配,需要进行处理。

When the line for var in (files) is reached, the directory is checked to see if any files match and need to be processed.

然后,命令(CMD真的),发出目录查询枚举文件。该查询返回的只有第一个文件中集。如果有匹配文件掩模命令,任何aditional的文件,一个标志被设置,指示给呼叫者(CMD),该有要处理多个文件,但剩余的文件的名单尚未检索

Then, for command (cmd really), issues a directory query to enumerate the files. This query returns only the first file in set. If there are any aditional files that match the file mask in for command, a flag is set that indicates to the caller (cmd) that there are more files to be processed, but the list of the remaining files is not yet retrieved.

当在 code的执行reachs一个迭代结束,并有等待被读取文件,发送一个查询,以获得<强等待被处理的文件列表和匹配文件选择>​​剩余。

When execution of code inside for reachs the end of an iteration, and there are files pending to be read, a query is sent to get the remaining of the list of files pending to be processed and that match the for file selection.

系统填补了缓冲区的文件列表中的剩余。如果在该点的文件列表是短到足以在缓冲区满readed,该查询将不再重复。如果文件列表是足够大,以不适合于缓冲液中,部分列表被检索并当在检索列表文件进行处理,查询将被再次发送,以​​获得更多要处理的文件

System fills a buffer with the list of files remaining. If at that point the list of files is short enough to be full readed in buffer, the query will not be repeated. If the list of files is big enough to not fit in buffer, partial list is retrieved and when files in retrieved list are processed, the query will be sent again to get more files to process.

在缓冲区中的文件数取决于文件名长度。短文件名,缓冲区少查询的详细文件到文件系统。

Number of files in buffer depends on length of filename. Shorter filenames, more files in buffer and less queries to filesystem.

此行​​为(在检索第一个文件的处理结束文件的剩余列表),如果文件的查询返回有挂起的文件只是做。当一个查询不返回该标志,没有更多的文件检索。

This behaviour (retrieving remaining list of files at end of first file processing) is only done if the query for files returns that there are files pending. When one query does not return that flag, no more files are retrieved.

例外

如果在NTFS工作,文件只得到列入重新查询,如果他们按字母顺序排列比已经在被处理为命令的最后一个文件更大。

If working in NTFS, the files only get included in "requeries" if they are alphabetically greater than the last file that has been processed in for command.

如果工作,如果FAT,查询将包含生成的所有新文件independly匹配它名字的命令文件选择了。是的,它可以进入一个无限循环。 (在测试中,该系统缓冲区只检索一个文件名,并重新查询在每次迭代)。你可以试试。

If working if FAT, the query will include all new file generated that match the for command file selection independly of it name. And yes, it can get into an infinite loop. (In tests, the system buffer only retrieve one filename, and requery in each iteration). You can try

break > a.txt && cmd /v:on /c "for %f in (*.txt) do break > !random!.txt"

我所有的测试已经取得了在Windows 7 64位,NTFS和FAT32分区(这是一个USB驱动器上)。没有办法测试其他配置。如果有人看到diferent行为,请评论。

All my tests has been made on a windows 7 64bit, NTFS and FAT32 partition (this on a USB drive). No way to test other configurations. If anyone see a diferent behaviour, please comment.

有关更多信息,请 ZwQueryDirectoryFile

这篇关于为什么一个额外的文件被创建在批处理程序的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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