JavaScript Batch-Renaming in a Folder - rename

I have pairs of strings that need to be replaced in the file names of a given folder, e.g. BUL to Bg-bg, ENG to En-us, etc. There are 12 such pairs. I have found a script to take care of a single pair of replacement but whenever I am trying to add more arguments, it somehow only loops through one and renames just one matching string. Cannot figure our a simpler code that would accept all replacement string pairs at once without creating a separate loop for each pair. Any ideas would be appreciated.
var sFolderName, sStringToFind;
var nResult;
sFolderName = "E:\\Users\\Username\\Desktop\\Sample"; // directory name
sStringToFind1 = "BUL";
sStringToReplace1 = "Bg-bg";
sStringToFind2 = "ENG";
sStringToReplace2 = "En-us";
nResult = renameFiles(sFolderName, sStringToFind1, sStringToReplace1, sStringToFind2, sStringToReplace2);
WScript.Echo(nResult + " files renamed");
// Function Name: renameFiles
// sFolder: Folder Name (use double backslashes)
// sString1: String to search for
// sString2: String to replace
// Returns: Number of files renamed
function renameFiles(sFolder, sString1, sString2, sString3, sString4) {
var oFSO, oFile, oFolder;
var re, index;
var sName;
var i = 0, n;
oFSO = new ActiveXObject("Scripting.FileSystemObject");
oFolder = oFSO.GetFolder(sFolder);
try {
index = new Enumerator(oFolder.Files);
for (; !index.atEnd(); index.moveNext()) {
oFile = index.item();
sName = oFile.Name;
n = sName.indexOf(sString1);
if(n != -1) {
try {
sName = sName.substring(0, n) + sString4 +
sName.substr(n + sString3.length);
oFile.Name = sName;
i++;
} catch(e) {
WScript.Echo("Can not rename file " + sName + " because\n" + e.description);
}
}
}
index = new Enumerator(oFolder.Files);
for (; !index.atEnd(); index.moveNext()) {
oFile = index.item();
sName = oFile.Name;
n = sName.indexOf(sString3);
if(n != -1) {
try {
sName = sName.substring(0, n) + sString4 +
sName.substr(n + sString3.length);
oFile.Name = sName;
i++;
} catch(e) {
WScript.Echo("Can not rename file " + sName + " because\n" + e.description);
}
}
}
}
catch(e) {
WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
return 0;
} finally {
oFSO = null;
re = null;
return i;
}
}

You could use replace() and do all replacements inside the same loop:
function renameFiles(sFolder, sString1, sString2, sString3, sString4) {
var oFSO, oFile, oFolder;
var re, index;
var sName;
var i = 0, n;
oFSO = new ActiveXObject("Scripting.FileSystemObject");
oFolder = oFSO.GetFolder(sFolder);
try {
index = new Enumerator(oFolder.Files);
for (; !index.atEnd(); index.moveNext()) {
oFile = index.item();
sName = oFile.Name;
sName = sName.replace(sString1, sString2);
sName = sName.replace(sString3, sString4);
if (oFile.Name != sName) {
i++;
oFile.Name = sName;
}
}
}
catch(e) {
WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
return 0;
} finally {
oFSO = null;
re = null;
return i;
}
}
Or you could use an array os pairs, and iterate through these pairs and pass any number of pairs...
renameFiles(sFolder, [
[sStringToFind1, sStringToReplace1],
[sStringToFind2, sStringToReplace2],
// ...,
[sStringToFindN, sStringToReplaceN],
]);
function renameFiles(sFolder, arrayOfPairs) {
var oFSO, oFile, oFolder;
var re, index;
var sName;
var i = 0, n;
oFSO = new ActiveXObject("Scripting.FileSystemObject");
oFolder = oFSO.GetFolder(sFolder);
try {
index = new Enumerator(oFolder.Files);
for (; !index.atEnd(); index.moveNext()) {
oFile = index.item();
sName = oFile.Name;
for (var j = 0; j < arrayOfPairs.length; j++) {
sName = sName.replace(arrayOfPairs[j][0], arrayOfPairs[j][1]);
}
if (oFile.Name != sName) {
i++;
oFile.Name = sName;
}
}
}
catch(e) {
WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
return 0;
} finally {
oFSO = null;
re = null;
return i;
}
}

