Last active
April 17, 2016 09:11
-
-
Save hi5/4dbd52c304c4c24f183ff26d185df23f to your computer and use it in GitHub Desktop.
AutoHotkey + Total Commander: Merge comments in descript.ion with text files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Script: MergeComments.ahk | |
Purpose: Merge comments in descript.ion with text files? | |
Forum: http://www.ghisler.ch/board/viewtopic.php?t=43811 | |
Gist: https://gist.github.com/hi5/4dbd52c304c4c24f183ff26d185df23f | |
TC Button bar, add a new button: | |
Command: pathto\MergeComments.ahk | |
Parameters: %L | |
Usage: | |
- Either select files (can be current folder, branch view and search results (after feed to listbox)) | |
and press the button | |
- To process all files in the CURRENT and SUB folders place the cursor (highlighted file) on the .. | |
at the very top of the active panel, press the button. Confirm you want to process all files or cancel the script. | |
Log will be created in the folder the script resides in. | |
You can define the default extensions in the "ext" variable below, use a comma separated list | |
*/ | |
#NoEnv | |
SetBatchLines, -1 | |
SetWorkingDir, %A_ScriptDir% | |
ext:="txt,bat,cmd" | |
If !%0% | |
{ | |
MsgBox, 20, Error, No files selected.`nDo you want to process all files and folders in the current panel?`n(No to cancel script) | |
IfMsgBox, No | |
ExitApp | |
ClipboardSave:=ClipboardAll | |
Clipboard:="" | |
SendMessage 1075, 2029, 0, , ahk_class TTOTAL_CMD ; Copy source path to clipboard | |
Sleep 100 | |
Path:=Clipboard | |
Clipboard:="" | |
Clipboard:=ClipboardSave | |
ClipboardSave:="" | |
Loop, %Path%\*.*, 1, 1 | |
Files .= A_LoopFileFullPath "`n" | |
Files:=RTrim(Files,"`n") | |
} | |
Else | |
FileRead, Files, %1% ; Read list of selected files from Total Commander | |
q=" | |
ProcessedFiles:=0 | |
SkippedFilesList:="" | |
ErrorFilesList:="" | |
SuccessFilesList:="" | |
NewFiles:="" | |
SelectedNewFiles:=0 | |
StringReplace, Files, Files, `n, `n, UseErrorLevel | |
SelectedFiles:=ErrorLevel | |
Gui, Add, Text, x5 y5, Extensions (csv): | |
Gui, Add, Edit, xp+85 yp-3 w200 vExt, %ext% | |
Gui, Add, Text, x5 yP+30, Additional info: | |
Gui, Add, Edit, xp+85 yp-3 w200 h25 R1 -Multi vClip, %clipboard% | |
Gui, Add, Checkbox, xp yp+30 vAddInfo, Use additional info | |
Gui, Add, Checkbox, xp yp+25 vCreateLog, Generate log file? | |
Gui, Add, Button, xp yp+25 w50 gStart, Start | |
Gui, Add, Button, xp+60 yp w50 gCancel, Cancel | |
Gui, Show, , Merge Comments (Descript.ion) | |
Return | |
Cancel: | |
ExitApp | |
Return | |
Start: | |
Gui, Submit, Destroy | |
If (AddInfo = 0) | |
Clip:="" | |
Progress, b1 w300 CWFFFFFF CB000080, , Finding files, Merging comments: | |
Progress, 5 ; Set to 5% to start | |
ProcessFiles: | |
Loop, parse, Files, `n, `r | |
{ | |
If InStr(FileExist(A_LoopField),"D") ; skip directories | |
{ | |
SkippedFilesList .= "- " A_LoopField " (folder)`n" | |
continue | |
} | |
SplitPath, A_LoopField, FileName, Folder, FileExtension | |
If FileExtension not in %ext% | |
{ | |
SkippedFilesList .= "- " A_LoopField "`n" | |
continue | |
} | |
Folder .= "\" | |
FileRead, d, %Folder%descript.ion | |
PBar:=Floor((A_Index/SelectedFiles)*90) | |
Progress, %PBar% | |
; trying to find the FileName in the descript.ion (simply parse entire list, not very efficient) | |
Loop, parse, d, `n, `r | |
{ | |
If (A_LoopField = "") | |
continue | |
line:=A_LoopField | |
If (SubStr(line,1,1) = q) | |
{ | |
CurrentFileName:=Trim(SubStr(line,1,InStr(line,q,,2)),q) | |
Comment:=Trim(SubStr(line,InStr(line,q,,2)),q " ") | |
} | |
else | |
{ | |
CurrentFileName:=Trim(SubStr(line,1,InStr(line," "))) | |
Comment:=SubStr(line,InStr(line," ")+1) | |
} | |
; MsgBox % Folder FileName "`n" Folder CurrentFileName "`n" comment ; debug only | |
If (FileName <> CurrentFileName) | |
Continue | |
; 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%Clip%%Comment%, %Folder%%FileName% ; `n = new line so `n`n is two new lines | |
If (ErrorLevel = 1) | |
ErrorFilesList .= "- " Folder FileName "`n" | |
Else | |
SuccessFilesList .= "- " Folder FileName "`n" | |
ProcessedFiles++ | |
Break ; we already found it no need to look further | |
} | |
Progress, 100 | |
} | |
Progress, Off | |
If (CreateLog = 0) | |
MsgBox, 64, Done, % ProcessedFiles " file(s) processed out of " SelectedFiles " file(s) selected." | |
If (CreateLog = 1) | |
{ | |
Gosub, SaveLog | |
MsgBox, 68, Done, % ProcessedFiles " file(s) processed out of " SelectedFiles " file(s) selected`nDo you want to open log file?" | |
IfMsgBox, Yes | |
Run %LogFile% | |
} | |
ExitApp | |
SaveLog: | |
SuccessFilesList:=RTrim(SuccessFilesList,"- `n") | |
SkippedFilesList:=RTrim(SkippedFilesList,"- `n") | |
ErrorFilesList :=RTrim(ErrorFilesList ,"- `n") | |
If (SuccessFilesList = "") | |
SuccessFilesList:="(None)" | |
If (SkippedFilesList = "") | |
SkippedFilesList:="(None)" | |
If (ErrorFilesList = "") | |
ErrorFilesList:="(None)" | |
LogFile=MergeComments-log-%A_Now%.txt | |
FileAppend, | |
( | |
Merge Comments log (%A_Now%) | |
------------------------------------- | |
Selected Files: %SelectedFiles% | |
Processed Files: %ProcessedFiles% | |
------------------------------------- | |
OK: | |
%SuccessFilesList% | |
------------------------------------- | |
Skipped files (extensions or folders): | |
%SkippedFilesList% | |
------------------------------------- | |
Errors: | |
%ErrorFilesList% | |
------------------------------------- | |
), %LogFile% | |
Return | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment