I've been working on a script, a windows console application, that reads a .CSV for a file-path and other various information and then using System.IO.File.Copy to copy the file to a new directory and give it a new name. The file's im working with are .WAV files. Essentially I take a peek at the .CSV provided, then grab the filepath from there, then rename the file using different columns from the .CSV with dot-notation. I.E. Acc.Date.Name.Type.wav.
However, I'm experiencing "NotSupportedException" error on the line that copies and create the new file. The sub message is "The given paths format is not supported." Now, I've fairly new to VB code, I' assuming right now that .WAV format is not supported.
I'm not sure what all to add, if anything is needed let me know.
Here is the script so far:
Module Module1
Sub Main()
Dim fileLine As String
Dim aryTextFile() As String
Dim objReader As New System.IO.StreamReader("C:\Temp\EDCallFilePaths.csv")
Do While objReader.Peek() <> -1
fileLine = fileLine & objReader.ReadLine() & vbNewLine
aryTextFile = fileLine.Split(",")
If Not aryTextFile(7) = "null" Then
System.IO.File.Copy(aryTextFile(7), "C:\Temp\" & aryTextFile(0) & "." & aryTextFile(2) & "." & aryTextFile(3) & "." & aryTextFile(4) & "." & aryTextFile(5) & "." & aryTextFile(6) & ".wav")
fileLine = ""
End If
Loop
End Sub
End Module
I should note, column 7 is the filepath to the recording
Related
JavaError.128 = "project creation failed. & vbLf & Please try again and if the problem persists then contact the administrator"
I am able to read this message from text file. the issue is vbLf is not considered as newline in msgbox. it prints vbLf in msgbox.
Using sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(errorfilePath)
While ((sr.Peek() <> -1))
line = sr.ReadLine
If line.Trim().StartsWith("JavaError." & output) Then
isValueFound = True
Exit While
End If
End While
sr.Close()
End Using
If isValueFound Then
Dim strArray As String() = line.Split("="c)
MsgBox(strArray(1).Replace("""", "").Trim({" "c}))
End If
You can make all your code a simpler one line version using File.ReadAllLines and LINQ. This code will put all the lines starting with javaerror into the textbox, not just the first:
textBox.Lines = File.ReadAllLines(errorFilePath) _
.Where(Function(s) s.Trim().StartsWith("JavaError")) _
.Select(Function(t) t.Substring(t.IndexOf("= ") + 2).Replace(" & vbLf & ", Environment.NewLine)) _
.ToArray()
You need to Imports System.IO and System.Linq
This code reads all the lines of the file into an array, then uses LINQ to pull out only those starting with java error, then projects a new string of everything after the = with vbLf replaced with a newline, converts the enumerable projection to an array of strings and assigns it to the textBox lines
If you don't want all the lines but instead only the first:
textBox.Text = File.ReadLines(errorFilePath) _
.FirstOrDefault(Function(s) s.Trim().StartsWith("JavaError")) _
?.Substring(t.IndexOf("= ") + 2).Replace(" & vbLf & ", Environment.NewLine))
This one uses ReadLine instead of ReadALlLines - ReadLines works progressively, and it makes sense to be able to stop reading after we foundt he first rather than have the overhead of reading ALL (million) lines only to then end up pulling the first out and throwing 999,999 lines of effort away. So it's reading line by line, pulls out the first that starts with "JavaError" (or Nothing if there is no such line), then checks if Nothing came out (the ?) and skips the Substring if it was Nothing, or it does a Substring on everything after the = and replaces vbLf with newline
For a straight up mod of your original code:
Using sr As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(errorfilePath)
While ((sr.Peek() <> -1))
line = sr.ReadLine
If line.Trim().StartsWith("JavaError." & output) Then
isValueFound = True
line = line.Replace(" & vbLf & ", Environment.NewLine))
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ added code
Exit While
End If
End While
sr.Close()
End Using
If isValueFound Then
Dim strArray As String() = line.Split("="c)
MsgBox(strArray(1).Replace("""", "").Trim({" "c}))
End If
Note that I've always made my replacement on & vbLf & with a space at each end to avoid stray spaces being left behind - if your file sometimes doesn't have them, consider using Regex to do the replace, e.g. Regex.Replace(line, " ?& vbLf & ?", Environment.NewLine
This could work:
Dim txtFile As String = "project creation failed. & vbLf & Please try again and if the problem persists then contact the administrator"
Dim arraytext() As String = txtFile.Split("&")
Dim txtMsgBox As String = Nothing
For Each row As String In arraytext
If Trim(row) = "vbLf" Then
txtMsgBox = txtMsgBox & vbLf
Else
txtMsgBox = txtMsgBox & Trim(row)
End If
Next
MsgBox(txtMsgBox)
I've been searching the net and have tried all the code found. My code should be simple but it's not working. I'm using a list to store image strings (but they do not have the extension on them). I want to test if the image file exists in a folder and if it does copy it. If it doesn't write to a file with the image name. The result I'm getting is all the files do not exists. But I checked and the files are there.
For Each image In GraphicList
ImgFile = ImgLocation & "\" & image & ".*"
Dim MoveFile As String
MoveFile = createFigFolder & "\" & image & ".*"
If Not System.IO.File.Exists(ImgFile) Then
Debug.Write("File does not exists : " & ImgFile & vbCrLf)
' file does not exist
Else
Debug.Write("File EXISTS : " & ImgFile & vbCrLf)
System.IO.File.Copy(ImgFile, MoveFile)
End If
Next
Here's the code that writes the GraphicList
Private Sub CreateGraphicsFunction(sender As Object, e As EventArgs)
Dim Regex = New Regex("infoEntityIdent=""(ICN.+?)[""].*?[>]")
Dim ICNFiles = Directory.EnumerateFiles(MoveToPath, "*.*", SearchOption.AllDirectories)
For Each tFile In ICNFiles
Dim input = File.ReadAllText(tFile)
Dim match = Regex.Match(input)
If match.Success Then
GraphicList.Add(match.Groups(1).Value)
Dim Regex2 = New Regex("<!ENTITY " & match.Groups(1).Value & " SYSTEM ""(ICN.*?[.]\w.+)")
Dim sysFileMatch = Regex2.Match(input)
If sysFileMatch.Success Then
ICNList.Add(sysFileMatch.Groups(1).Value)
Debug.Write("found ICN " & sysFileMatch.Groups(1).Value)
End If
End If
Next
End Sub
New Code to cycle through array and see if its entries match strings in Graphic Lists. This code doesn't work, but I think it's similar to what I want.
Dim fileEntries As String() = Directory.GetFiles(ImgLocation, ".*")
' Process the list of files found in the directory. '
Dim fileName As String
For Each fileName In fileEntries
If ICNList.Contains(fileName) Then
Debug.Write("File EXISTS : " & fileName & vbCrLf)
Else
Debug.Write("File does not exists : " & fileName & vbCrLf)
End If
Next
Join the list of filenames on the source list of files, using the filename without extension as the key. Then iterate over the results and copy each one
Dim fileNames = System.IO.Directory.GetFiles(imgLocation).Join(
graphicList,
Function(p) Path.GetFileNameWithoutExtension(p),
Function(f) f,
Function(p, f) p)
' create the directory first (does nothing if it already exists)
Directory.CreateDirectory(newLocation)
' copy each file
For Each fileName In fileNames
System.IO.File.Copy(fileName, Path.Combine(newLocation, Path.GetFileName(fileName)))
Next
PupilID = "\" & PupilID & ".txt"
If Dir$(PupilID) = "" Then
Dim swpupilinfo As New StreamWriter(Application.StartupPath & PupilID, True)
swpupilinfo.Close()
End If
If System.IO.File.Exists(filename) = True Then
readerlinecount = File.ReadAllLines(Dir$(PupilID))
End If
Dim objwritere As New System.IO.StreamWriter(filename, True)
objwritere.WriteLine(quiztaken & " correct answers " & correct & " wrong answers " & wrong & " total mark " & totalmark)
objwritere.Close()
This is what I am using to write to the text file, all variables have values but when I open the .txt file it contains nothing.
Not that great at programming just going off what my teacher gave me.
Half of the code you have provided does not seem to relate to the question you have asked? Below is a simple implementation of writing to a text file.
You seem to be using pupilId and fileName as file path locations which could be causing some confusion?
StreamWriter which implements IDisposable via TextWriter, I recommend making use of it. See below
Dim pupilId As Integer = 1
Dim path As String = String.Format("\\{0}.txt", pupilId)
Using sw As New StreamWriter(filePath,True)
sw.WriteLine("Whatever text you want here")
End Using
I need to pass a string into the ProcessStartInfo for ffmpeg so that I can call the dim.Argument and have it append the strings and variables from code as I parse through the files.
I have the current filename.mp3 that the code sees in movedFileInfo but it won't allow me to append it to the string using & operators... help?
I know there may be one other way to do this utilizing the ffmpeg command in separate function to simply loop through a directory using "for" but I've not found a successful command for running my ffmpeg.exe nor the ffprompt in windows. I also need to append a carriage return when I write to merge.txt but can't find an example... I'm new to vba.
These commands work but vba is complaining about the & operator in my string with the error The operator '&' is not defined for types 'String' and 'System.IO.FileInfo'.
So what I understand is that the string I'm passing into psi.Arguments doesn't like the fact that I'm sending it a string and a variable appended using the & operator... do I simply use a comma or how do I append the variable movedFileInfo to ffmpeg -i? psi is defined above as ProcessStartInfo... so I'm not sure what types vb recognizes for it... I haven't found info on ProcessStartInfo to kick off my ffmpeg exe.
See code below:
Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'videos are from SD card always on I:\Private\AVCHD\BDMV\STREAM\
'store files to RAID drives in folders structured as :
' F:\BUILD\FLIGHT#\CAM#\<TAIL_NUMBER>_FLT_<FLT_NUMBER>_UTC_<START_UTC_TIME>_CAM_<CAM_NUMBER>.MTS
'set base dir as directory F:\
Dim dir As String = "C:\"
Dim video_card_dir As String = "C:\1bak\" '"I:\Private\AVCHD\BDMV\STREAM\"
Directory.SetCurrentDirectory(dir)
Dim new_flightnum_directory = dir & Me.BUILD.Text & "\" & FLT.Text & "\"
'establish new video dir> F: \ BUILD \ FLIGHT # \ CAM # \
Dim new_video_directory = dir & Me.BUILD.Text & "\" & FLT.Text & "\" & Me.CAM.Text & "\"
'establish new filename to rename the video file
' TAIL # FLT #
Dim new_filename As String = TAIL.Text & "_" & FLT.Text & "_" & UTC.Text & "_" & CAM.Text
Dim ffmpeg As String = "C:\ffmpeg\bin\ffmpeg.exe"
'****FFMPEG required variables
Dim psi As ProcessStartInfo = New ProcessStartInfo("C:\ffmpeg\bin\ffmpeg.exe")
Dim proc As Process = Process.Start(psi)
psi.UseShellExecute = True
psi.CreateNoWindow = True
'****end FFMPEG required variables
'!!!!!!!!!!!!!!!!!!!!!!!!!!!need to add the processing below to the IF statement aboev so that if the folders exist, the video processing doesn't attempt to run on existing files
' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~START - MOVE THIS DOWN BELOW CREATION OF FILE STRUCTURE WHEN DOEN DEBUGGING************
'***START MOVING the files from the card to the new directory****
For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")
Dim foundFileInfo As New System.IO.FileInfo(foundFile)
My.Computer.FileSystem.MoveFile(foundFile, new_video_directory & foundFileInfo.Name)
Next
For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")
Dim movedFileInfo As New System.IO.FileInfo(foundFile)
psi.Arguments = "ffmpeg -i " & movedFileInfo & " -vcodec -c libx264 " & movedFileInfo & ".mp4"
psi.ToString()
'proc = Process.Start(psi)
'***convert each MTS file in the new directory to MP4****
'Writes filenames to merge.txt as " path\to\merge.txt , 'file ' F:\path\to\ file1 .MP4 '" so that ffmpeg can merge, then rename
'My.Computer.FileSystem.WriteAllText(new_video_directory & "merge.txt", "file '" & movedFileInfo & ".mp4'" & vbCrLf, True)
'>>>>need to add carriage return to text file
'NOW CAPTURE FILENAMES OF MP4 and MERGE INTO 1 MP4 FILE
' merge all F:\path\to\merge.txt to merge the files & merge them
'psi.Arguments = "ffmpeg -f concat -i " & new_video_directory & "merge.txt -c copy " & new_filename & ".mp4"
proc = Process.Start(psi)
Next
'***END MERGE FILES***
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~* END - MOVE
'***START CREATE STORAGE DIRECTORY STRUCTURE ***
'Verify if the build # directory exists?
If My.Computer.FileSystem.DirectoryExists(dir & Me.BUILD.Text) Then
MessageBox.Show("The build directory exists, moving on to create subdirectories")
Else
Try
'create the new directory F:\ build \ FLIGHT #
My.Computer.FileSystem.CreateDirectory(dir & Me.BUILD.Text)
MessageBox.Show("The build directory" & dir & Me.BUILD.Text & " was created.")
Catch ex As Exception
MessageBox.Show("Doh! The build directory could not be created! Error: " & ex.Message, "Error creating directory.", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
'verify if the flight num directory exists - or create it
If My.Computer.FileSystem.DirectoryExists(new_flightnum_directory) Then
MessageBox.Show("The flight # folder already exists! Check that you have the right Flight #.")
Else
Try
'create the new directory F:\ BUILD \ FLIGHT #
My.Computer.FileSystem.CreateDirectory(new_flightnum_directory)
'Now create new subDirectories
My.Computer.FileSystem.CreateDirectory(new_video_directory)
MessageBox.Show("The new flight directory & video CAM subdirectories have been created! The videos will be moved and files converted now which will take some time.")
Catch ex As Exception
MessageBox.Show("Doh! The flight num or CAM directory could not be created! Error: " & ex.Message, "Error creating directory.", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
'***END CREATE STORAGE DIRECTORY STRUCTURE ***
MessageBox.Show("new merged video file has been created in " & dir & Me.BUILD.Text & "\" & Me.CAM.Text)
End Sub
End Class
The problem is that the & operator in VB is used only to concatenate System.String objects. It's stricter than using + to concatenate. You can't use it to concatenate a String and a System.IO.FileInfo object together. What you want is to get the file name from the FileInfo object. It has an attribute, FileInfo.FullName which will return the full path of the file as a String - from the documentation.
Try instead:
For Each foundFile As String In My.Computer.FileSystem.GetFiles(video_card_dir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.MTS")
Dim movedFileInfo As New System.IO.FileInfo(foundFile)
psi.Arguments = "ffmpeg -i " & movedFileInfo.FullName & " -vcodec -c libx264 " & movedFileInfo.FullName & ".mp4"
psi.ToString()
proc = Process.Start(psi)
Next
If you want to replace the .mts extension with .mp4, instead of simply appending .mp4 as this code does, please see this question.
I'm currently using the Directory.Move method to copy files from one location to another. What i would like to do is only move files with certain extensions (.dbf, .ini & .txt). If the original folder doesn't contain any of these files then i just want to create an empty directory
Current code I'm using is...
Dim n As Integer
If lb1.SelectedItems.Count = 0 Then Exit Sub
For n = 0 To UBound(AllDetails)
If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lb1.SelectedItem Then
If Not My.Computer.FileSystem.DirectoryExists(aMailbox & "\" & AllDetails(n).uFile) Then
Directory.Move(zMailbox & AllDetails(n).uFile, aMailbox & "\" & AllDetails(n).uFile)
lb3.Items.Add(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
Else
lb3.Items.Add(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
Exit Sub
End If
End If
Next
All the variables are declared and this works but moves the entire folder contents
One way to approach this is to use each extension as a search pattern for File.GetFiles, then use Directory.Move on each file returned. Something like this might help:
For Each OldFile As String In (From s In {".dbf", ".ini", ".txt"}
From f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, s)
Select f)
Directory.Move(OldFile, aMailbox & "\" & AllDetails(n).uFile & "\" & Path.GetFileName(OldFile))
Next
if what you want is certain file extensions only? well Open File Dialog can filter that for you
Let's say you call this operation with a button click:
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.Title = "Open File Dialog"
fd.InitialDirectory = "the initial directory you want to look at first"
'this filters the available files to be opened!
fd.Filter = "dbf files|*.dbf*|ini files|*.ini*|Text files|*.txt*"
fd.FilterIndex = 2 'set's the default files to open first as .ini
fd.RestoreDirectory = True
If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Copy the file to the location using the copyer subroutin
resulta.Text = fd.FileName.ToString
Copyer(fd.FileName.Tostring, "the location where you want to copy")
End If
End Sub
Now for the copying of the file, have a look at this subroutine. This subroutine checks first if the file exists. If it does, it deletes it, then copies the new file.
Public Sub Copyer(ByVal theFile As String, ByVal Lokasyon As String)
Try
Dim resultpath As String = Lokasyon
If System.IO.File.Exists(resultpath) = True Then
System.IO.File.Delete(resultpath)
'this deletes the file so you can overwrite it.
End If
My.Computer.FileSystem.CopyFile(theFile, resultpath)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Please let me know if this has helped you.