Related

How to create and download a zip file in ASP.NET Core?

We need to bundle a multiple files in a Zip format and download it. Could you please suggest a way to do this in ASP.NET core without using any third party libraries.
In ASP.NET MVC we can achieve this using https://msdn.microsoft.com/en-us/library/system.io.packaging.aspx. Whether is it possible in ASP.NET core 2.0 ?
I think this will help you a lot
protected FileStreamResult DownloadFolder(string path, string[] names, int count)
{
FileStreamResult fileStreamResult;
var tempPath = Path.Combine(Path.GetTempPath(), "temp.zip");
if (names.Length == 1)
{
path = path.Remove(path.Length - 1);
ZipFile.CreateFromDirectory(path, tempPath, CompressionLevel.Fastest, true);
FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
fileStreamResult.FileDownloadName = names[0] + ".zip";
}
else
{
string extension;
string currentDirectory;
ZipArchiveEntry zipEntry;
ZipArchive archive;
if (count == 0)
{
string directory = Path.GetDirectoryName(path);
string rootFolder = Path.GetDirectoryName(directory);
using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
{
for (var i = 0; i < names.Length; i++)
{
currentDirectory = Path.Combine(rootFolder, names[i]);
foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
{
zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, names[i] + filePath.Substring(currentDirectory.Length), CompressionLevel.Fastest);
}
}
}
}
else
{
string lastSelected = names[names.Length - 1];
string selectedExtension = Path.GetExtension(lastSelected);
if (selectedExtension == "")
{
path = Path.GetDirectoryName(Path.GetDirectoryName(path));
path = path.Replace("\\", "/") + "/";
}
using (archive = ZipFile.Open(tempPath, ZipArchiveMode.Update))
{
for (var i = 0; i < names.Length; i++)
{
extension = Path.GetExtension(names[i]);
currentDirectory = Path.Combine(path, names[i]);
if (extension == "")
{
foreach (var filePath in Directory.GetFiles(currentDirectory, "*.*", SearchOption.AllDirectories))
{
zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + filePath, filePath.Substring(path.Length), CompressionLevel.Fastest);
}
}
else
{
zipEntry = archive.CreateEntryFromFile(this.contentRootPath + "\\" + currentDirectory, names[i], CompressionLevel.Fastest);
}
}
}
}
FileStream fileStreamInput = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Delete);
fileStreamResult = new FileStreamResult(fileStreamInput, "APPLICATION/octet-stream");
fileStreamResult.FileDownloadName = "folders.zip";
}
if (File.Exists(tempPath))
{
File.Delete(tempPath);
}
return fileStreamResult;
}

Using Epplus to import data from an Excel file to SQL Server database table

I've tried implementing thishttps://www.paragon-inc.com/resources/blogs-posts/easy_excel_interaction_pt6 on an ASP.NET MVC 5 Application.
//SEE CODE BELOW
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var regPIN = DB.AspNetUsers.Where(i => i.Id == user.Id).Select(i => i.registrationPIN).FirstOrDefault();
if (file != null && file.ContentLength > 0)
{
var extension = Path.GetExtension(file.FileName);
var excelFile = Path.Combine(Server.MapPath("~/App_Data/BulkImports"),regPIN + extension);
if (System.IO.File.Exists(excelFile))
{
System.IO.File.Delete(excelFile);
}
else if (file.ContentType == "application/vnd.ms-excel" || file.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
file.SaveAs(excelFile);//WORKS FINE
//BEGINING OF IMPORT
FileInfo eFile = new FileInfo(excelFile);
using (var excelPackage = new ExcelPackage(eFile))
{
if (!eFile.Name.EndsWith("xlsx"))//Return ModelState.AddModelError()
{ ModelState.AddModelError("", "Incompartible Excel Document. Please use MSExcel 2007 and Above!"); }
else
{
var worksheet = excelPackage.Workbook.Worksheets[1];
if (worksheet == null) { ModelState.AddModelError("", "Wrong Excel Format!"); }// return ImportResults.WrongFormat;
else
{
var lastRow = worksheet.Dimension.End.Row;
while (lastRow >= 1)
{
var range = worksheet.Cells[lastRow, 1, lastRow, 3];
if (range.Any(c => c.Value != null))
{ break; }
lastRow--;
}
using (var db = new BlackBox_FinaleEntities())// var db = new BlackBox_FinaleEntities())
{
for (var row = 2; row <= lastRow; row++)
{
var newPerson = new personalDetails
{
identificationType = worksheet.Cells[row, 1].Value.ToString(),
idNumber = worksheet.Cells[row, 2].Value.ToString(),
idSerial = worksheet.Cells[row, 3].Value.ToString(),
fullName = worksheet.Cells[row, 4].Value.ToString(),
dob = DateTime.Parse(worksheet.Cells[row, 5].Value.ToString()),
gender = worksheet.Cells[row, 6].Value.ToString()
};
DB.personalDetails.Add(newPerson);
try { db.SaveChanges(); }
catch (Exception) { }
}
}
}
}
}//END OF IMPORT
ViewBag.Message = "Your file was successfully uploaded.";
return RedirectToAction("Index");
}
ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
return View();
}
else
{
ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
return View();
}
}
See Picture Error
Any help would be greatly appreciated mates.
you can do like this:
public bool readXLS(string FilePath)
{
FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row; //get row count
string queryString = "INSERT INTO tableName VALUES"; //Here I am using "blind insert". You can specify the column names Blient inset is strongly not recommanded
string eachVal = "";
bool status;
for (int row = 1; row <= rowCount; row++)
{
queryString += "(";
for (int col = 1; col <= colCount; col++)
{
eachVal = worksheet.Cells[row, col].Value.ToString().Trim();
queryString += "'" + eachVal + "',";
}
queryString = queryString.Remove(queryString.Length - 1, 1); //removing last comma (,) from the string
if (row % 1000 == 0) //On every 1000 query will execute, as maximum of 1000 will be executed at a time.
{
queryString += ")";
status = this.runQuery(queryString); //executing query
if (status == false)
return status;
queryString = "INSERT INTO tableName VALUES";
}
else
{
queryString += "),";
}
}
queryString = queryString.Remove(queryString.Length - 1, 1); //removing last comma (,) from the string
status = this.runQuery(queryString); //executing query
return status;
}
}
Details: http://sforsuresh.in/read-data-excel-sheet-insert-database-table-c/

Convert text file into Excel

