## Copy from all files in a folder lines specified by line number to new file

Help with writing and running scripts

### Copy from all files in a folder lines specified by line number to new file

Apologies to all but I am a very new user to this forum and have been tasked with something that I am starting to get to grips with.

I have a large number of data text files stored in a folder and need to copy co-ordinate information from specific line numbers from each of the files and would like to store this information in a spreadsheet.

Is it possible to visit each of the text files one after another and store the information on separate lines in a spreadsheet?

Any assistance about how to go about stating this would be appreciated

Thanks
sscpord
Newbie

Posts: 3
Joined: Thu Mar 17, 2011 8:44 am

### Re: Copy from all files in a folder lines specified by line number to new file

Is there something common on the lines of interest? In this case you could simply execute a (regular expression) Find in Files with results to an edit window to get all the lines you want in a file.

If there is nothing common and you really have to copy 5th, 18th, 34th, ... line from all the text files into a new file, you would really need a script. You need to include the function GetListOfFiles into your script, and you have to adapt the directory path, the file specification in the first line and the line numbers in the array.

Code: Select all
// Execute function GetListOfFiles() to get a list of file names// into an ASCII file which is the active file on success.if (GetListOfFiles(0,"C:\\Temp\\","*.txt")) {   // Define the line numbers to copy lines from all the files.   var anLineNumbers = new Array(5,18,34);   // Select the entire file content of active file which contains all the   // file names matching the file specification in the specified directory.   UltraEdit.activeDocument.selectAll();   // Load this list into a string variable.   var sFileList = UltraEdit.activeDocument.selection;   // Close the list file without saving it.   UltraEdit.closeFile(UltraEdit.activeDocument.path,2);   // Split the long string with the file names up into an   // array of strings with each string containing a file name.   var asFileNames = sFileList.split("\r\n");   // Remove the last string from the array which is usually an empty string   // caused by the line termination after last file name in the list file.   if (!asFileNames[asFileNames.length-1].length) asFileNames.pop();   // Define an empty string to collect the lines to copy.   var sLinesToCopy = "";   // Sometimes like for Unicode encoded files or when different types   // of line terminators are used in the opened files or in target   // file, using a clipboard is better than a string variable. But   // using a string variable is faster than using a clipboard.   // UltraEdit.selectClipboard(9);   // UltraEdit.clearClipboard();   // Run the following loop for all file names in the array of strings.   for (var nFileNum = 0; nFileNum < asFileNames.length; nFileNum++) {      // Open the file with the name stored in the array of strings. It      // is important here that none of the files in the list is already      // opened in UltraEdit because then this command wouldn't do anything      // resulting it applying the next steps perhaps on the wrong file.      // See http://www.ultraedit.com/forums/viewtopic.php?f=52&t=4596      // how to verify if a file is already open before opening a file.      UltraEdit.open(asFileNames[nFileNum]);      // This loop appends the lines of interest to the string variable      // or the clipboard. No check is done if the file contains really      // all those lines because that would make the script slower.      for(var nLineNum = 0; nLineNum < anLineNumbers.length; nLineNum++) {         UltraEdit.activeDocument.gotoLine(anLineNumbers[nLineNum],1);         // if (UltraEdit.activeDocument.currentLineNum != anLineNumbers[nLineNum]) break;         UltraEdit.activeDocument.selectLine();         sLinesToCopy += UltraEdit.activeDocument.selection;         // UltraEdit.activeDocument.copyAppend();      }      // Close the file opened just for getting the lines of interest.      UltraEdit.closeFile(UltraEdit.activeDocument.path,2);   }   // Open a new file and paste or write the collected lines into this file.   UltraEdit.newFile();   UltraEdit.activeDocument.write(sLinesToCopy);   // UltraEdit.activeDocument.paste();   // UltraEdit.clearClipboard();   // UltraEdit.selectClipboard(0);   // Move the caret to top for evaluating the result.   UltraEdit.activeDocument.top();}

Mofi
Grand Master

Posts: 4198
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

### Re: Copy from all files in a folder lines specified by line number to new file

Hi Mofi

I really have to say a big thanks for the work and the time that you have given to help me with this.

The GetListOfFiles works a treat on it's own but I am getting an error in Line 1 of the Select All code that you sent me.

I have set up a folder on my desktop with the path C:\Documents and Settings\dbigwood\Desktop\QQ wiith a couple of .dxf files in it
I have adjusted the line numbers for the array to 1950,1952,1954,1954 and 2808

Here are the first couple of lines in the code

if (GetListOfFiles(0,"C:\Documents and Settings\dbigwood\Desktop\QQ","*.dxf")) {

var anLineNumbers = new Array(1950,1952,1954,1956,2808);

I have tried various options in the first line but still get the error coming up, do you have any suggestions?

Derek
sscpord
Newbie

Posts: 3
Joined: Thu Mar 17, 2011 8:44 am

### Re: Copy from all files in a folder lines specified by line number to new file

The backslash character is an escape character in Javascript strings. Therefore you need the line:

if (GetListOfFiles(0,"C:\\Documents and Settings\\dbigwood\\Desktop\\QQ\\","*.dxf")) {

Mofi
Grand Master

Posts: 4198
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

### Re: Copy from all files in a folder lines specified by line number to new file

Hi Mofi

Sorry to have been a while in saying thanks for the enlightenment but have been away since you posted the reply.

I have done as you have stated but am still getting an error in the first line that is preventing the script from running.

The first two lines of the code now look like this

if (GetListOfFiles(0,"C:\\Documents and Settings\\dbigwood\\Desktop\\QQ\\","*.dxf")) {

var anLineNumbers = new Array(1950,1952,1954,1956,2808);

Is there a possibility of something missing in the first line as the GetListOfFiles has 4 inputs ?

any help would be appreciated

Derek
sscpord
Newbie

Posts: 3
Joined: Thu Mar 17, 2011 8:44 am

### Re: Copy from all files in a folder lines specified by line number to new file

Have you included the code of function GetListOfFiles at top of the script. Without copying the code of this function into my script you get an error message because of unknown symbol GetListOfFiles. Important is that you copy just the function code, without all the additional code below the function for demonstrating the usage of this function. You have to copy only from line

function GetListOfFiles (nFileList, sDirectory, sFileType, bSubDirs) {

to line

} // End of function GetListOfFiles

Another, perhaps better possibility to include the function is to make a copy of the downloaded script file (for example as GetListOfFilesFunc.js) , open the copy, remove the code for demonstrating usage and save the script file now containing only the function itself. (The comment lines could be also removed to make script execution a little bit faster.) The function can now be included into the main script file with the line:

// include C:\full path\to external\script\GetListOfFilesFunc.js

The 4th parameter bSubDirs of GetListOfFiles is optional. Javascript supports optional parameters. It is used with value false if not used on call. Of course you can add ,false to call of GetListOfFiles.

Is your script an ASCII/ANSI DOS file as indicated with just DOS in the status bar at bottom of the UltraEdit window with the opened script file active? If you see for example U-DOS or U8-DOS, your script file is encoded with UTF-8 or UTF-16 which are both not supported by the script interpreter.

BTW: What is the error displayed in the output window? You can copy content of output window via context menu (right click into output window).

Mofi
Grand Master

Posts: 4198
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna