Moving Multiple Files of Same Type - vb.net

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).

Related

File Download via shdocvw.dll with custom headers

I need to download a really large file in msaccess via a vba application.
Using the objects MSXML2.ServerXMLHTTP.6.0 and WinHttp.WinHttpRequest.5.1 result in an error stating that there is not enough storage available to complete this operation. Therefore i resorted in using the DoFileDownload method from shdocvw.dll.
What i want to do is pass an extra header (an API key) to the request sent by the function.
Here is roughly what i want to do.
Private Declare Function DoFileDownload Lib "shdocvw.dll" _
(ByVal lpszFile As String) As Long
Public Sub Download()
sDownloadFile = StrConv(<link_to_download>, vbUnicode)
'set a header before calling DoFileDownload
Call DoFileDownload(sDownloadFile)
End Sub
How do i approach this problem?
A WebRequest downloading a whole file at once stores the whole data in response.
Although there are options to chunk response, using Wget is less coding, but more options.
Private Sub DownloadFileWget()
Const PathToWget As String = "" 'if wget is not in path use "Path\To\Wget"
Dim LinkToFile As String
Dim SavePath As String
With CreateObject("WScript.Shell")
LinkToFile = "http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab" 'huge file > 500MB
SavePath = "C:\doc" 'folder to save download
.CurrentDirectory = SavePath
.Run Chr(34) & PathToWget & "wget.exe" & Chr(34) & " --header='name: value' " & Chr(34) & LinkToFile & Chr(34) & " -N", 1, True
' -N: Continue download only if the local version is outdated.
End With
End Sub

File doesn't zip using 7zip command in 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?

Use combobox in savepath

first of all: Sorry for the not so clear title. I didn't know a better way to descripe my question.
I'm building a application that has to save user-specified data to a sdcard on a plc.
I already found out how to connect to that plc but am still working on the saving part.
For the testing i just used:
ds.WriteXml("C:\" & DateTimePicker1.Text & ".xml")
I think it's possible to change it to \192.168.2.16\SDcard\filename but that's not very flexible.
What i would like to have is the ability to take the value from a combobox and use that as the ip adress.
What is the best way to do this? as i don't think it's a simpe thing like making the savepad
(\" & comboIP.selectedvalue & "\Sdcard\" & DateTimePicker1.Text & ".xml") Unfortunately, the SD card is still on it's way so i can't test it yet..
Thanks in advance!
ds.WriteXml("C:\" & comboIP.Text & "\SDCard\" & DateTimePicker1.Text & ".xml")
That works just fine.
You don't really need the SDCard in hand to test this out.
You can simply create temporary variables before the WriteXML function call, set a breakpoint on them, and ensure that they are the correct values beforehand.
e.g.:
Dim sSelectedIP As String = comboIP.Text
Dim sDateTimePicker As String = DateTimePicker1.Text
Dim sCompleteDirectory As String = "C:\" & sSelectedIP & "\SDCard"
If My.Computer.FileSystem.DirectoryExists(sCompleteDirectory) = False Then
My.Computer.FileSystem.CreateDirectory(sCompleteDirectory)
End If
ds.WriteXml(sCompleteDirectory & "\" & sDateTimePicker & ".xml")

Directory.Move isn't trying the correct folder

I have made some code intended to sort all my movies on my PC into subfolders for each letter of the alphabet (e.g. "An Example" would go in a subfolder containing only movies which start with the letter "A".
The code I've written looks to me like it should work without problems, although for some reason this code:
'Declarations
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, mainFolder.Name & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.ToString, TS.ToString & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
End If
Next
Keeps throwing a exception at this line:
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
This is the exception:
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in
mscorlib.dll
Additional information: Could not find a part of the path
'D:\Users\Yorrick\documents\visual studio 2012\Projects\filmsort\filmsort\bin\Debug\(CAM)The Internship'.
As indicated by the declarations above, I have no idea how or why this code is trying to access that folder.
If anyone could take a look and hopefully spot the mistake I've made, that would be very much be appreciated.
Edit:
Having fixed the above problem, I encountered a new one.
Using the code below, I now get the same exception, except this time additional information says "Could not find a part of the path." All my variables seem to be correct while debugging, so I seriously can't see why this isn't working.
Note: The commented line is something I tried, which gives me the System.IO.IOException: Cannot create a file when that file already exists.
Code:
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, mainFolder.FullName & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
'System.IO.Directory.CreateDirectory(CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, TS.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
End If
Next
This happens when you don't specify a full path name, like "c:\foo\bar", but a relative path name, like "bar". Relative path names are turned into full ones by prepending Environment.CurrentDirectory. Which by default is the build directory of your project.
This happened because you used DirectoryInfo.Name. Which is "bar" for a directory whose path is c:\foo\bar. You must use the FullName property instead.
I just rewrote it a bit, i think that should suffice for your purpose.
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If f.Name.ToUpper.Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(TS, f.Name))
ElseIf f.Name.ToUpper.Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(CAM, f.Name))
End If
Next

Issue with an LPR Command in VB

I am creating a VB app which will "move" xls reports from a directory to a ReportSafe app. I am also working in an existing VB app which does just that, so I am using it for reference.
It isn't as simple as moving files from one directory to another, because ReportSafe requires an lpr command to tell it (ReportSafe) which file to pick up.
Here is what I have so far:
Imports System.IO
Module Module1
Sub Main()
''Declarations
Dim Files As ArrayList = New ArrayList()
Dim FileName As String
''Write All Files in *directory* to ReportSafe
Files.Clear()
Files.AddRange(Directory.GetFiles(*directory*))
For Each FileName In Files
Dim RPname As String
Dim RealName As String
RPname = FileName.ToString
RealName = "/"
RealName = RealName & RPname.Remove(0, 34)
Dim a As New Process
a.StartInfo.FileName = "C:\Windows\system32\lpr.exe"
a.StartInfo.Arguments = "-S*ServerName* -Plp -J" & Chr(34) & RealName & Chr(34) & " " & Chr(34) & RPname & Chr(34)
a.StartInfo.UseShellExecute = False
Next
End Sub
End Module
The whole lpr command/arguments are throwing me for a loop. I'm not sure if my question is specific to ReportSafe, and if that's the case, I may be out of luck here. I have pulled this code from the already existing app which moves reports to ReportSafe, and adjusted for my own use, but no luck so far.
FYI, I had to turn on LPR Monitor services to obtain to the lpr.exe
Questions:
What are the proper arguments to pass through to this lpr command?
Is there a problem with the logic that is causing the issue?
I continued to tinker and look at my reference code and discovered some flaws in logic:
For one, the report name I was passing did not include the complete file path.
Another thing is that I never started the process with a.Start(). Rookie mistakes for sure... haha