File doesn't zip using 7zip command in vb.net - vb.net

I am trying to zip a file in vb.net. I am using 7zip to do this. I am using the Process.Start method.
Here is the zip line of my code:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip" + (ChosenFile & "\" & "SavedFiles") + NewFileName1)
No error that I know of happens, however when I look through the path, I cannot find the zipped files.
ChosenFile & "\" & "SavedFiles" is the destination folder.
NewFileName1 is the file to be zipped

you must do a space after "-tzip" and u forgot use " to folders with spaces like that:
Process.Start("C:\Program Files\7-Zip\7z.exe", "a -tzip " & (ControlChars.Quote & ChosenFile & "\" & "SavedFiles" & ControlChars.Quote) & " " & ControlChars.Quote & NewFileName1 & ControlChars.Quote &)

Probably its obvious, but you have the rights to access those files with the user who runs the script?
Else, can you run the script manually step by step to debug where it fails?

Related

Executing a bat. file with arguments from vba code

I'm trying to execute a bat. file from vba but can't get it working (after viewing some of the threads from the subject).
here is the code now:
Dim filet As String
Dim numero As String
Const pekka = "TIEDOSTO"
Const sami = "NUMEROT"
filet = Range(pekka).Cells(1, 1).value
numero = Range(sami).Cells(1, 1).value
commandstring = "D:"
commandstring2 = "cd folder name"
commandstring3 = "Create-tri.bat" + " " + filet + " " + numero
Call Shell("cmd.exe /S" & commandstring & commandstring2 & commandstring3, vbNormalFocus)
So according to my logic, this code should first access D, then the wanted folder and then execute the bat. file with the given parameters (filet and numero). What am I doing wrong here?
Best regards and thanks in advance,
Johannes
Try to remove the Call, it is depreciated in VBA.
Then use Shell "cmd.exe /S" & commandstring & commandstring2 & commandstring3.
To see what you are actually trying to execute, use:
Debug.Print "cmd.exe /S" & commandstring & commandstring2 & commandstring3
What do you get? Most probably it is not an executable command, ending on .bat Some reference here - Execute .bat file from Excel VBA Macro

Vba to call a powershell script or .bat file

I have a line to call a powershell script and it works but the powershell windows gets closed without it working.
the code is, any ideas?
Additionally I would like it to be able to run .bat files (I tried Shell ( & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt) bit I get the same error.
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell ("PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt)
End Sub
Please see the link below and make sure you have permissions for the machine.
http://www.tek-tips.com/viewthread.cfm?qid=1585510
I don't know about your setup or security, so if you encounter an error, search for 'PS permissions', or something along those lines. The VBA script will definitely work!
If you insert a space after .ps1 it should work:
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell "PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1 " & pathcrnt
End Sub
Imagine the resulting string; for pathcrnt C:\Windows it would be this: "PowerShell C:\Windows\GUI-getuserpropertiesV2.10.ps1C:\Windows"
You also might want to enclose the path elements with double quotes to prevent spaces messing with your parameters, like this:
Shell "PowerShell """ & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1"" """ & pathcrnt & """"
Since double quotes also are string delimiters in VBA you have to escape them with another double quote.

Download File and Extract Immediately - File is being used by another process

I'm looping through a list of zip files and downloading them from a WebClient. I'm trying to extract them immediately after they download, but getting an error that the file is being used by another process.
Error: The process cannot access the file 'c:\temp\test.zip' because it is being used by another process.
The only thing that I can think of is that maybe the file is still "in use" for a short time after being downloaded? DownloadFile does block ("This method blocks while downloading the resource." - http://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx), so I can't see it being an issue with the file still downloading.
Any ideas on a workaround?
Using client As New WebClient
oLog.Info("Downloading " & strFileServer & " to " & strFileLocal & "...")
If Not Directory.Exists(strPathLocal) Then Directory.CreateDirectory(strPathLocal)
client.DownloadFile(strFileServer, strFileLocal)
End Using
Using archive As ZipArchive = ZipFile.OpenRead(strFileLocal)
For Each entry As ZipArchiveEntry In archive.Entries
oLog.Info("Extracting " & entry.FullName & " to " & strPathLocal & "...")
entry.ExtractToFile(strFileLocal, True)
Next
End Using

Moving Multiple Files of Same Type

I need to be able to move all .pbo files from one folder, into another folder. The following is my code:
For Each foundPBO As String In My.Computer.FileSystem.GetFiles( _
downloadDirectory & "\Mod Works\Process\#" & CurrentMod, _
FileIO.SearchOption.SearchAllSubDirectories, "*.pbo")
My.Computer.FileSystem.MoveFile(foundPBO, downloadDirectory & "\Mod Works\Process\#STHUD\Addons")
Next
It doesn't do anything when I run it, and the directory strings are correct (downloadDirectory is set correctly & CurrentMod is set correctly)
Any suggestions?
UPDATE:
Thanks for the help, although, I'm getting an error with conversion? Here's my following code, it doesn't get to the "3" message (debug):
Dim testDir As String = downloadDirectory & "\Mod Works\Process\#STHUD\"
For Each foundPBO As String In My.Computer.FileSystem.GetFiles( _
MsgBox("2"), _
testDir, _
MsgBox("3"), _
FileIO.SearchOption.SearchAllSubDirectories, "*.pbo")
MsgBox("4")
My.Computer.FileSystem.MoveFile(foundPBO, downloadDirectory & "\Mod Works\Process\#STHUD\Addons\" & System.IO.Path.GetFileName(foundPBO))
MsgBox("5")
Next
The destination path is wrong: you are intending to move "dir_source\file_source.pbo" to "dir_dest\"; but you should do: "dir_dest\file_source.pbo". Just replace
My.Computer.FileSystem.MoveFile(foundPBO, downloadDirectory & "\Mod Works\Process\#STHUD\Addons")
with:
My.Computer.FileSystem.MoveFile(foundPBO, downloadDirectory & "\Mod Works\Process\#STHUD\Addons\" & System.IO.Path.GetFileName(foundPBO))
Bear in mind that there are System.IO equivalences for all what you are doing (getting files and moving them). You might prefer to rely on System.IO, rather than on My.Computer.FileSystem, as far as this Namespace contains methods to perform much more I/O-related actions (i.e., dealing with files, directories and paths).

Shell Command Gives Error "error after : and."

I used :
Shell("cmd /k type " & System.IO.Path.GetTempPath & "file.exe > " & Application.ExecutablePath & ":file.exe")
Temp Folder : C:\Documents and Settings\Admin\Local Settings\Temp\
Command Prompt Window gives error : File not found, error after : and. System can't find destination path.
I think the problem is in this name : Documents and Settings
What should I do to bring it to life?
P.S : File exists, it works when I use : start command.
You need extra wrapping quotes.
Shell("cmd /k type """ & System.IO.Path.GetTempPath & "file.exe"" > " & Application.ExecutablePath & ":file.exe")
Spaces are delimiters in command line parameters. If you have a space in a single parameter, you need to wrap the entire parameter in quotes.
you probably need to put your path between "". something like
Shell("cmd /k type \"" & System.IO.Path.GetTempPath & "file.exe\" > " & Application.ExecutablePath & ":file.exe")