I have Bluray folder structure
- BMDV
- ANY!
- FAB!
...
I use library https://github.com/DiscUtils/DiscUtils and I tried:
CDBuilder builder = new CDBuilder();
builder.UseJoliet = false;
AddDir("paths", builder, "paths");
builder.Build("stuff.iso");
static void AddDir(string dirPath, CDBuilder builder, string originalPath)
{
var files = Directory.GetFiles(dirPath);
var dirs = Directory.GetDirectories(dirPath);
var pathDiff = string.Join(separator, dirPath.Split(separator).Except(originalPath.Split(separator)));
if (!string.IsNullOrWhiteSpace(pathDiff))
{
Console.WriteLine($"Adding directory {pathDiff}");
builder.AddDirectory(pathDiff);
}
foreach (var file in files)
{
string location = string.IsNullOrWhiteSpace(pathDiff) ?
Path.GetFileName(file) :
$"{pathDiff}{separator}{Path.GetFileName(file)}";
Console.WriteLine($"Adding file: {location}");
builder.AddFile(location, file);
}
foreach (var dir in dirs)
{
AddDir(dir, builder, originalPath);
}
}
When open ISO with BDInfo (https://www.videohelp.com/software/BDInfo), it says that is not valid UDF stream.
How to create UDF ISO file ?
Related
I'm learning c# and i have a task to:
Sync content of the two directories.
Given the paths of the two dirs - dir1 and dir2,then dir2 should be synchronized with dir 1:
If a file exists in dir1 but not in dir2,it should be copied
-if a file exists in dir1 and in dir2,but content is changed,then file from dir1 should overwrite the one from dir2
-if a file exists in dir2 but not in dir1 it should be removed
Notes: files can be extremly large
-dir1 can have nested folders
hints:
-read and write files async
-hash files content and compare hashes not content
I have some logic on how to do this,but i dont know how to implement it.I googled the whole internet to get a point to start,but unsuccesseful.
I started with this:
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
string sourcePath = #"C:\Users\artio\Desktop\FASassignment\root\dir1";
string destinationPath = #"C:\Users\artio\Desktop\FASassignment\root\dir2";
string[] dirsInSourcePath = Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories);
string[] dirsInDestinationPath = Directory.GetDirectories(destinationPath, "*", SearchOption.AllDirectories);
var filesInSourcePath = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories);
var filesInDestinationPath = Directory.GetFiles(destinationPath,"*",SearchOption.AllDirectories);
//Directories in source Path
foreach (string dir in dirsInSourcePath)
{
Console.WriteLine("sourcePath:{0}", dir);
Directory.CreateDirectory(dir);
}
//Directories in destination path
foreach (string dir in dirsInDestinationPath)
{
Console.WriteLine("destinationPath:{0} ", dir);
}
//Files in source path
foreach (var file in filesInSourcePath)
{
Console.WriteLine(Path.GetFileName(file));
}
//Files in destination path
foreach (var file in filesInDestinationPath)
{
Console.WriteLine(Path.GetFileName(file));
}
}
}
As i understand,i should check if in dir1 are some folders and files,if true,copy them in folder 2,and so on,but how to do this? i'm burning my head out two days already and have no idea.. please help.
Edit: For the first and second point i got a solution. :
public static void CopyFolderContents(string sourceFolder, string destinationFolder, string mask, Boolean createFolders, Boolean recurseFolders)
{
try
{
/*if (!sourceFolder.EndsWith(#"\")) { sourceFolder += #"\"; }
if (!destinationFolder.EndsWith(#"\")) { destinationFolder += #"\"; }*/
var exDir = sourceFolder;
var dir = new DirectoryInfo(exDir);
SearchOption so = (recurseFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
foreach (string sourceFile in Directory.GetFiles(dir.ToString(), mask, so))
{
FileInfo srcFile = new FileInfo(sourceFile);
string srcFileName = srcFile.Name;
// Create a destination that matches the source structure
FileInfo destFile = new FileInfo(destinationFolder + srcFile.FullName.Replace(sourceFolder, ""));
if (!Directory.Exists(destFile.DirectoryName) && createFolders)
{
Directory.CreateDirectory(destFile.DirectoryName);
}
if (srcFile.LastWriteTime > destFile.LastWriteTime || !destFile.Exists)
{
File.Copy(srcFile.FullName, destFile.FullName, true);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);
}
}
It's not perfect,but it works.How this function should be improved: add async copy,and compare hashes of files not to copy again the identical ones. How to do it?
So,after some time of much more research,i came up with this solution:
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string sourcePath = #"C:\Users\artio\Desktop\FASassignment\root\dir1";
string destinationPath = #"C:\Users\artio\Desktop\FASassignment\root\dir2";
var source = new DirectoryInfo(sourcePath);
var destination = new DirectoryInfo(destinationPath);
CopyFolderContents(sourcePath, destinationPath, "", true, true);
DeleteAll(source, destination);
}
public static void CopyFolderContents(string sourceFolder, string destinationFolder, string mask, Boolean createFolders, Boolean recurseFolders)
{
try
{
var exDir = sourceFolder;
var dir = new DirectoryInfo(exDir);
var destDir = new DirectoryInfo(destinationFolder);
SearchOption so = (recurseFolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
foreach (string sourceFile in Directory.GetFiles(dir.ToString(), mask, so))
{
FileInfo srcFile = new FileInfo(sourceFile);
string srcFileName = srcFile.Name;
// Create a destination that matches the source structure
FileInfo destFile = new FileInfo(destinationFolder + srcFile.FullName.Replace(sourceFolder, ""));
if (!Directory.Exists(destFile.DirectoryName) && createFolders)
{
Directory.CreateDirectory(destFile.DirectoryName);
}
//Check if src file was modified and modify the destination file
if (srcFile.LastWriteTime > destFile.LastWriteTime || !destFile.Exists)
{
File.Copy(srcFile.FullName, destFile.FullName, true);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace);
}
}
private static void DeleteAll(DirectoryInfo source, DirectoryInfo target)
{
if (!source.Exists)
{
target.Delete(true);
return;
}
// Delete each existing file in target directory not existing in the source directory.
foreach (FileInfo fi in target.GetFiles())
{
var sourceFile = Path.Combine(source.FullName, fi.Name);
if (!File.Exists(sourceFile)) //Source file doesn't exist, delete target file
{
fi.Delete();
}
}
// Delete non existing files in each subdirectory using recursion.
foreach (DirectoryInfo diTargetSubDir in target.GetDirectories())
{
DirectoryInfo nextSourceSubDir = new DirectoryInfo(Path.Combine(source.FullName, diTargetSubDir.Name));
DeleteAll(nextSourceSubDir, diTargetSubDir);
}
}
}
It does everything it should,the only missing points are the async copy and sha comparison,but at least i have a solution.
My folder structure is as .. temp\2016\09\11\16
In the last folder I have multiple text/json files.
I would like to run a console application that would loop through every folder and rename the files with foldernames concatenated.
So a file abcd.json would become 2016091116_abcd.json
Then copy this file to another folder.
I tried..
using System;
using System.IO;
using System.Collections;
namespace concatdf
{
class Program
{
static void Main(string[] args)
{
string folderPath = "D:\\500GBNP\\Projects\\temp";
DirectoryInfo startDir = new DirectoryInfo(folderPath);
RecurseFileStructure recurseFileStructure = new RecurseFileStructure();
recurseFileStructure.TraverseDirectory(startDir);
}
public class RecurseFileStructure
{
public void TraverseDirectory(DirectoryInfo directoryInfo)
{
var subdirectories = directoryInfo.EnumerateDirectories();
foreach (var subdirectory in subdirectories)
{
TraverseDirectory(subdirectory);
}
var files = directoryInfo.EnumerateFiles();
foreach (var file in files)
{
HandleFile(file);
}
}
void HandleFile(FileInfo file)
{
string destfolderPath = "D:\\500GBNP\\Projects\\temp1\\";
file.CopyTo(destfolderPath+file.Name);
}
}
}
}
With the above code I'm able to traverse and copy all files to target directory but file names do not get concatenated with foldernames.
So a file abcd.json in folder temp\2016\09\11\16 would become 2016091116_abcd.json and all files get copied to temp1 folder.
I would sincerely appreciate if someone could help.
You can append the folder name in each recursion and append the destination filename.
using System;
using System.IO;
using System.Collections;
namespace concatdf
{
class Program
{
static void Main(string[] args)
{
string folderPath = "D:\\500GBNP\\Projects\\temp";
DirectoryInfo startDir = new DirectoryInfo(folderPath);
RecurseFileStructure recurseFileStructure = new RecurseFileStructure();
recurseFileStructure.TraverseDirectory(startDir, string.Empty);
}
public class RecurseFileStructure
{
public void TraverseDirectory(DirectoryInfo directoryInfo, string fileAppend)
{
var subdirectories = directoryInfo.EnumerateDirectories();
foreach (var subdirectory in subdirectories)
{
TraverseDirectory(subdirectory, fileAppend + subdirectory.Name);
}
var files = directoryInfo.EnumerateFiles();
foreach (var file in files)
{
HandleFile(file, fileAppend);
}
}
void HandleFile(FileInfo file, string fileAppend)
{
string destfolderPath = "D:\\500GBNP\\Projects\\temp1\\";
file.CopyTo(destfolderPath + fileAppend +"_"+ file.Name);
}
}
}
}
We just need to make HandleFile a bit more intelligent. Let's take the last 4 folder names and add it to the name..
void HandleFile(FileInfo file)
{
string destfolderPath = "D:\\500GBNP\\Projects\\temp1\\";
var pathBits = file.DirectoryName.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var s = string.Concat(pathBits[^4..]);
file.CopyTo(Path.Combine(destfolderPath, s+'_'+file.Name));
}
This renames the file as it copies (which seemed a more sensible way to go, to me). If you truly want to rename the file before you copy, insert a call to FileInfo.MoveTo() after you copy
Strive to use Path when working with paths, not string concatenation
What about something simpler like this?:
public static void CopyFiles(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory)
{
// This will provide all files in sourceDirectory and nested directories
foreach (var file in sourceDirectory.EnumerateFiles("*", SearchOption.AllDirectories))
{
// work out what the relative path from sourceDirectory is
// e.g. 2016\09\11\16\abcd.json
string relativePath = Path.GetRelativePath(sourceDirectory.FullName, file.FullName);
// get the directory part and replace the separator with an empty string (this could be
// made more efficient)
string directoryPart = Path.GetDirectoryName(relativePath)
.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
// get just the filename
string filePart = Path.GetFileName(relativePath);
// combine the target directory, the directory part, and the filename part
// I've made the assumption that you don't want files in the base directory
// to be called _filename, so we just use filePart when there is no directoryPart
string newFileName = string.IsNullOrEmpty(directoryPart) ? filePart : $"{directoryPart}_{filePart}";
string newFullName = Path.Combine(targetDirectory.FullName, newFileName);
// copy the file to the new location
file.CopyTo(newFullName);
}
}
Using SQL Server 2014 \ VS 2019. I have the below C# script task to delete files in a folder older than x number of days, and this works.
public void Main()
{
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod)))
{
currFileInfo.Delete();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
However, how do I modify to just say, delete all files in the directory where the filenames begins with 'ABC'?
Define your start with "PREFIX" in a string something like StartsWithPrefix. Use the String.StartsWith Method to check if the FileInfo Name has the defined prefix needed to meet the requirements.
int RetentionPeriod = Convert.ToInt32(Dts.Variables["$Package::FileRententionDays"].Value.ToString());
string directoryPath = Dts.Variables["CSV_ArchivePath"].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, "*.*");
string StartsWithPrefix = "ABC";
foreach (string currFile in oldFiles)
{
FileInfo currFileInfo = new FileInfo(currFile);
if (currFileInfo.LastWriteTime < (DateTime.Now.AddDays(-RetentionPeriod))
&& currFileInfo.Name.StartsWith(StartsWithPrefix))
{
currFileInfo.Delete();
}
}
https://learn.microsoft.com/en-us/dotnet/api/system.string.startswith?view=netcore-3.1
https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo.name?view=netcore-3.1
I want to compress Zip to be same structure with folder.
but, ZipFile API seems unable to compress folder.
How to compress these folder structure?
The following code can't compress folder itself.
using (ZipArchive archive = ZipFile.Open(Path.Combine(m_strWorkingDirectory, "build.zip"), ZipArchiveMode.Create))
{
foreach( string path in m_listTargetPath )
{
string strPath = Path.Combine(m_strWorkingDirectory, path);
archive.CreateEntryFromFile(strPath, path);
}
}
If ZipFile.CreateFromDirectory() doesn't do what you need (I thought it would) then you can just get all the files/folders you need and add them in using an extension method:
public static class FileExtensions
{
public static IEnumerable<FileSystemInfo> AllFilesAndFolders(this DirectoryInfo dir)
{
foreach (var f in dir.GetFiles())
{
yield return f;
}
foreach (var d in dir.GetDirectories())
{
yield return d;
foreach (var o in AllFilesAndFolders(d))
{
yield return o;
}
}
}
}
And then using your same format, you should be able to do something like so (not tested):
DirectoryInfo dir = new DirectoryInfo(m_strWorkingDirectory);
using (ZipArchive archive = ZipFile.Open(Path.Combine(m_strWorkingDirectory, "build.zip"), ZipArchiveMode.Create))
{
foreach (FileInfo file in dir.AllFilesAndFolders().Where(o => o is FileInfo).Cast<FileInfo>())
{
var relPath = file.FullName.Substring(dir.FullName.Length + 1);
archive.CreateEntryFromFile(file.FullName, relPath);
}
}
I am trying Mono on a linux ubuntu.
Enumartion of Files wont work with subdirectories. If i use GetFiles() it will work.
I used a console to get the output of the files so I wont get the enumerat output:
[Test]
public void NotImportentTest ()
{
Directory.CreateDirectory("test");
Directory.CreateDirectory("test/dings");
File.Create("test/dings/hallo").Close();
var curdir = Directory.GetCurrentDirectory();
var searchdir = new DirectoryInfo(curdir + "/test");
var files = searchdir.EnumerateFiles("*",SearchOption.AllDirectories);
foreach (var file in files)
{
Console.WriteLine("Enumerate:{0} in {1}\r\n", file, curdir);
}
foreach (var file in searchdir.GetFiles("*",SearchOption.AllDirectories))
{
Console.WriteLine("GetFiles:{0} in {1}\r\n", file, curdir);
}
}