0

In the below code, if the files being searched for are not present, it still tries to copy the file, indicating that the logic to say if the file is there, then extract it, fails to work correctly, how may I fix this?

Jar = AppData & ".technic\modpacks\" & Modpack & "\bin\modpack.zip"
ExtractTo = Temp

' If version.json already exists in Temp, get rid of it first.
If objFso.FileExists(Temp & "version.json") Then
    objFso.DeleteFile(Temp & "version.json")
End If

' Fallback procedure, if errors occur and jar is still a ZIP, then skip this.
If objFso.FileExists(AppData & ".technic\modpacks\" & Modpack & "\bin\modpack.jar") Then
    objFso.MoveFile AppData & ".technic\modpacks\" & Modpack & "\bin\modpack.jar", _
        AppData & ".technic\modpacks\" & Modpack & "\bin\modpack.zip"
End If

' Open the ZIP and loop through to find the files we need.
Dim i
Set FilesInZip = objShellApp.NameSpace(Jar).Items
For i = 0 To FilesInZip.Count - 1
    ' If version.json is there, extract it to TEMP.
    If FilesInZip.Item(i).Name = "version.json" Then
        objShellApp.NameSpace(ExtractTo).CopyHere(FilesInZip.Item(i))
    End If

    ' If forgeversion.properties is there, extract it to TEMP.
    If FilesInZip.Item(i).Name = "forgeversion.properties" Then
        objShellApp.NameSpace(ExtractTo).CopyHere(FilesInZip.Item(i))
    End If
Next

My sequence of events are this:

  1. I open my app, it detects as it is supposed to that the forge file exists and has a version.
  2. I delete the jar/zip and then put a fresh one in without the files the function tries to find.
  3. I press the refresh link in the app to run the above function again.

This is when I get CopyFile stating the version.json could not be found.

It's intended function is to when not able to find the files, produce an update to the DOM to state No forge version, but instead tries to copy the file even though it isn't there.

Why is this happening?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Bolte
  • 131
  • 3
  • 14
  • This is basically a debugging question. If you have MS Office installed, I recommend you create a Word file, open the VBA IDE and set up a module with your VBS code there. VBA has a decent debugger that lets you step though your code line-wise very conveniently, including variable inspector, watch list, breakpoints and console window. VBA and VBS are compatible for the most part, so after a few minor tweaks, if at all, it's gonna be a *much* better debugging experience than with VBS. – Tomalak Sep 04 '16 at 16:04
  • Thanks for the tip, I don't have office installed. I know that the problem is the line `If FilesInZip.Item(i).Name = "version.json" Then` I guess it has no fallback, but it just seems odd that it is trying to extract the file, when it is supposed to be inside an if statement that checks if the file is there, it should be failing to find it and skipping that statement alltogether. – Bolte Sep 04 '16 at 16:10
  • Too bad. Do you have `On Error Resume Next` in effect for your script? Get rid of it. – Tomalak Sep 04 '16 at 16:19
  • It's like there is some difference between me opening the app (with the only function call being `StatusUpdates()` and me pressing the refresh link, which is ` Rescan `... It all works as intended if I just close and re-open the app. No, I do not have On Error Resume Next... And it is finding the correct files because those are the only ones that even get extracted from the ZIP. – Bolte Sep 04 '16 at 16:19
  • I'm sorry, it's very hard to help you, unfortunately. I can't see anything immediately wrong with your code. I can't recommend much except sprinkling debug statements / MsgBox throughout your code that constantly show the values of variables and function call results. [This question](http://stackoverflow.com/questions/5397152/how-to-debug-ie9-hta) might help you to set up a more elegant debugging environment than that. – Tomalak Sep 04 '16 at 16:42
  • Ok so on further investigation, it seems that when I launch the app, it follows the correct function path. But when I hit the refresh link that calls the command again, it runs the exact same logic path (it believes the file count is still higher, when its not now) and tries to extract version.json, which it can't find for obvious reasons. BUT, if I open the zip and put in version.json into the otherwise empty file, the proper logic follows. Its almost like the original file is cached and when the function is ran again it opens the same file. But Ransack can't find any duplicates anywhere. – Bolte Sep 04 '16 at 17:33
  • I tried setting both objects to Nothing as well to ensure they are unloaded from memory. Still the same, in each instance of the app that is loaded, it follows only one path of logic, it doesn't change the outcome when the function is ran again from within the app based on the changes to the source file. – Bolte Sep 04 '16 at 18:35
  • 1
    Maybe some kind of caching is going on. Isolate the logic into a separate HTA (or VBS) and dumb it down so much that it basically only contains the absolutely necessary five-or-so lines of code. Hard-code all paths, remove every bit of inessential code. Either it starts to work, at which point you can add back functionality bit by bit, to find the point where it breaks, or it does not start to work even with the simplest, self-contained code. In the latter case, take this example and post another question with it. Chances are higher that someone can help. – Tomalak Sep 04 '16 at 18:47
  • Thanks for the advice, I will do this another day, my head is currently mashed. – Bolte Sep 04 '16 at 19:17
  • Try to debug with MS Sctipt Editor, [this](http://stackoverflow.com/q/38934476/2165759) may help. – omegastripes Sep 06 '16 at 13:36

0 Answers0