How to merge comments in descript.ion with text files?

English support forum

Moderators: Hacker, petermad, Stefan2, white

sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

How to merge comments in descript.ion with text files?

Post by *sorcar »

Is there a way to merge text in comment filelds (of descript.ion) to their corresponding text files? Say, I have three files:

Code: Select all

Files		COMMENTS
A.bat		REM Code source www.site1.com
B.bat		REM Code source www.site2.com
C.bat		REM Code source www.site3.com
How can I append the comment texts to the .bat files?

Thanks in advance.
User avatar
Vochomurka
Power Member
Power Member
Posts: 816
Joined: 2005-09-14, 22:19 UTC
Location: Russia
Contact:

Post by *Vochomurka »

There are at least two ways:
1) Shell command "copy" with plus sign (+). Please refer to any command reference (example);
2) cm_Combine command of TC (files need to be renamed first)
Single user license #329241
PowerPro scripts for Total Commander
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Script: parse file, find related file, add to content

Post by *Stefan2 »

 

You mean something like:


- for each line in descript.ion
---- get the file name to variable $file
---- get the comment to variable $comm
---- find and open the $file
---- add $comm to $file content and save it

?



If yes, then: where to add that comment line? At the very end of each batch file?

?





 
User avatar
Vochomurka
Power Member
Power Member
Posts: 816
Joined: 2005-09-14, 22:19 UTC
Location: Russia
Contact:

Post by *Vochomurka »

2sorcar
Ah, now I see I misunderstood the question (like Stefan2). Can you ask it in other words?
Single user license #329241
PowerPro scripts for Total Commander
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

2 Vochomurka:

Let me try to explain a little more. I have thousands of text files with different extensions (.bat, .cmd, .txt, .vbs ...) in various directories. Most files have information field as comments (descript.ion). I want to merge (or append) these comments at the bottom of the text files. I wanted to keep the explanation brief, but let me know if it is not clear yet.

A plugin through Change Attributes/Plugin: Property: Value would have been ideal to append the comments to their corresponding text files. But a DOS script, possibly with a FOR loop, invoked through a button could "type" the comment snippets to the text files would also be equally fine. (But how can it work if text files are many in a directory and descript.ion file is only one?)

I will not dare suggest a mechanism, as I have found over the years in this forum wonderful solutions which I did not find through hours of google search, but often found that someone came up with an elegant solution!
Last edited by sorcar on 2016-04-01, 15:53 UTC, edited 1 time in total.
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

2 Stephan2
You mean something like:

- for each line in descript.ion
---- get the file name to variable $file
---- get the comment to variable $comm
---- find and open the $file
---- add $comm to $file content and save it
This can pretty much lead to a solution, I guess, though finding and opening each $file could be tedious (I have thousands of such files).
If yes, then: where to add that comment line? At the very end of each batch file?

Yes, at the very end of each .bat (or other text) files.
hi5
Power Member
Power Member
Posts: 642
Joined: 2012-11-03, 11:35 UTC
Contact:

Post by *hi5 »

@sorcar: are you familiar with AutoHotkey? If not and you are willing to install it it seems a relatively trivial task using AHK. Create a script and place it in the folder with your files. Be sure to make a backup of your data before running the script

Code: Select all


FileRead, d, descript.ion
q="

