运行一个额外的迭代的Windows批处理文件循环 [英] Windows batch file loop running one extra iteration

查看:108
本文介绍了运行一个额外的迭代的Windows批处理文件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Windows批处理文件重命名一气呵成一个很大的数字文件。改名简单地增加了preFIX的文件名。该文件执行除最后一个文件,对此preFIX是某种应用两次罚款。我不知道我在做什么错。任何人都可以指出问题给我吗?

 为%% i的(* .csv的)都仁%%我我的$ P $ _ PFIX我%%


解决方案

 关闭@echo
SETLOCAL
DEL * .csv的
XCOPY .. \\ *。csv格式。

对于%% i的(* .csv的)做(
回声====================
回声%%我
DIR * .csv的
仁%% I X %%我
DIR * .csv的
回声====================)
)> U:\\ junk.txt&放大器;编辑U:\\ junk.txt

我在数载.CSVs一个目录下一个干净的目录运行上面的批处理。它所做的就是扎普当前目录的.csvs,从父目录中复制这些再由prefixing重命名。它透露了不少。

(U:是RAMDRIVE,编辑执行EDITPLUS;记事本也同样可以在这里)

运行依赖于被添加相当什么preFIX。一个preFIX开始一从preFIX的工作方式不同开始的X。与该结果取决于所使用的文件系统。 NTFS presents文件名的字母顺序,但在RAMDRIVE的FAT系统不名称进行排序。

我相信这个问题是由不处理同一个文件名列表改变预期FindNext中文件名的机制造成的。由于它的变化之一,该名称可以在列表中移动,因此作为其新的名字再次遇到该文件可能被再加工 - 可能多次

一个治愈是

来代替选择

  FOR / Fdelims =%% i的('DIR / B * .csv的')做(

在其中创建了DIR列表和然后处理,因此所做的更改不会影响它。

I'm using a windows batch file to rename a very large number of files in one go. The renaming simply adds a prefix to the file name. The file executes fine except for the last file, to which the prefix is somehow applied twice. I'm not sure what I'm doing wrong. Can anyone point out the problem to me?

for %%i in (*.csv) do ren %%i myprefix_%%i

解决方案

@echo off
SETLOCAL
DEL *.csv
XCOPY ..\*.csv .
(
for %%i in (*.csv) do (
echo ====================
echo %%i
dir *.csv
ren %%i x%%i
dir *.csv
echo ====================

)
)>u:\junk.txt&edit u:\junk.txt

I ran the above batch in a clean directory below a directory that contained a few .CSVs. All it does is zap the current directory's .csvs, copy those from the parent directory and then rename by prefixing. It revealed quite a bit.

(U: is a RAMDRIVE, EDIT executes EDITPLUS; NOTEPAD would do as well here)

The operation depends on quite what prefix is added. A prefix starting "a" works differently from a prefix starting "x". AND the result depends on the filesystem being used. NTFS presents the filenames alphabetically but the FAT system on the RAMDRIVE doesn't sort the names.

I believe the problem is caused by the 'findnext' filename mechanism not dealing as expected with a changing list of filenames. As it changes one, that name may move in the list and hence the file may be reprocessed as its new name is encountered again - possibly multiple times.

A cure is to replace the for selection with

for /f "delims=" %%i in ('dir /b *.csv') do (

where the DIR list is created and THEN processed, so the changes don't affect it.

这篇关于运行一个额外的迭代的Windows批处理文件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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