1

I have created a project in vb.net that saves a Solid Edge assembly in multiple formats and then zips those formats individually and deletes the unzipped files from the directory. Until very recently, the program was working fine. Now, however, the files are not zipped in the directory. All that happens is that a file with the extension .7z is created one level up (ie I want files to be zipped in C:\Folder\New Folder but the .7z file is created in C:\Folder). Here is some of the project code:

    ' The extensions are stored in the INI file and must be retrieved
            Extension = GetIniValue("CADMakros", "FileFormatExtensions3D", "C:\Windows\RTSettings.INI")

            ' Checks if there are extensions in the INI file
            If Extension = "" Or Extension = " " Then
                MsgBox("Keine Erweiterungen in «RTSettings.ini» eingetragen")
                Exit Sub
            End If

            ' The extensions are separated by a ";" in the INI file
            ' Therefore, they are split up into separate strings and sorted in the NewExtensions array
            'The NewExtensions array does not have a defined size so that an arbitrary number of extensions can be added to the INI file
            NewExtensions = Extension.Split(";")

            ' The spaces are removed from the extension strings
            For i = 0 To UBound(NewExtensions)
                NewExtensions(i) = NewExtensions(i).Replace(" ", "")
                Debug.Print(NewExtensions(i))
            Next


            ' An array containing the filenames is created whose size is dependent on the number of extensions
            ' Therefore, the array size changes when the INI file is modified
            Dim NewFileNames(NewExtensions.Count) As String
            Dim zippedFileNames(NewFileNames.Length) As String

            ' Remove solid edge extension
            FileName = Microsoft.VisualBasic.Left(FileName, InStrRev(FileName, ".") - 1)

            ' Uses the file name as a default response for the input box
            FileName1 = InputBox("Dateinamen eingeben", DefaultResponse:=FileName)
            If FileName1 = " " Then
                MsgBox("Bitte Dateinamen eingeben")
                Exit Sub
            ElseIf FileName1 = "" Then
                Exit Sub
            End If

            ProgressBar1.Value = 30

            ' Creates a new file name that acts as the path of the file
            For k = 0 To (NewExtensions.Length - 1)
                NewFileNames(k) = ChosenFile & "\" & FileName1 & NewExtensions(k)
            Next


            ' The files are saved
            For k = 0 To (NewFileNames.Count - 2)
                objDocument.SaveAs(NewFileNames(k))

                ' The progress of the program is sent to the backgroundWorker so it can update the progress bar accordingly
                BackgroundWorker1.ReportProgress(30 + (k / (NewFileNames.Count - 2)) * 65)

                ' The program must be given time to update the progress bar
                System.Threading.Thread.Sleep(200)
            Next

            For j = 0 To NewFileNames.Length - 2
                For i = 0 To UBound(NewExtensions)
                    If NewFileNames(j).Contains(NewExtensions(i)) Then
                        zippedFileNames(j) = NewFileNames(j).Substring(0, NewFileNames(j).Length - NewExtensions(i).Length)
                        zippedLocation(j) = zippedFileNames(j) + "-" + NewExtensions(i).Substring(1)
                    End If
                Next
            Next

            ' The files are zipped
            Shell(zipPath & " a " & zippedLocation(0) & ".zip " & NewFileNames(0))
            Shell(zipPath + " a " + zippedLocation(1) + ".zip " + NewFileNames(1))
            Shell(zipPath + " a " + zippedLocation(2) + ".zip " + NewFileNames(2))
            'Dim save As New ProcessStartInfo(zipPath)
            'save.Arguments = zipPath &  " a -tzip " & zippedLocation(0) & ".zip " & NewFileNames(0)
            'Process.Start(save)
            ' The program is given time to zip the file before it is deleted (this ensures the zipped file contains the required information)
            Thread.Sleep(2000)

            ''The unzipped file is deleted from the computer
            My.Computer.FileSystem.DeleteFile(NewFileNames(0))
            My.Computer.FileSystem.DeleteFile(NewFileNames(1))
            My.Computer.FileSystem.DeleteFile(NewFileNames(2))

            ProgressBar1.Value = 100

The zip command is near the bottom (they are shell commands). Sorry about the message box messages, they're in German because I'm doing this for a German company. Thank you in advance!

Aidan Kehoe
  • 104
  • 3
  • 14
  • Has any code/config file changed between the working scenario and the non working scenario? Are you getting an error? – Andrew Mortimer Jun 17 '16 at 11:37
  • @AndrewMortimer I'm not getting any errors. And there is no .config file. There is a resources file but I don't think that changed. – Aidan Kehoe Jun 17 '16 at 11:41
  • The `Shell(zipPath + " a " + zippedLocation(0) + ".zip" + NewFileNames(0))` is exactly the same as the working version. The variable values are the same too. Also, everything works fine when I specify a folder with just one parent directory (C:\Folder works fine but C:\Folder\New Folder does not.) – Aidan Kehoe Jun 17 '16 at 11:45
  • Is the presence of sub directories new? – Andrew Mortimer Jun 17 '16 at 11:48
  • @AndrewMortimer I don't think so. I seem to remember zipping to places with sub directories. Even if it is new, it should still work though. Based on the code I wrote, I don't know why a subdirectory would make a difference, unless I'm missing something. – Aidan Kehoe Jun 17 '16 at 11:52
  • Can you debug.print those shell commands and test running them manually? – Andrew Mortimer Jun 17 '16 at 11:59
  • @AndrewMortimer I figured it out! It's because the sub directory has a space in its name! It would treat C:\Folder\New as the directory instead of C:\Folder\New Folder. This also messed up all the commands that follow. Do you know a way to format the Shell command so that command prompt doesn't get confused by the space? – Aidan Kehoe Jun 17 '16 at 12:01
  • Try wrapping with single quotes. – Andrew Mortimer Jun 17 '16 at 12:05
  • Or possibly http://stackoverflow.com/questions/13097872/vb-net-how-to-pass-a-string-with-spaces-to-the-command-line – Andrew Mortimer Jun 17 '16 at 12:07
  • No that didn't work. And the stackoverflow you suggested was unsolved :( – Aidan Kehoe Jun 17 '16 at 12:19
  • Oh okay I've got it now. you need to enclose it in double quotations. And in vb.net, a double double quotation will print a single double quotation. So you wrap the directory with four double quotations. – Aidan Kehoe Jun 17 '16 at 12:27
  • Wrap arguments in double quotes – Matt Wilko Jun 17 '16 at 13:22

0 Answers0