I need to convert text file to Excel file. I found articles regarding this but my requirement is little different so I am not getting any idea.
I have text file including rows in this format
Jun 13 07:35:08 mail dovecot: pop3-login: Login: user=<veena,.patel#test.com>, method=PLAIN, rip=102.201.122.131, lip=103.123.113.83, mpid=33178, session=<Wfdfdfcxvc>
I want to create Excel file having four columns:-
first column includes "Jun 13 07:35:08" of above row
second column includes "pop3" of above row
third column includes "veena,.patel#test.com"
and fourth column includes "102.201.122.131"
All other data is not required in Excel. How can I do this? I know this is not what I wanted. I should have put some code about what I have tried first, but really I'm not getting any idea.
private void button2_Click(object sender, EventArgs e)
{
try
{
if (!String.IsNullOrWhiteSpace(fileName))
{
if (System.IO.File.Exists(fileName))
{
//string fileContant = System.IO.File.ReadAllText(fileName);
System.Text.StringBuilder sb = new StringBuilder();
List<Country> countries = new List<Country>();
Country country = null;
string line;
string[] arrLine;
System.IO.StreamReader file = new System.IO.StreamReader(fileName);
while ((line = file.ReadLine()) != null)
{
if (!string.IsNullOrWhiteSpace(line))
{
if (line.Contains("rip="))
{
arrLine = line.Split(new string[] { "mail" }, StringSplitOptions.None);
if (arrLine.Length > 1)
{
sb.Append(arrLine[0] + ";");
if (line.Contains("pop3-login:"))
{
sb.Append("pop3;");
}
else if (line.Contains("imap-login:"))
{
sb.Append("imap;");
}
else
{
sb.Append(";");
}
arrLine = line.Split(new string[] { "user=<" }, StringSplitOptions.None);
if (arrLine.Length > 1)
{
arrLine = arrLine[1].Split(new string[] { ">," }, StringSplitOptions.None);
if (arrLine.Length > 1)
{
sb.Append(arrLine[0] + ";");
}
else
{
sb.Append(";");
}
}
else
{
sb.Append(";");
}
arrLine = line.Split(new string[] { "rip=" }, StringSplitOptions.None);
if (arrLine.Length > 1)
{
arrLine = arrLine[1].Split(new string[] { "," }, StringSplitOptions.None);
if (arrLine.Length > 1)
{
sb.Append(arrLine[0] + ";");
country = countries.FirstOrDefault(a => a.IP == arrLine[0]);
if (country != null && !string.IsNullOrWhiteSpace(country.IP))
{
sb.Append(country.Name + ";");
}
else
{
sb.Append(GetCountryByIP(arrLine[0],ref countries) + ";");
}
}
else
{
sb.Append(";;");
}
}
else
{
sb.Append(";;");
}
sb.Append(System.Environment.NewLine);
}
}
}
}
file.Close();
DialogResult dialogResult = saveFileDialog1.ShowDialog();
string saveFileName=Application.StartupPath + #"\data.csv";
if (dialogResult == DialogResult.OK)
{
saveFileName = saveFileDialog1.FileName;
}
System.IO.File.WriteAllText(saveFileName, sb.ToString());
MessageBox.Show("File Save at " + saveFileName);
fileName = string.Empty;
textBox1.Text = string.Empty;
}
else
{
MessageBox.Show("File Not Found");
}
}
else
{
MessageBox.Show("Select File");
}
}
catch (Exception ex)
{
MessageBox.Show("Message:" + ex.Message + " InnerException:" + ex.InnerException);
}
}
If you are up to VBA -after you have watched some tutorials- you need this approach.
1. Get the text file by an EOF method.
2. A UDF for each of your desired criteria using REGEX would be my approach -hint: you can check your regex logic here-.
Here's an example for extracting the email:
Function UserInString(StringToAnalyze As String) As String
Dim regex As Object: Set regex = CreateObject("VBScript.RegExp")
Dim Regexmatches As Variant
Dim ItemMatch As Variant
With regex
.Pattern = "[a-z]{1,99}[,][.][A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"
.Global = True
End With
If regex.Test(StringToAnalyze) = True Then 'there's a user in the string! ' 1. If regex.Test(StringToAnalyze) = True
Set Regexmatches = regex.Execute(StringToAnalyze)
For Each ItemMatch In Regexmatches
UserInString = IIf(UserInString = "", ItemMatch, ItemMatch & "," & UserInString)
Next ItemMatch
Else ' 1. If regex.Test(StringToAnalyze) = True
UserInString = "There's no user in the string!"
End If ' 1. If regex.Test(StringToAnalyze) = True
End Function

Why is parallel.Invoke not working in this case

