VB Do While 只适用于一次迭代 [英] VB Do While only works for one iteration

查看:29
本文介绍了VB Do While 只适用于一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未用 VB 做过任何事情,我正在尝试解决这个脚本的一个小问题.基本上,脚本应该查找文件夹中的所有 jpg,如果文件名已存在,则将其从目标文件夹中删除,将该值添加到表中,然后重命名源文件,使其存在于目标文件夹中.

I've never done anything before with VB and am trying to work out a little problem with this script. Basically the script should look for all jpgs in a folder, if the file name exists already remove it from the target folder, add the value to a table and then rename the source file so it exists in the target folder.

这个脚本在一定程度上起作用,例如,如果文件不存在,它将重命名任何文件,但对于任何已经存在的文件,它只处理一个然后结束.我可以多次运行它以清除其余部分,但宁愿一次性清除它们.我已经做了很多阅读,但看不出哪里出了问题.任何人都可以对此有所了解吗?

This script works to an extent, for example it will rename any file if the file doesn't exist already but for any files that do already exist it processes only one then then ends. I can run it multiple times to clear the rest but would rather it cleared them all in one go. I have done quite a bit of reading but can't see what is going wrong. Can anyone shed any light on this?

Public Function GetLPUFileAddress()

Dim strSourceFolder As String
Dim strFile As String
Dim strTargetFolder As String

Dim dbs As DAO.Database
Dim rstMgr As DAO.Recordset

strSourceFolder = "C:\Users\Images\LPU-HOLDING\"
strFile = Dir(strSourceFolder & "*.JPG")
strTargetFolder = "C:\Users\Images\LPU\"

Do While strFile <> ""
    If Dir(strTargetFolder & strFile) <> "" Then Kill strTargetFolder & strFile
    CurrentDb.Execute "INSERT INTO TBL_LPU ( File_Name, Import_Date ) VALUES ('" & strFile & "',Date())"
    Name strSourceFolder & strFile As strTargetFolder & strFile
    strFile = Dir
Loop

End Function

推荐答案

通过调用带有适当过滤器的 Dir() 函数,例如 "C:\Users\Images\LPU\*.JPG",你开始枚举并得到第一个文件名.
之后,不带任何参数重复调用Dir()函数,会得到所有*.JPG文件名,每次调用一个.
如果您在任何时候使用参数调用 Dir(),这将重置当前枚举并开始一个新枚举.

By calling the Dir() function with an appropriate filter, such as "C:\Users\Images\LPU\*.JPG", you start enumeration and get the first file name.
After that, repeatedly calling the Dir() function without any parameters, you will get all *.JPG file names, one for each call.
If you at any point call Dir() with a parameter, this will reset the current enumeration and start a new one.

因此,当您使用 Dir() 进行枚举时,您不能使用 Dir() 来检查文件夹中文件的存在.这将重置枚举.

You therefore can't use Dir() to check existance of a file in a folder while you're enumerating with Dir(). This resets the enumeration.

您必须使用 其他方式检查文件的存在,或者只是尝试在不检查的情况下杀死它,忽略错误.

You have to either use some other way of checking existance of the file, or just try to kill it without checking, ignoring the error.

这篇关于VB Do While 只适用于一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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