Read and Write text file using batch script

February 16, 2012 at 09:14:15
Specs: Windows 7
I'm very new to batch scripting and I would like to do the following:
1. For all txt files found in a specified directory
2. Read in one txt file at a time
3. Take 9 line of that txt file and duplicate is 27 times
4. Write finished file to directory.

I have this code in c# but dont know how to do this in batch scripting. Any help or point of direction is appreciated. Thank you!

The algorithm goes something like this:

while (true) // infinite loop
{
// read next batch of 9 or whatever's left in file
while (counter < 9 && (line = sr.ReadLine()) != null)
{
correctStr = line.Replace("\"", "");
temp[counter] = correctStr;
counter++;
}

// write X times to file
for (int x = 0; x < setCount; x++)
{
for (int i = 0; i < counter; i++)
{
sw.WriteLine(temp[i]);
}

if (counter < 9)
{
for (int i = counter; i < 9; i++)
{
sw.WriteLine();
}
}
}

if (counter < 9) break; // can't be any more lines left in file

Array.Clear(temp, 0, 9); // clear array for next batch of 9
counter = 0; // clear counter
}


See More: Read and Write text file using batch script

Report •


#1
February 20, 2012 at 06:17:32
You're probably not getting many hits because the cross-section of people who know CMD, C#, and visit the site is very small.

That said, here's the breakdown for anyone looking to punish themselves:
Step 1) Read 9 lines of file, strip double quotes
Step 2) Write out those 9 lines, pad with blank lines at end if you didn't read 9 lines
Step 3) Repeat step 2 setCount times. (Variable is neither defined nor initialized, but I could say that about every variable here.)
Step 4) Go to step 1.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question