I have an array of files like this..
string[] unZippedFiles;
the idea is that I want to parse these files in paralle. As they are parsed a record gets placed on a concurrentbag. As record is getting placed I want to kick of the update function.
Here is what I am doing in my Main():
foreach(var file in unZippedFiles)
{ Parallel.Invoke
(
() => ImportFiles(file),
() => UpdateTest()
);
}
this is what the code of Update loooks like.
static void UpdateTest( )
{
Console.WriteLine("Updating/Inserting merchant information.");
while (!merchCollection.IsEmpty || producingRecords )
{
merchant x;
if (merchCollection.TryTake(out x))
{
UPDATE_MERCHANT(x.m_id, x.mInfo, x.month, x.year);
}
}
}
This is what the import code looks like. It's pretty much a giant string parser.
System.IO.StreamReader SR = new System.IO.StreamReader(fileName);
long COUNTER = 0;
StringBuilder contents = new StringBuilder( );
string M_ID = "";
string BOF_DELIMITER = "%%MS_SKEY_0000_000_PDF:";
string EOF_DELIMITER = "%%EOF";
try
{
record_count = 0;
producingRecords = true;
for (COUNTER = 0; COUNTER <= SR.BaseStream.Length - 1; COUNTER++)
{
if (SR.EndOfStream)
{
break;
}
contents.AppendLine(Strings.Trim(SR.ReadLine()));
contents.AppendLine(System.Environment.NewLine);
//contents += Strings.Trim(SR.ReadLine());
//contents += Strings.Chr(10);
if (contents.ToString().IndexOf((EOF_DELIMITER)) > -1)
{
if (contents.ToString().StartsWith(BOF_DELIMITER) & contents.ToString().IndexOf(EOF_DELIMITER) > -1)
{
string data = contents.ToString();
M_ID = data.Substring(data.IndexOf("_M") + 2, data.Substring(data.IndexOf("_M") + 2).IndexOf("_"));
Console.WriteLine("Merchant: " + M_ID);
merchant newmerch;
newmerch.m_id = M_ID;
newmerch.mInfo = data.Substring(0, (data.IndexOf(EOF_DELIMITER) + 5));
newmerch.month = DateTime.Now.AddMonths(-1).Month;
newmerch.year = DateTime.Now.AddMonths(-1).Year;
//Update(newmerch);
merchCollection.Add(newmerch);
}
contents.Clear();
//GC.Collect();
}
}
SR.Close();
// UpdateTest();
}
catch (Exception ex)
{
producingRecords = false;
}
finally
{
producingRecords = false;
}
}
the problem i am having is that the Update runs once and then the importfile function just takes over and does not yield to the update function. Any ideas on what am I doing wrong would be of great help.
Here's my stab at fixing your thread synchronisation. Note that I haven't changed any of the code from the functional standpoint (with the exception of taking out the catch - it's generally a bad idea; exceptions need to be propagated).
Forgive if something doesn't compile - I'm writing this based on incomplete snippets.
Main
foreach(var file in unZippedFiles)
{
using (var merchCollection = new BlockingCollection<merchant>())
{
Parallel.Invoke
(
() => ImportFiles(file, merchCollection),
() => UpdateTest(merchCollection)
);
}
}
Update
private void UpdateTest(BlockingCollection<merchant> merchCollection)
{
Console.WriteLine("Updating/Inserting merchant information.");
foreach (merchant x in merchCollection.GetConsumingEnumerable())
{
UPDATE_MERCHANT(x.m_id, x.mInfo, x.month, x.year);
}
}
Import
Don't forget to pass in merchCollection as a parameter - it should not be static.
System.IO.StreamReader SR = new System.IO.StreamReader(fileName);
long COUNTER = 0;
StringBuilder contents = new StringBuilder( );
string M_ID = "";
string BOF_DELIMITER = "%%MS_SKEY_0000_000_PDF:";
string EOF_DELIMITER = "%%EOF";
try
{
record_count = 0;
for (COUNTER = 0; COUNTER <= SR.BaseStream.Length - 1; COUNTER++)
{
if (SR.EndOfStream)
{
break;
}
contents.AppendLine(Strings.Trim(SR.ReadLine()));
contents.AppendLine(System.Environment.NewLine);
//contents += Strings.Trim(SR.ReadLine());
//contents += Strings.Chr(10);
if (contents.ToString().IndexOf((EOF_DELIMITER)) > -1)
{
if (contents.ToString().StartsWith(BOF_DELIMITER) & contents.ToString().IndexOf(EOF_DELIMITER) > -1)
{
string data = contents.ToString();
M_ID = data.Substring(data.IndexOf("_M") + 2, data.Substring(data.IndexOf("_M") + 2).IndexOf("_"));
Console.WriteLine("Merchant: " + M_ID);
merchant newmerch;
newmerch.m_id = M_ID;
newmerch.mInfo = data.Substring(0, (data.IndexOf(EOF_DELIMITER) + 5));
newmerch.month = DateTime.Now.AddMonths(-1).Month;
newmerch.year = DateTime.Now.AddMonths(-1).Year;
//Update(newmerch);
merchCollection.Add(newmerch);
}
contents.Clear();
//GC.Collect();
}
}
SR.Close();
// UpdateTest();
}
finally
{
merchCollection.CompleteAdding();
}
}

