VBScript 不 oFS.FileExists 文件在 oFS.CreateFolder

问题描述

我在使用 VBScript 时遇到了一个奇怪的问题。我想通过以下测试实现一些其他代码:

如果[文件夹]中有类似[that]的文件,请不要将其复制到[文件夹]中。

问题是,我在 oFS.FileExists 中发现了一个奇怪的关系,我可以在手动创建的文件夹中使用它,只要我手动将文件复制并粘贴到其中即可。然后 oFS.FileExists 就像一个魅力。

Dim oFS
Set oFS = CreateObject("Scripting.FileSystemObject")

filestr = "C:\where\file\is\file.file"

If (oFS.FileExists(filestr)) Then
  WScript.Echo("File exists!")
  WScript.Quit()
Else
  WScript.Echo("File does not exist!")
End If

但这不完全是我的观点。我想测试一个文件是否已经在所需的文件夹中,这样的文件夹将使用 oFS.CreateFolder 自动生成。但在测试自动生成的文件夹时,情况就不同了。

Dim oFS
Set oFS = CreateObject("Scripting.FileSystemObject")

oFS.CreateFolder(destination & objFoldername)

最初我认为我要查找的文件可能有问题。我把它移到了其他地方,oFS.FileExists 找到了它。所以我想这可能是文件夹本身的情况。我可以看到该文件夹​​是只读文件夹。我在其他手动创建的只读文件夹中对其进行了测试,也找到了。 最后,我像 oFS.CreateFolder 一样手动创建了文件夹,手动将文件粘贴到其中,然后……它也找到了一个很好的文件。

正如我所见,我在生成的文件夹中进行的每个测试都失败了,但在手动创建的文件夹中完成了,通过了。

了不起!

有人遇到过这样的情况吗?你知道为什么 oFS.FileExists 对自己创建的东西视而不见吗?

我使用的是 64 位 Windows 10 Home,如果相关的话,我用 Visual Studio Code 编写了这两个脚本。

干杯伙计们,我不能成为第一个。

编辑 leeharvey1

谢谢 leeharvey1,你花了一分钟来看看这个。这是创建目录的代码:

Dim oFS,oFile,objShell,objFolder,sFolderPathspec,destination,file
Set oFS = CreateObject("Scripting.FileSystemObject")
sFolderPathspec = "C:\folder\where\files\are\"
Set objShell = CreateObject ("Shell.Application")
destination = "C:\folder\where\new\folders\with\files\are\intended\to\be\"
Set objFolder = objShell.Namespace(sFolderPathspec)


For Each file In objFolder.Items    
    name = file.Name
    wykonano = objFolder.GetDetailsOf(file,12)
    If wykonano = "" Then
        wykonano = objFolder.GetDetailsOf(file,3) 
    End If
    arr = Split(wykonano," ") 
    brr = Split(arr(0),"-")
    rok = brr(0)
    miesiac = brr(1)
    objFoldername = rok & "-" & miesiac
    
    If CStr(oFS.FolderExists(destination & objFoldername)) >< "Prawda" Then
        oFS.CreateFolder(destination & objFoldername)
    End If

    newdestination = destination & objFoldername & "\" & name
    
    oFS.CopyFile sFolderPathspec & name,newdestination,False
     
Next

文件存在的整个测试开始了,因为我无法运行以下内容:

oFS.CopyFile sFolderPathspec & name,False

我希望复制但不覆盖。错误,但是语法正确,与“Fałsz”相反(这在我的 Windows 语言中是正确的)。但是,一旦遇到目标文件夹中已有的文件,代码就会崩溃。也许我应该有某种代码可以让代码序列继续处理由现有文件引起的崩溃? (就像 Python 一样)

于是我想到了下面的存在性测试问题。

我想我将使用 Files 集合的以下方法。如上所述,我每次在生成的文件夹中进行测试时都会失败,但在手动创建的文件夹中完成,通过。

这是代码(目前在不同的 VBScript 文件中):

filestr = "C:\where\file\is\file.file"

Dim oFS
Set oFS = CreateObject("Scripting.FileSystemObject")

If oFS.FileExists(filestr) Then
    MsgBox("Jest plik")
Else
    MsgBox("Nie ma pliku")
End If

Function FileExists(FilePath)
  Set oFS = CreateObject("Scripting.FileSystemObject")
  If oFS.FileExists(FilePath) Then
    FileExists=CBool(1)
  Else
    FileExists=CBool(0)
  End If
End Function

If FileExists(filestr) Then
  WScript.Echo "Does Exist"
Else
  WScript.Echo "Does not exist"
End If

If (oFS.FileExists(filestr)) Then
  WScript.Echo("File exists!")
  WScript.Quit()
Else
  WScript.Echo("File does not exist!")
End If

所以,有些细节你想知道:

  • 不,我不是针对网络共享文件工作。这一切都在我 PC 的 ssd 上。
  • 您是否尝试过禁用防病毒软件?不,如果我需要这样做才能使用它,我不需要代码。
  • 我想我需要查找文件而不是文件夹,定位文件时出现了某种问题。您认为定位文件夹本身也有问题吗?
  • 检查文件夹所有者。好吧,就我在 Windows 文件夹属性中看到的而言,它的外观和设置与那里的任何其他文件夹相同。

再次感谢 leeharvey1 抽出宝贵时间!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...