I had some codes to split files and join ,Now am trying to make a program that split file from a url and download , for example http://tegos.ru/new/mp3_full/David_Guetta_feat_Ne-Yo_and_Akon_-_Play_Hard.mp3 spilit this file in two pieces and download
my split-er code
Byte[] byteSource = System.IO.File.ReadAllBytes(FileInputpath);
FileInfo fiSource = new FileInfo(txtPath.Text);
int partsize = (int)Math.Ceiling((double)(fiSource.Length / OutputFiles));
int fileOffset = 0;
string currPartPath;
FileStream fsPart;
int sizeReamining = (int)fiSource.Length;
for (int i = 0; i < OutputFiles; i++)
{
currPartPath = FolderOutputPath + "\\" + fiSource.Name + "." + String.Format(#"{0:D4}", i) + ".gparts";
if (!File.Exists(currPartPath))
{
fsPart = new FileStream(currPartPath, FileMode.CreateNew);
sizeReamining = (int)fiSource.Length - (i * partsize);
if (sizeReamining < partsize)
{
partsize = sizeReamining;
}
fsPart.Write(byteSource, fileOffset, partsize);
fsPart.Close();
fileOffset += partsize;
}
}
Use HttpWebRequest to get a stream from the specified url.
When you have the stream, you can use your previous code.
Download/Stream file from URL - asp.net
Related
When we are copying images in one folder to another folder, images are going to copy one by one, then it takes more time when thousands's of images are copying, Is there any Possibility to copy Multiple images at a time? "This is My code"
int avilableCharts = 0;
int unavialableCharts = 0;
string chartid;
int count = 0;
StreamReader rd = new StreamReader(txtFileName.Text);
StreamWriter tw = new StreamWriter("C:\\LogFiles\\SucessfullyMovedImages.txt");
StreamWriter tw1 = new StreamWriter("C:\\LogFiles\\UnavailableImages.txt");
DirectoryInfo dirinfo = new DirectoryInfo(txtSourceFolder.Text);
FileInfo[] file = dirinfo.GetFiles("*.pdf");
while (!rd.EndOfStream)
{
chartid = rd.ReadLine() + ".pdf";
count = count + 1;
worker.ReportProgress(count);
string FName = string.Empty;
if (File.Exists(txtSourceFolder.Text + chartid))
{
File.Copy(txtSourceFolder.Text + chartid , txtDestinationFolder.Text + chartid );
avilableCharts = avilableCharts + 1;
tw.WriteLine(chartid);
}
else
{
unavialableCharts = unavialableCharts + 1;
tw1.WriteLine(chartid);
}
}
tw.Close();
tw1.Close();
MessageBox.Show("Successfully Copied Images are :" + avilableCharts);
MessageBox.Show("Total Unavilable Images are : " + unavialableCharts);
use below code :
public class SimpleFileMove
{
static void Main()
{
string sourceFile = #"C:\Users\Public\public\test.txt";
string destinationFile = #"C:\Users\Public\private\test.txt";
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
// To move an entire directory. To programmatically modify or combine
// path strings, use the System.IO.Path class.
System.IO.Directory.Move(#"C:\Users\Public\public\test\", #"C:\Users\Public\private");
}
}
File A B contains million urls.
1, go through the url in file A one by one.
2, extract subdomain.com (http://subdomain.com/path/file)
3, if subdomain.com exist file B, save it to file C.
Any quickest way to get file C with c#?
Thanks.
when i use readline, it have no much different.
// stat
DateTime start = DateTime.Now;
int totalcount = 0;
int n1;
if (!int.TryParse(num1.Text, out n1))
n1 = 0;
// memory
dZLinklist = new Dictionary<string, string>();
// read file
string fileName = openFileDialog1.FileName; // get file name
textBox1.Text = fileName;
StreamReader sr = new StreamReader(textBox1.Text);
string fullfile = File.ReadAllText(#textBox1.Text);
string[] sArray = fullfile.Split( '\n');
//IEnumerable<string> sArray = tool.GetSplit(fullfile, '\n');
//string sLine = "";
//while (sLine != null)
foreach ( string sLine in sArray)
{
totalcount++;
//sLine = sr.ReadLine();
if (sLine != null)
{
//string reg = "http[s]*://.*?/";
//Regex R = new Regex(reg, RegexOptions.Compiled);
//Match m = R.Match(sLine);
//if(m.Success)
int length = sLine.IndexOf(' ', n1); // default http://
if(length > 0)
{
//string urls = sLine.Substring(0, length);
dZLinklist[sLine.Substring(0,length)] = sLine;
}
}
}
TimeSpan time = DateTime.Now - start;
int count = dZLinklist.Count;
double sec = Math.Round(time.TotalSeconds,2);
label1.Text = "(" + totalcount + ")" + count.ToString() + " / " + sec + " = " + (Math.Round(count / sec,2)).ToString();
sr.Close();
I would go for using Microsoft LogParser for processing big files: MS LogParser. Are you limited to implement it in described way only?
Having problems trying to save a txt file in a specific folder, the name of the txt file is inserted by the user,the directory is created with no problems, but the txt file isn't saved, I can only get to save the text file when I don't use the folder (which is not what I want), any ideas on this? any help would be appreciated.
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string directory = "data";
if (!myIsolatedStorage.DirectoryExists(directory))
{
myIsolatedStorage.CreateDirectory(directory); //create the folder where the data will be placed
}
string name = textbox1.Text;
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(directory+"\\"+name+".txt", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
int total_number_of_points=(App.Current as App).number_of_random_points + (App.Current as App).number_of_foresight_points;
writeFile.WriteLine("Point East North Height STD-E STD-N STD-H Longitude Latitude Description");
for (int n = 1; n <= total_number_of_points; n++)
{
string point_data = "";
for (int i = 0; i < 8; i++)
{
point_data = point_data + " " + Convert.ToString((App.Current as App).point_information[n, i]);
}
point_data = point_data + " " + Convert.ToString((App.Current as App).point_description[n]);
writeFile.WriteLine(" " + Convert.ToString(n) + " " + point_data);
}
writeFile.Close();
}
I am trying to download a file from dropbox using the RESTful API. When I encounter a file that has a space in it "My Photo.png" the program stops on var request = (HttpWebRequest) WebRequest.Create(requestUri); It returns a 403 error. If I remove the spaces and try the file download again it works perfectly. Ive checked the formatted uri and it is being returned as "My+Photo.png" is this how it should be? What am I doing wrong?
var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer),
String.Format("files?root={0}&path={1}",
root, UpperCaseUrlEncode(path)));
My Method:
private static string UpperCaseUrlEncode(string s)
{
char[] temp = HttpUtility.UrlEncode(s).ToCharArray();
for (int i = 0; i < temp.Length - 2; i++)
{
if (temp[i] == '%')
{
temp[i + 1] = char.ToUpper(temp[i + 1]);
temp[i + 2] = char.ToUpper(temp[i + 2]);
}
}
return new string(temp);
}
Could you do the normal URLEncode and do a string.Replace on the temp string it will work perfect for files with a space in the file name
temp = temp.Replace("+", "%20");
What I have going on is this:
1) Reading a directory of files
2) Writing out to a text file the filenames + "|"
3) Where i'm stuck.....
I have a bunch of files named... and need to be converted corispondingly:
Apple0154~3.Txt convertedTO -> Apple0156.txt
Apple0136~31.txt convertedTO -> Apple0166.txt
The prefix is always apple so it kinda goes like:
Apple (always the same prefix).
The numbers match is # + ~ subnumber -1
always in in .txt
I'm sure this is confusing i'm using this code but i cant figured out how to get this resulting textfile:
Apple0154~3.Txt|Apple0156.txt
Apple0136~31.txt|Apple0166.txt
{
string resultingfile = ***This is what i dont know***
string movedredfolder = (overlordfolder + "\\redactions\\");
DirectoryInfo movedredinfo = new DirectoryInfo(movedredfolder);
using (StreamWriter output = new StreamWriter(Path.Combine(movedredfolder, "Master.txt")))
{
foreach (FileInfo fi in movedredfolder)
{
output.WriteLine(Path.GetFileName(fi)+"|"+resultingfile);
}
}
}
Ok, I see what you are trying to do.
Try using Regular expressions to grab the 2 numbers out of the original file name. Something like:
Regex r = new Regex(#"Apple(\d+)~(\d+)\.txt");
Match mat = r.Match(filename);
if( !mat.Success )
{
// Something bad happened...
return;
}
int one = int.Parse(mat.Groups[1].Value);
int two = int.Parse(mat.Groups[2].Value);
int num = one + (two-1);
string newFilename = "Apple"+num.ToString("0000")+".txt";
Inside the foreach loop:
string fileName = Path.GetFileName(fi);
string[] parts = fileName.Split('~', '.');
int basenum = int.Parse(parts[0].Substring(6));
int offset = int.Parse(parts[1]);
string resultingfile = string.Format("Apple{0:0000}.txt", basenum+offset-1);
euh something like:
string path = Path.GetFileName(fi);
int indexOfTilde = path.IndexOf('~');
int indexOfPoint = path.LastIndexOf('.');
int length = indexOfPoint -indexOfTilde;
string tmp = path.SubString(indexOfTilde+1, length);
int numberToIncrease = Convert.ToInt32(tmp) - 1;
int baseNumber = Convert.ToInt32(path.SubString(5, indexOfTilde-4);
string newPath = "Apple" + (baseNumber + numberToIncrease ) + ".txt";
and you can use the FileInfo.MoveTo for file movement :)
good luck!
edit: damn... too slow typing of me...
Ok, this should work for one file:
String filename = "Apple0154~3.Txt";
Regex re = new Regex(#"Apple(?<num>\d+)\~(?<add>\d+)");
Int32 num = Int32.Parse(re.Match(filename).Groups["num"].Value);
Int32 add = Int32.Parse(re.Match(filename).Groups["add"].Value);
Int32 rez = num + (add - 1);
MessageBox.Show("Apple" + rez + ".txt");
using (var output = new StreamWriter(Path.Combine(movedredfolder, "Master.txt")))
{
foreach (var filePath in Directory.GetFiles(directoryPath))
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
var fileExtension = Path.GetExtension(filePath);
var index = fileName.IndexOf('~');
var firstNumber = Int32.Parse(fileName.Substring(5, index - 1));
var secondNumber = Int32.Parse(fileName.Substring(index + 1)) - 1;
output.Write("Apple0" +
(firstNumber + secondNumber).ToString() +
fileExtension + "|"
);
}
}