I'm kind of a noobie when it comes to things like this, and yes I tried google but all the information I got didn't explain how to do so.I'm just messing around with it really, even though I have been trying to fig
I am trying to get a list of files in one directory, and duplicate them all to .html files.
I can't seem to understand how to get the list of files in an string[] array to a foreach function. So I can do Foreach file in directory, File copy as the same name, convert it to .html, then save it again.
I know how to change the type, and get the list. But I can't get the function to list the files, then change the file type foreach file in the directory.
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Duplication;
namespace Tera.Code_Files
{
class Program
{
public static void Main(string[] args)
{
string[] ar = Directory.GetFiles("MyScripts/", "*.txt", System.IO.SearchOption.AllDirectories);
string copy;
MAIN:
Console.WriteLine("This will create an .html from all files in 'MyFiles'. Do you wish to continue? [N/Y] \n");
copy= Console.ReadLine();
switch (copy)
{
case "y":
break;
case "n":
Console.WriteLine("File(s) not changed!");
break;
case "Y":
break;
case "N":
Console.WriteLine("File(s) not changed!");
break;
default: goto MAIN;
}
}
}
}
[/code]
[b] And please be serious about this! [/b]
[cpp] public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("This will create an .html from all files in 'MyScripts'. Do you wish to continue? [N/Y]\n");
switch(char.ToLower(Console.ReadKey()))
{
case 'y':
convertToHtml("MyScripts/");
return;
case 'n':
Console.WriteLine("File(s) not changed!");
return;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
public static void convertToHtml(string path)
{
foreach(string filename in Directory.GetFiles(path, "*.txt", System.IO.SearchOption.AllDirectories))
{
System.IO.FileStream fileS = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
string html;
//do fancy text reading and editing here
System.IO.File.WriteAllText(filename.Substring(0, filename.length - 3) + "html", html);
//also possible to use a string array via File.WriteAllLines
}
}[/cpp]
Took the opportunity to heighten the quality of your existing code :)
[QUOTE=ZeekyHBomb;24499002][cpp] public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("This will create an .html from all files in 'MyScripts'. Do you wish to continue? [N/Y]\n");
switch(char.ToLower(Console.ReadKey()))
{
case 'y':
convertToHtml("MyScripts/");
return;
case 'n':
Console.WriteLine("File(s) not changed!");
return;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
public static void convertToHtml(string path)
{
foreach(string filename in Directory.GetFiles(path, "*.txt", System.IO.SearchOption.AllDirectories))
{
System.IO.FileStream fileS = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
string html;
//do fancy text reading and editing here
System.IO.File.WriteAllText(filename.Substring(0, filename.length - 3) + "html", html);
//also possible to use a string array via File.WriteAllLines
}
}[/cpp]
Took the opportunity to heighten the quality of your existing code :)[/QUOTE]
Aight, Thank you very much. :D
[code]
using System;
using System.IO;
namespace Tera.Code_Files
{
public class Program
{
public static void Main(string[] args)
{
while (true)
{
Console.WriteLine("This will create an .html from all files in 'MyScripts'. Do you wish to continue? [N/Y]\n");
switch (Console.ReadKey().Key)
{
case ConsoleKey.Y:
convertToHtml("MyScripts/");
return;
case ConsoleKey.N:
Console.WriteLine("File(s) not changed!");
return;
case ConsoleKey.Q:
Console.WriteLine("Closing");
return;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
public static void convertToHtml(string path)
{
foreach (string filename in Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories))
{
string newFilename = Path.ChangeExtension(filename, ".html");
File.Copy(filename, newFilename);
}
}
}
}
[/code]
heighten the quality more....
[QUOTE=Nisd;24509547][code]
using System;
using System.IO;
namespace Tera.Code_Files
{
public class Program
{
public static void Main(string[] args)
{
while (true)
{
Console.WriteLine("This will create an .html from all files in 'MyScripts'. Do you wish to continue? [N/Y]\n");
switch (Console.ReadKey().Key)
{
case ConsoleKey.Y:
convertToHtml("MyScripts/");
return;
case ConsoleKey.N:
Console.WriteLine("File(s) not changed!");
return;
case ConsoleKey.Q:
Console.WriteLine("Closing");
return;
default:
Console.WriteLine("Invalid input.");
break;
}
}
}
public static void convertToHtml(string path)
{
foreach (string filename in Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories))
{
string newFilename = Path.ChangeExtension(filename, ".html");
File.Copy(filename, newFilename);
}
}
}
}
[/code]
heighten the quality more....[/QUOTE]
File.Move
Just saying
[editline]10:28AM[/editline]
I do agree, that is much cleaner though
this kind of thing can be done much easier with a simple shell script
[QUOTE=ButtsexV2;24533429]this kind of thing can be done much easier with a simple shell script[/QUOTE]
Yes, But I hate using batch files.
Install Bash.
[QUOTE=Jookia;24553445]Install Bash.[/QUOTE]
Well, let's just say I prefer to stick with C# and nothing else.
I got it working, but for some stupid reason when I tell it to convert the files. It takes the full name: TestFile1.txt, and outputs it: TestFile1.txt.html. How do I fix it?
A dirty method would be to use
[code]
System.Text.RegularExpressions.Regex.Replace(input, ".txt", "");
[/code]
before you add the .html to the filename.
[QUOTE=pikzen;24562954]A dirty method would be to use
[code]
System.Text.RegularExpressions.Regex.Replace(input, ".txt", "");
[/code]
before you add the .html to the filename.[/QUOTE]
We already saw that one could just use System.IO.Path.ChangeExtension
Sorry, you need to Log In to post a reply to this thread.