Loop, parse, d, `n, `r
	{
	 if (A_LoopField = "")
		continue
	 line:=A_LoopField
	 If (SubStr(line,1,1) = q) 
		{
		 filename:=Trim(SubStr(line,1,InStr(line,q,,2)),q)
		 comment:=Trim(SubStr(line,InStr(line,q,,2)),q " ")
		} 
	 else
		{
		 filename:=SubStr(line,1,InStr(line," "))
		 comment:=SubStr(line,InStr(line," ")+1)
		}
	 ;MsgBox % filename "`n" comment
	 FileAppend, `n%comment%, %filename%
	}

sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Thanks for coming to the rescue. AHK solution should be quite fine. While running the script, I got the following dialog box message:
---------------------------
test.ahk
---------------------------
Error at line 1.

Line Text: ileRead, d, descript.ion
Error: This line does not contain a recognized action.

The program will exit.
---------------------------
OK
---------------------------
What do I do now?
hi5
Power Member
Power Member
Posts: 642
Joined: 2012-11-03, 11:35 UTC
Contact:

Post by *hi5 »

The first line should be "FileRead" not "ileRead", you probably didn't copy the letter F from the script code posted above. Try adding it and see what happens :-)
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Oops! corrected. Now It works like a charm. It did append the comments to text files with different extensions, exactly as I asked for.

Now please suggest amendment for: (a) how can the script work recursively through subdirectories, (b) How can I add some optional text/linebreaks between the text file and the appended comment? (c) If there is a linebreak in the comment, the appended text replaces it with "\n". Is there a way to fix it?
hi5
Power Member
Power Member
Posts: 642
Joined: 2012-11-03, 11:35 UTC
Contact:

Post by *hi5 »

Put this script in the parent folder of the folders you want to do this on and it should parse through all files adding the info from description.

You see the `n in the FileAppend command? `n is a new line so if you want more new lines just add another `n.

You can use StringReplace to remove \n with a space or make it a new REM line - both are below the ; at the start of a line in AHK means comment so you can use which one you like but removing / adding ;

Code: Select all


/*

- folder where script resides
  |_subfolder
  |_subfolder
  |_subfolder
  |_subfolder
  
*/

q="

Loop, Files, *, D
	{
	 folder:=A_LoopFileLongPath "\"
	 FileRead, d, %folder%descript.ion

	 Loop, parse, d, `n, `r
		{
		 if (A_LoopField = "")
			continue
		 line:=A_LoopField
		 If (SubStr(line,1,1) = q) 
			{
			 filename:=folder Trim(SubStr(line,1,InStr(line,q,,2)),q)
			 comment:=Trim(SubStr(line,InStr(line,q,,2)),q " ")
			} 
		 else
			{
			 filename:=folder SubStr(line,1,InStr(line," "))
			 comment:=SubStr(line,InStr(line," ")+1)
			}
		 ; MsgBox % filename "`n" comment
		 ; StringReplace, comment, comment, \n, %A_Space%, All ; remove all \n replace with space
		 StringReplace, comment, comment, \n, `nREM%A_Space%, All ; make each \n its own REM line
		 FileAppend, `n%comment%, %filename% ; `n = new line so `n`n is two new lines
		}
	}

F4MiniMenu (Forum) - Open selected file(s) from TC in defined editor(s) - A (minimalistic) clone of F4Menu
Source at GitHub (AutoHotkey). TCSyncComments (copy file comments)
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Works one level deep, but not further below! (Not in current directory either).

StringReplace explanation is clear and both the options are working fine.
hi5
Power Member
Power Member
Posts: 642
Joined: 2012-11-03, 11:35 UTC
Contact:

Post by *hi5 »

You can add the letter R to the Loop, Files command - see https://autohotkey.com/docs/commands/LoopFile.htm

So it becomes

Code: Select all

Loop, Files, *, DR
You still have the first script posted above so you can simply run that in the current folder by adding

Code: Select all

Run, test.ahk
either at the top or very bottom of the Loop version of the script. that way it processes the files in the current folder as well.
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

Everything works perfectly. Thank you, hi5, for the script. It ends almost a year of my wish and wait.
sorcar
Member
Member
Posts: 100
Joined: 2005-04-12, 17:45 UTC
Location: U.S.

Post by *sorcar »

This script is of great help in me. But it works on on files in the default directory, or on all subdirectories.

How to make it work in TC on selected files only? Better still, it will be more helpful if the script resides in a fixed folder, can be invoked through a button bar to work on selected files.

Anyone can help wit this ahk script, or through another approach?

Thanks in advance.
Post Reply