EXPERIMENT: rename files by reading lines from a text file using
#1 : 11/07-25 01:57 Delta Foxtrot
Posts: 521
|
Hello all,
Full title: EXPERIMENT: rename files by reading lines from a text file using <File Content> Here's a little experiment I found interesting: Read a text file character by character using <File Content>, assemble the each line as an array entry, then replace the filenames in the ARen files list with the array entries. (If you are not into scripting you may wish to stop reading here). (Bye bye, everybody!) Anybody who has used ARen's scripting capabilities for much time has discovered that you can use <File Line> to read in sequential lines of a file into an array or other object, then use that object to rename files/folders. So this script is just an experiment to see how hard it would be to instead use <File Content> and determine the lines' lengths and content in a more granular way - reading one character at a time. I can't really think of anything you could do with this that you couldn't with <File Line>, and more easily, but the fact that something is pointless has never stopped me from striding boldly into the breach! :) EDIT: I realized that some of you may not know about the <File Line> tag being able to read an external file, since it's not documented in the User Guide (it was sort of documented in the "What's New" section back in version 3.61 - https://www.advancedrenamer.com/whatsnew_v3 - all it says is that <File Line> works like <File Content>, but that's enough). The format is <File Line:[line#]::[FileToRead]> If you don't specify a [FileToRead] (must be in quotes, so in a script it has to be in the format '"c:\path1\path2\filename.ext"' So if you are reading successive lines using a counter var j, the form would be app.parseTags( '<File Line:'+k+'::'+'"c:\path1\path2\filename.ext"'+'>' ) END EDIT BTW, this script doesn't care what's in the file lines, or the extension, or anything except that it's got line breaks that conform to Windows CR/LF construction, but it probably wouldn't be hard to change to read, for example, a csv file. So here it is, copiously commented. Enjoy(?) // ------------------------------- PRE BATCH ------------------------------- // File to test [REPLACE WITH THE PATH/NAME OF YOUR TEXT FILE]: // ( for some reason it looks like ARen no longer cares if you use single // or double backslashes as directory separators... maybe the v4 js engine? // My file was this: fromFile = '"J:\PhotographerCodes.tsv"' ; it makes no sense // but that worked fine. fromFile = '"C:\\test\myTextFile.txt"' ; //fromFile = '\"'+fromFile+'\"' ; // Counter to keep track of position in file: charPos = 0; // Position of start of current line: curLineStart = 0; // Counter for number of lines: lineCount = 0; // Counter for total characters in each line: lineLength = 0; // Number of characters in Operating System new line: OS_newLineLen = 2; // Current character (as string): curChar = "" ; // Current character (as code): curCode = 0; // array to hold lines: const lineArr = [] ; // Loop to read each character, check for new line, save the line, update counters: for ( charPos = 0; lineCount < app.itemCount; charPos++ ) { // while lineCount of file is less than the number of files (limits size of lineArr ), // grab the next character in file: curChar = app.parseTags( '<File Content:'+charPos+':1:'+fromFile+'>' ) ; // get its code: curCodeStr = curChar.charCodeAt(0).toString(); // if it's a vertical-space character (right now just carriage return) // update counters and capture line to array: if ( curCodeStr == "13" ) { // carriage return + line-feed in Windows // the following could surely be done more efficiently, but oh well... // it just updates lineLen lineLen = charPos + ((OS_newLineLen - 1) - curLineStart) ; // get line using <File Content> tag: lineArr[lineCount] = app.parseTags( '<File Content:'+curLineStart+':'+ lineLen+':'+fromFile+'>' ) ; // update everything else: charPos += (OS_newLineLen - 1) ; curLineStart += lineLen ; lineCount++ ; lineLen = 0 ; curCode = "" ; } } // if you look at the JS Console you'll see the array of lines: test = [] ; for ( k = 0; k < lineArr.length; k++ ) { // the 'replace' in the next line removes any line feeds so each entry is one line: test[k] = ( k.toString() + " - " + lineArr[k] ).replace(/(\r\n|\n|\r)/gm, "") ; app.log( test[k] ) ; } // ------------------------------- MAIN ------------------------------- // SEE PRE BATCH FIRST LINE TO NAME THE TEXT FILE TO READ ndx = index ; // first file is 0 // if a specific line was needed, depending on the current name, date, etc, // logic could be added here // return the new name (currently just the array element corresponding // to the index number) return lineArr[ ndx ] ; // ------------------------------- END ------------------------------- Best, DF |
#2 : 12/07-25 13:38 Chris
Posts: 38
|
Reply to #1:
Thanks. In case it helps anyone else, <FileContent> is documented here https://www.advancedrenamer.com/user_guide/v4/tags_advanced . Also, important to note that <FileContent> results are inaccurate on files containing <>:"/\|?* . |