Dir()函数找不到新解压缩的文件? [英] Dir() function can't find freshly unzipped files?

查看:118
本文介绍了Dir()函数找不到新解压缩的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Ron de Bruin的引用的解压缩宏的示例1解压缩文件: http://www.rondebruin.nl/win/s7/win002.htm



我迄今已经使用了很多次,但是现在我正在尝试使用两倍压缩的下载文件。我遇到的问题是,下面第二部分代码中的Dir()函数找不到一次解压后的.zip文件,直到我以某种方式停止宏。



我尝试过的
将DoEvents和1秒钟等待到第二个解压缩部分



什么工作:
打开宏,执行运行到光标到第二部分代码顶部的Dir()行,然后点击继续。



所需的行为:
宏可以通过单个命令运行,而不必通过运行到光标停止。



复制以下代码的相关连续部分,第一部分工作,以下部分需要额外的推送:



<$ p $ 835仅解压和复制
如果String8 =835然后
'重命名为(目录)(日期)(文件类型)(业务线)(文件扩展名)
名称Stri ng1 As String3& String9& & String8& s& String11& .zip
'将完整的路径保存为一个字符串
String5 = String3& String9& & String8& s& String11& .zip
'String6是835s存档
String6 =(完整文件路径,隐藏隐私,可以插入通用路径,如果需要)
'数据库文件夹
String7 =(完整的文件路径,隐藏的隐私,可以插入通用路径,如果需要)
'从下载文件夹复制到归档文件夹和数据库文件夹
FileCopy String5,String6
FileCopy String5,String7
Kill String5
'解压缩所选存档
设置Object1 = CreateObject(Shell.Application)
String12 =(仅带目录的路径,带尾部反斜杠)
Variant1 = String12
Variant2 = String7
Object1.Namespace(Variant1).CopyHere Object1.Namespace(Variant2).items
On Error Resume Next
Set Object2 = CreateObject(scripting.filesystemobject)
Object2.deletefolder Environ(Temp)& \Temporary Directory *,True
On Error GoTo 0
Kill String7

这部分需要我运行到光标,否则Dir()函数将找不到解压缩的文件,第二个解压缩将被跳过。一次解压缩的文件以.out.zip结尾。不要问我,那就是我如何收到他们。我已经尝试将DoEvents和一秒钟等待在第二行之下:

 '进一步解压缩
String4 = Dir(String12&.out.zip)
Do While String4<
'保存文件的完整路径
String5 = String12& String4
'解压缩所选存档
Variant1 = String12
Variant2 = String5
Object1.Namespace(Variant1).CopyHere Object1.Namespace(Variant2).items
错误恢复Next
Object2.deletefolder Environ(Temp)& \Temporary Directory *,True
On Error GoTo 0
Kill String5
String4 = Dir(String12&.out.zip)
循环
GoTo AllDone
如果

其他注释
我有一行代码中的DoEvents出现在这两个部分之前。



我使用Excel 2010。

解决方案

1秒等待时间太少了。你有两个选择。



A)增加等待时间。私人子等待(ByVal nSec As Long)
nSec = nSec +定时器
虽然这个程序

 nSec>定时器
DoEvents
Wend
End Sub

目录命令类型等待15 。这意味着您的代码将在DIR再次执行之前等待15秒。您可以将其更改为任何适当的数字。 15 只是一个例子。



B)我更喜欢第二种方式。在这里你将循环,直到DIR找到该文件夹​​中的东西,但同时我将合并上述代码与这个



这是一个例子

  Do Dir(String12&* .out.zip)=
等待2
循环

Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
虽然nSec>定时器
DoEvents
Wend
End Sub

您可以添加更多例如,循环的功能,尝试次数。看到这个

  Dim numberOfAttmpts As Long 

Do Dir(String12&* .out。 zip)=
等待2

numberOfAttmpts = numberOfAttmpts + 1

如果numberOfAttmpts> 5然后
MsgBox5次尝试也失败退出
退出子
结束如果
循环

