File not copy to correct directory - vb.net

I am trying to copy a file from a network drive then move to the different folder. Somehow is not moving the file to the directory I specified. After executing the read file code, the file is copy in the same directory.(see pic)
I have try the same code from my local drive and it works fine.
'copy the file
File.Copy(Path.Combine("\\swnas.swmed.org\Phytel\", Dts.Variables("File").Value.ToString), Path.Combine("\\swnas.swmed.org\Phytel\Raw\MCKPP\Processed", insertNewName), True)
'read the file
objStreamReader = New StreamReader(Path.Combine("\\swnas.swmed.org\Phytel\Raw\MCKPP\Processed", insertNewName))
'overwrite the file
objWriter = New StreamWriter(Dts.Variables("File").Value.ToString, False)

I'd take a minute and make sure there are no odd permission rules being applied to the directory you are copying/moving to.

Related

Read and edit the contents of a gzip file?

I am trying to read and edit a file within a gzip file. I can do this with .zip files, but I get an error when trying to read the .gzip file.
Search results so far only talk about compressing or decompressing .gzip files.
If relative, the .gzip file (as per file properties) actually has a ".dat" extension. The file I need to edit within it doesn't have any extension.
Using archive As ZipArchive = Compression.ZipFile.Open(Path.Combine(SelectedWorld, "level.dat"), ZipArchiveMode.Update)
Dim entry As ZipArchiveEntry = archive.GetEntry("level")
Dim s As String = ""
Using sr As New StreamReader(entry.Open())
s = sr.ReadToEnd()
End Using
Dim M As Match = Regex.Match(s, "LevelName")
If M.Success Then
MsgBox(M.Value)
'edit word after "LevelName" <<I'm going to need help with this too.
End If
End Using
The above code throws the following error:
System.IO.InvalidDataException: 'End of Central Directory record could not be found.'
Trying the following doesn't seem to have the right stuff to read/modify a file.
Using archive As IO.Compression.GZipStream = IO.Compression.GZipStream
I have found little else for dealing with these types of files. Any help would be greatly appreciated.
A gzip file is not a zip file. Two entirely different things. The error about not finding an end-of-central-directory record is something only found in a zip file.
A zip file is an archive, with files stored within that are independently compressed. That permits removing and adding some files without having to recompress the other files. Though the whole thing will need to be copied to move things around.
A gzip file stores only one compressed file. If you want to edit that one file, then simply gunzip it, edit it, and then re-gzip it.
Often the one compressed file in a gzip file is itself an uncompressed tar archive of many files. The extension of the file will be .tar.gz. If you want to edit the tar archive, again you would gunzip it to get a .tar file, edit it with the tar command, which can delete and append files, and then re-gzip it.
I got it working using:
Imports Cyotek.Data.Nbt
NewName = InputBox("Enter New Name")
Dim MyFile As String = Path.Combine(SelectedWorld, "level.dat")
Dim document As NbtDocument = NbtDocument.LoadDocument(MyFile)
Dim root As TagCompound = document.DocumentRoot
root.Name = "Data"
Dim TC As TagCompound = root.GetCompound("Data")
Dim MyTag As Tag = TC.GetTag("LevelName")
MyTag.SetValue(NewName)
document.Save(MyFile)

Missing txt file from Visual Basic

I am working in Visual Basic 2017. I have tried to add the file to the Debug folder, but then it just shows that the txt file ienter image description heres missing. I don't have the option under the "Word Solution".. How can I make the file show up? It keeps telling me it doesn't exist.
Dim inFile As IO.StreamReader
Const FileName As String = "words.txt"
Dim subscript As Integer
You can get the path of the directory (Debug or Release or any other) of the *.exe file with:
Dim directory as String = My.Application.Info.DirectoryPath
Using this information, you can then construct the full path with
Dim path As String = IO.Path.Combine(directory, FileName)
If IO.File.Exists(path) Then
...
You can check in Windows File Explorer to see where the file actually is (notice the Copy Path on the ribbon). In File Explorer you will see that the .exe you are running is down 2 directories from the Words Project directory. The double dots in the path is an old DOS way to navigating around directories without having to type out the whole path. This tells the compiler to find the file up 2 directories from the current directory.
For testing purposes this will work. For a release version you could add the file to Resources and access it the same way in any version.
You don't need a stream for a text file. .ReadAllLines returns an array of the lines in the text file
Private Sub OpCode()
Dim words = File.ReadAllLines("..\..\words.txt")
End Sub

Delete directory on network shared folder

This question is bring from another forum which not having answer yet for my situation.
I have something to do on network shared folder. But when I search on Internet, it giving me a code to do in own computer only. Step that I want to do is:
Check destination (network shared folder) path is empty or not.
Delete folder content (not the main one) eg: "\USER-PC\File\"; the folder "File" no need to deleted, but the content inside is need to deleted.
Copy folder content from source to new destination.
No. 1 and 3 is OK. But No. 2 is not yet found. How to delete a content from directory on Network Shared Folder?
Delete directory code that I use but exception "Could not complete operation since directory is a root directory":
My.Computer.FileSystem.DeleteDirectory(strDestination, FileIO.DeleteDirectoryOption.DeleteAllContents)
Please assist
EDITED:
To delete all files inside the main directory:-
Dim directory As New DirectoryInfo(strDestination)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next file
To delete all folders inside the main directory:-
For Each folder As DirectoryInfo In directory.GetDirectories()
folder.Delete(True)
Next folder
Use this instead (it's C#, you'll need to convert it to VB.NET):
DirectoryInfo directory = new DirectoryInfo("\\USER-PC\File");
foreach(FileInfo file in directory.GetFiles()) {
file.Delete();
}

How do I get a text file to be a part of my build?

I wrote a program that reads from text files and can create them to load and save data. I have a few files that are the "default" data that are loaded as soon as the program start. The files are loaded by a static reference. My code runs fine before I publish it, but obviously when I publish it, the static references no longer work. I don't know how to add the default data to the build as distinct text files so that I can still reference it after the build.
I imagine being able to build the program and have some sort of folder that accompanies the executable with the default data files in them that I can easily reference, but I don't know how to do that (or if there is a better way).
Below is the start of the code I use to read from the file. Currently, the default data's file name is passed statically into the sub and is used to identify the file to read from, so I'd like to have a published file that I can do the same thing with.
Try
Dim sr As New IO.StreamReader(FileName)
Dim strLine As String = ""
Do Until sr.EndOfStream
strLine = sr.ReadLine
'Code that interprets the data in the file
Note: I've tried adding the files as "Resources" but I can't seem to reference the file as a text file; I can only retrieve the massive wall of text contained within the document which won't work with the above code (unless of course I'm missing something).
If you could clarify:
How do I add a file to a build so that I can still access it
collectively by a file name?
How will my code reference the files (e.g. by
"My.Resources.filename"?) in the final build?
You can add the file to the build as either a content file or an embedded resource.
For a content file, set the Build Action of the file to 'content', and Copy to Output Directory to 'Copy Always' in the file properties. You can then access the file like this:
FileName = Application.StartupPath() = + FileName
Dim sr As New IO.StreamReader(FileName)
...
To embed the file as a resource you have to set the Build Action of the file to 'Embedded Resource' and Copy to Output Directory to false.
This Microsoft support page has a walkthough about accessing embedded resources. The code would be something like this:
Dim sr As StreamReader
Dim thisAssembly As Assembly
thisAssembly = Assembly.GetExecutingAssembly()
sr = New StreamReader(thisAssembly.GetManifestResourceStream("NameSpace." + FileName))
Dim strLine As String = ""
Do Until sr.EndOfStream
strLine = sr.ReadLine
'Code that interprets the data in the file
...
Replace NameSpace with the namespace of your application (Project Properties -> Application -> root namespace)
You also have to add Imports System.Reflection at the top of your code file.
Using an embedded resource has the advantage of less files to manage, and you don't have to keep track of paths.

How to use Process.Start

I'm using process.Start to run Convert.exe. This program's purpose is to convert all files which are in the exe's folder. So when I normally use it, I copy paste a file into the same folder as Convert.exe and then run Convert.exe. Convert.exe will create a new "converted" file in the same folder.
I'm trying to automate this tedious process. A User selects a file which needs to be converted from FolderA, I copy it to the same folder where Convert.exe is and I'm using process.start(Convert.exe) to run it.
Just to be clear, this "Convert.exe" accepts NO arguments.
The problem: "Convert.exe" is not converting the files in its folder. Instead it's converting all the files in FolderA for some weird reason. I don't know why it picked that folder, I never even try to send it as an argument or nothing.
Here's the code I have:
Dim techInfo As New System.IO.FileInfo(itm.strFilePath)
techInfo.CopyTo(ConverterPath & techInfo.Name)
Dim procInfoConvert As New ProcessStartInfo
procInfoConvert.CreateNoWindow = False
procInfoConvert.Arguments = ""
procInfoConvert.FileName = ConverterPath & "Convert.exe"
Dim procConvert As Process = Process.Start(procInfoConvert)
I did a test where I copy pasted a file into the "Convert.exe" folder and then just run this code:
process.start(ConverterPath & "Convert.exe")
The exe returns nothing, same as if there was no files in the folder.
The only thing I can think of is that when process.Start is run, it copies the file to another location and runs it from there.
Any ideas anyone?
Try this:
procInfoConvert.WorkingDirectory = ConverterPath
That'll set the process up to start in the directory it's contained in, instead of the current directory.