Convert a number in scientific notation to numeric format in Excel using a macro or VBA

MS Excel has eaten my head. It is randomly converting numbers into scientific notation format. This causes a problem when I load the file saved in tab delimited format into SQL Server. I know I can provide a format file and do lot of fancy stuff. But let's say I can't.
Is there a macro which loops over all the cells and if the number in a cell is in scientific notation format then it converts it to numeric format?
Say:
Input: spaces signify different cells.
1.00E13 egalitarian
Output after macro:
10000000000000 egalitarian
I am trying this in Excel 2007.
I wrote a simple C# program to resolve this issue. Hope it's of use.
Input:
Input directory where files reside (assuming files are in .txt format).
Output:
Output directory where converted files will be spit out.
Delimiter:
Column delimiter.
The code
using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
using System.Threading;
namespace ConvertToNumber
{
class Program
{
private static string ToLongString(double input)
{
string str = input.ToString().ToUpper();
// If string representation was collapsed from scientific notation, just return it:
if (!str.Contains("E"))
return str;
var positive = true;
if (input < 0)
{
positive = false;
}
string sep = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
char decSeparator = sep.ToCharArray()[0];
string[] exponentParts = str.Split('E');
string[] decimalParts = exponentParts[0].Split(decSeparator);
// Fix missing decimal point:
if (decimalParts.Length == 1)
decimalParts = new string[] { exponentParts[0], "0" };
int exponentValue = int.Parse(exponentParts[1]);
string newNumber = decimalParts[0].Replace("-","").
Replace("+","") + decimalParts[1];
string result;
if (exponentValue > 0)
{
if(positive)
result =
newNumber +
GetZeros(exponentValue - decimalParts[1].Length);
else
result = "-"+
newNumber +
GetZeros(exponentValue - decimalParts[1].Length);
}
else // Negative exponent
{
if(positive)
result =
"0" +
decSeparator +
GetZeros(exponentValue + decimalParts[0].Replace("-", "").
Replace("+", "").Length) + newNumber;
else
result =
"-0" +
decSeparator +
GetZeros(exponentValue + decimalParts[0].Replace("-", "").
Replace("+", "").Length) + newNumber;
result = result.TrimEnd('0');
}
float temp = 0.00F;
if (float.TryParse(result, out temp))
{
return result;
}
throw new Exception();
}
private static string GetZeros(int zeroCount)
{
if (zeroCount < 0)
zeroCount = Math.Abs(zeroCount);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < zeroCount; i++) sb.Append("0");
return sb.ToString();
}
static void Main(string[] args)
{
//Get Input Directory.
Console.WriteLine(#"Enter the Input Directory");
var readLine = Console.ReadLine();
if (readLine == null)
{
Console.WriteLine(#"Enter the input path properly.");
return;
}
var pathToInputDirectory = readLine.Trim();
//Get Output Directory.
Console.WriteLine(#"Enter the Output Directory");
readLine = Console.ReadLine();
if (readLine == null)
{
Console.WriteLine(#"Enter the output path properly.");
return;
}
var pathToOutputDirectory = readLine.Trim();
//Get Delimiter.
Console.WriteLine("Enter the delimiter;");
var columnDelimiter = (char) Console.Read();
//Loop over all files in the directory.
foreach (var inputFileName in Directory.GetFiles(pathToInputDirectory))
{
var outputFileWithouthNumbersInScientificNotation = string.Empty;
Console.WriteLine("Started operation on File : " + inputFileName);
if (File.Exists(inputFileName))
{
// Read the file
using (var file = new StreamReader(inputFileName))
{
string line;
while ((line = file.ReadLine()) != null)
{
String[] columns = line.Split(columnDelimiter);
var duplicateLine = string.Empty;
int lengthOfColumns = columns.Length;
int counter = 1;
foreach (var column in columns)
{
var columnDuplicate = column;
try
{
if (Regex.IsMatch(columnDuplicate.Trim(),
#"^[+-]?[0-9]+(\.[0-9]+)?[E]([+-]?[0-9]+)$",
RegexOptions.IgnoreCase))
{
Console.WriteLine("Regular expression matched for this :" + column);
columnDuplicate = ToLongString(Double.Parse
(column,
System.Globalization.NumberStyles.Float));
Console.WriteLine("Converted this no in scientific notation " +
"" + column + " to this number " +
columnDuplicate);
}
}
catch (Exception)
{
}
duplicateLine = duplicateLine + columnDuplicate;
if (counter != lengthOfColumns)
{
duplicateLine = duplicateLine + columnDelimiter.ToString();
}
counter++;
}
duplicateLine = duplicateLine + Environment.NewLine;
outputFileWithouthNumbersInScientificNotation = outputFileWithouthNumbersInScientificNotation + duplicateLine;
}
file.Close();
}
var outputFilePathWithoutNumbersInScientificNotation
= Path.Combine(pathToOutputDirectory, Path.GetFileName(inputFileName));
//Create the directory if it does not exist.
if (!Directory.Exists(pathToOutputDirectory))
Directory.CreateDirectory(pathToOutputDirectory);
using (var outputFile =
new StreamWriter(outputFilePathWithoutNumbersInScientificNotation))
{
outputFile.Write(outputFileWithouthNumbersInScientificNotation);
outputFile.Close();
}
Console.WriteLine("The transformed file is here :" +
outputFilePathWithoutNumbersInScientificNotation);
}
}
}
}
}
This works fairly well in case of huge files which we are unable to open in MS Excel.
Thanks Peter.
I updated your original work to:
1) take in an input file or path
2) only write out a processing statement after every 1000 lines read
3) write the transformed lines to the output file as they are processed so a potentially large string is not kept hanging around
4) added a readkey at the end so that console does not exit automatically while debuggging
using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
using System.Threading;
namespace ConvertScientificToLong
{
class Program
{
private static string ToLongString(double input)
{
string str = input.ToString().ToUpper();
// If string representation was collapsed from scientific notation, just return it:
if (!str.Contains("E"))
return str;
var positive = true;
if (input < 0)
{
positive = false;
}
string sep = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
char decSeparator = sep.ToCharArray()[0];
string[] exponentParts = str.Split('E');
string[] decimalParts = exponentParts[0].Split(decSeparator);
// Fix missing decimal point:
if (decimalParts.Length == 1)
decimalParts = new string[] { exponentParts[0], "0" };
int exponentValue = int.Parse(exponentParts[1]);
string newNumber = decimalParts[0].Replace("-", "").
Replace("+", "") + decimalParts[1];
string result;
if (exponentValue > 0)
{
if (positive)
result =
newNumber +
GetZeros(exponentValue - decimalParts[1].Length);
else
result = "-" +
newNumber +
GetZeros(exponentValue - decimalParts[1].Length);
}
else // Negative exponent
{
if (positive)
result =
"0" +
decSeparator +
GetZeros(exponentValue + decimalParts[0].Replace("-", "").
Replace("+", "").Length) + newNumber;
else
result =
"-0" +
decSeparator +
GetZeros(exponentValue + decimalParts[0].Replace("-", "").
Replace("+", "").Length) + newNumber;
result = result.TrimEnd('0');
}
float temp = 0.00F;
if (float.TryParse(result, out temp))
{
return result;
}
throw new Exception();
}
private static string GetZeros(int zeroCount)
{
if (zeroCount < 0)
zeroCount = Math.Abs(zeroCount);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < zeroCount; i++) sb.Append("0");
return sb.ToString();
}
static void Main(string[] args)
{
//Get Input Directory.
Console.WriteLine(#"Enter the Input Directory or File Path");
var readLine = Console.ReadLine();
if (readLine == null)
{
Console.WriteLine(#"Enter the input path properly.");
return;
}
var pathToInputDirectory = readLine.Trim();
//Get Output Directory.
Console.WriteLine(#"Enter the Output Directory");
readLine = Console.ReadLine();
if (readLine == null)
{
Console.WriteLine(#"Enter the output path properly.");
return;
}
var pathToOutputDirectory = readLine.Trim();
//Get Delimiter.
Console.WriteLine("Enter the delimiter;");
var columnDelimiter = (char)Console.Read();
string[] inputFiles = null;
if (File.Exists(pathToInputDirectory))
{
inputFiles = new String[]{pathToInputDirectory};
}
else
{
inputFiles = Directory.GetFiles(pathToInputDirectory);
}
//Loop over all files in the directory.
foreach (var inputFileName in inputFiles)
{
var outputFileWithouthNumbersInScientificNotation = string.Empty;
Console.WriteLine("Started operation on File : " + inputFileName);
if (File.Exists(inputFileName))
{
string outputFilePathWithoutNumbersInScientificNotation
= Path.Combine(pathToOutputDirectory, Path.GetFileName(inputFileName));
//Create the directory if it does not exist.
if (!Directory.Exists(pathToOutputDirectory))
Directory.CreateDirectory(pathToOutputDirectory);
using (var outputFile =
new StreamWriter(outputFilePathWithoutNumbersInScientificNotation))
{
// Read the file
using (StreamReader file = new StreamReader(inputFileName))
{
string line;
int lineCount = 0;
while ((line = file.ReadLine()) != null)
{
String[] columns = line.Split(columnDelimiter);
var duplicateLine = string.Empty;
int lengthOfColumns = columns.Length;
int counter = 1;
foreach (var column in columns)
{
var columnDuplicate = column;
try
{
if (Regex.IsMatch(columnDuplicate.Trim(),
#"^[+-]?[0-9]+(\.[0-9]+)?[E]([+-]?[0-9]+)$",
RegexOptions.IgnoreCase))
{
//Console.WriteLine("Regular expression matched for this :" + column);
columnDuplicate = ToLongString(Double.Parse
(column,
System.Globalization.NumberStyles.Float));
//Console.WriteLine("Converted this no in scientific notation " +
// "" + column + " to this number " +
// columnDuplicate);
if (lineCount % 1000 == 0)
{
Console.WriteLine(string.Format("processed {0} lines. still going....", lineCount));
}
}
}
catch (Exception)
{
}
duplicateLine = duplicateLine + columnDuplicate;
if (counter != lengthOfColumns)
{
duplicateLine = duplicateLine + columnDelimiter.ToString();
}
counter++;
}
outputFile.WriteLine(duplicateLine);
lineCount++;
}
}
}
Console.WriteLine("The transformed file is here :" +
outputFilePathWithoutNumbersInScientificNotation);
Console.WriteLine(#"Hit any key to exit");
Console.ReadKey();
}
}
}
}
}
An easier way would be to save it as a .csv file. Then, instead of just opening the file - you go to the Data tab and select "from text" so that you can get the dialogue box. This way you can identify that column with the scientific notation as text to wipe out that format. Import the file and then you can reformat it to number or whatever you want.

Resources