私有子等待(ByVal nSec As Long)
nSec = nSec + Timer
虽然nSec>定时器
DoEvents
Wend
End Sub


I've been unzipping files using Example 1 of Ron de Bruin's oft-cited unzipping macro found here: http://www.rondebruin.nl/win/s7/win002.htm

I have used it successfully many times so far, but now I am trying to work with downloaded files that are twice-zipped. The problem that I am running into is that the Dir() function in the second portion of code below cannot find the once-unzipped .zip files until I stop the macro in some way.

What I've Tried: Putting DoEvents and a 1-second Wait before the second unzipping portion

What Works: Opening the macro, doing "Run to Cursor" to the Dir() line at the top of the second portion of code, and then hitting continue.

Desired behavior: That the macro is able to run through with a single command rather than having to have it stop via Run to Cursor.

Copying the relevant consecutive portions of code below, the first portion works and the following portion needs the extra push:

'835 Unzipping and Copying Only
If String8 = "835" Then
    'Rename as (Directory) (Date) (File Type) (Business Line) (file extension)
    Name String1 As String3 & String9 & " " & String8 & "s " & String11 & ".zip"
    'Save the complete path as one string
    String5 = String3 & String9 & " " & String8 & "s " & String11 & ".zip"
    'String6 is the 835s Archive
    String6 = (full file path, obscured for privacy, can insert generic path if needed)
    'Database Folder
    String7 = (full file path, obscured for privacy, can insert generic path if needed)
    'Copy from the download folder to the archive folder and the database folder
    FileCopy String5, String6
    FileCopy String5, String7
    Kill String5
    'Unzip selected archive
    Set Object1 = CreateObject("Shell.Application")
    String12 = (directory only path with trailing backslash)
    Variant1 = String12
    Variant2 = String7
    Object1.Namespace(Variant1).CopyHere Object1.Namespace(Variant2).items
    On Error Resume Next
    Set Object2 = CreateObject("scripting.filesystemobject")
    Object2.deletefolder Environ("Temp") & "\Temporary Directory*", True
    On Error GoTo 0
    Kill String7

This portion requires me the "Run to Cursor" or the Dir() function will not find the unzipped files and the second unzipping will be skipped. The once-unzipped files end with ".out.zip". Don't ask me, that's just how I receive them. I have tried putting DoEvents and a one-second wait right before the second line below:

    'Further unzipping
    String4 = Dir(String12 & "*.out.zip")
    Do While String4 <> ""
        'Save the complete path of the file found
        String5 = String12 & String4
        'Unzip selected archive
        Variant1 = String12
        Variant2 = String5
        Object1.Namespace(Variant1).CopyHere Object1.Namespace(Variant2).items
        On Error Resume Next
        Object2.deletefolder Environ("Temp") & "\Temporary Directory*", True
        On Error GoTo 0
        Kill String5
        String4 = Dir(String12 & "*.out.zip")
    Loop
    GoTo AllDone
End If

Other Notes: I have DoEvents in one line of my code that occurs earlier than these two portions.

I use Excel 2010.

解决方案

1 Second wait time is way too less. You have two options.

A) Either increase the wait time. Paste this procedure

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

and just before the Dir command type Wait 15. This means your code will wait for 15 seconds before DIR executes again. You can change it to any appropriate number. 15 is just an example.

B) I would prefer the second way. Here you will loop till DIR finds something in that folder but at the same time I will merge the above code with this

Here is an example

Do While Dir(String12 & "*.out.zip") = ""
    Wait 2
Loop

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

You can add more functionality to the loop for example, number of attempts. See this

Dim numberOfAttmpts As Long

Do While Dir(String12 & "*.out.zip") = ""
    Wait 2

    numberOfAttmpts = numberOfAttmpts + 1

    If numberOfAttmpts > 5 Then
        MsgBox "5 attempts also failed. Exiting"
        Exit Sub
    End If
Loop

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

这篇关于Dir()函数找不到新解压缩的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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