how to handle a duplicate file while placing a same file existing folder in c#.net - vb.net

here i am reading files from one root folder and after reading that we are moving that particular file Success folder and taking a copy of that in to back up folder normal
while reading file i will check file naming convention by targeting backup folder if suppose it was exists in backup folder then i m moving the file to duplicate folder
my problem if suppose again same file came to process this file already existed in duplicate folder how do send the file in to duplicate folder, unfornately i dint have any property as file rename any way can anybody give some suggestion to resolve from issue please.
If File.Exists(Swift_Backup + "\" + Path.GetFileName(CBFile)) Then
' File.Move(CBFile, Swift_Duplicate + "\\" + Path.GetFileName(CBFile)) 'DUPLICATE FOLDER'
' File.Copy(oldFileName, NewFileName);
File.Copy(CBFile, Swift_Duplicate + "\\" + Path.GetFileNameWithoutExtension(CBFile) + "_" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + Path.GetExtension(CBFile)) 'DUPLICATE FOLDER'
File.Delete(CBFile)
END IF
i have tried like this but was not worked out for am i did any mistake
Swift_Duplicate --> duplicate folder name
Swift_Backup --> backup folder name
CBFile --> string filename will be there in cbfile, it will fetch from the root folder

Renaming a file is the same as moving it to the same directory with a different name. For example:
File.Move("C:\\docs\\new.doc", "C:\\docs\\old.doc");
Also, consider using FileInfo class:
FileInfo file = new FileInfo("C:\\docs\\new.doc");
file.MoveTo("C:\\docs\\new.doc");
FileInfo copy = file.CopyTo("C:\\docs\\old.doc");

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)

File not copy to correct directory

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.

using File.Move to copy file from source folder to destination folder changes my file owner

I am moving files from source folder to destination folder using File.Move, when files got moved to destination folder file owner name is getting changed to Administrator instead of original user I tried below code but unable to set file owner in destination folder. My application is a windows service
My Code:
Dim fOwner = File.GetAccessControl(srcFolder + "\" + srcFileName).GetOwner(GetType(System.Security.Principal.SecurityIdentifier))
After Moving file I am setting it to original owner
File.GetAccessControl(destFolder + "\" + destFileName).SetOwner(fOwner)
but it still shows owner as administrator. Tried lot of examples but no luck.
I believe you need to also call SetAccessControl to apply the owner change. Try something like this:
Dim fs as FileSecurity = File.GetAccessControl(Path.Combine(destFolder, destFileName))
fs.SetOwner(fOwner)
File.SetAccessControl(Path.Combine(destFolder, destFileName),fs)

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 you call files without drive path

I don't know how to explain this right but I would like to ask you guys how do you call all the files in a folder without the "C:\" directory so wherever I move my project in another computer I don't have to edit the path directory in my project is that even possible? I'm using visual basic 2008.
I would advice to use AppDomain.CurrentDomain.BaseDirectory , this code will return the directory where the current application is located, lets say you want to get all the directories in the folder where the application is located, then you will get something like this.
Dim currentpath As String = AppDomain.CurrentDomain.BaseDirectory 'Get the directory where the application is located.
Dim Directories() As String = Directory.GetDirectories(currentpath) 'get all the directories what are in the current location of the application
'
Console.WriteLine("This application is located at:: {0}", currentpath)
'
If Directories.Length = 0 Then
Console.WriteLine("There aren't any folders found in the location of the application.")
Else
'
Console.WriteLine("The follow folder(s) are found.")
'
For Each folder In Directory.GetDirectories(currentpath)
Console.WriteLine(folder)
Next
'
End If
'
Console.ReadLine()
Output:
This application is located at :: C:Users\Kona\Desktop\
The follow folder(s) are found.
C:\Users\Kona\Desktop\C#
C:\Users\Kona\Desktop\VB
C:\Users\Kona\Desktop\Haskell
C:\Users\Kona\Desktop\Java
You can easily just use the:
Environment.CurrentDirectory
variable.
(use ..\ to go up a directory...)