AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

manipulate .txt files

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
skyd1v3r



Joined: 18 Jun 2009
Posts: 73

PostPosted: Thu Jun 18, 2009 10:26 pm    Post subject: manipulate .txt files Reply with quote

Hello everyone,

sorry for starting off with a request instead of something useful.
I am in a heavy trouble. Due to a reinstallation of XP, my ever-since-working dos-batch-files faile, so I need to redo some stuff with AHK.

I need a script for the following:

Code:

FOR every .txt-file in the folder
   REPLACE every string "aaa" in the file with "bbb" and
   WRITE the new version of the file into the folder X:\somewhere\
   COMMENT: Original file shall not be changed
END LOOP


I am sure for someone skillfull itīs a matter of seconds to post nsome syntax. I am really appreciating any help.

Thanks

skyd1v3r

PS: If youīd like to question my choice of using AHK - youīre welcome to do so, once you posted the AHK-script above Smile
Back to top
View user's profile Send private message
ribbs2521



Joined: 28 Sep 2007
Posts: 273
Location: New York

PostPosted: Thu Jun 18, 2009 10:57 pm    Post subject: Reply with quote

This should work, I wrote it quickly and it hasn't been tested.

Code:
Loop, FolderPath\*.txt, 0, 1   ;<--The 0 means it only looks at files, change 1 to 0 if you don't want to look in sub folders
{
    tempContents := ""       ;Clear the temp variable for each new file
   Loop, Read, %A_LoopFileLongPath%     ;Read each line in the file
      tempContents .=        ;Append each line to the variable
   StringReplace, tempContents, tempContents, aaa, bbb, All   ;Replace each aaa with bbb in this variable
   FileAppend, %tempContents%, Destination Path\%A_LoopFileName%    ;Write the data to a new file
}
Back to top
View user's profile Send private message
skyd1v3r



Joined: 18 Jun 2009
Posts: 73

PostPosted: Thu Jun 18, 2009 11:24 pm    Post subject: Reply with quote

Hello ribs2521,

thank you for your rapid help!
The files are created in the desired folder with correct file names.
How ever, they are empty.
So either the variable is not created, or not written to the file.
This line seems stange to me, since it has nothing behind the .= -operator.
Code:
tempContents .=       

Also I am not sure if itīs tempContents, aaa, bbb, All
or tempContents, "aaa", "bbb", All, but I suggested itīs without as you wrote.


Any ideas?
Thanks again

skyd1v3r
Back to top
View user's profile Send private message
d-man



Joined: 08 Jun 2006
Posts: 285

PostPosted: Thu Jun 18, 2009 11:31 pm    Post subject: Reply with quote

what you need is "A_LoopReadLine" so make it:

tempContents := A_LoopReadLine
Back to top
View user's profile Send private message
ribbs2521



Joined: 28 Sep 2007
Posts: 273
Location: New York

PostPosted: Thu Jun 18, 2009 11:34 pm    Post subject: Reply with quote

d-man wrote:
what you need is "A_LoopReadLine" so make it:

tempContents := A_LoopReadLine


What he said, I don't know why I left that out lol

Actually it should be tempContents .= A_LoopReadLine

Note:
:= sets the variable once, if you do it again it will overwrite (this is like using = but for variables you don't need %variable% and for text you need quotes where using the = the percents are needed and quotations are not)
.= adds to the variable leaving, in tact, whatever is already on it.
Back to top
View user's profile Send private message
skyd1v3r



Joined: 18 Jun 2009
Posts: 73

PostPosted: Fri Jun 19, 2009 12:24 am    Post subject: Reply with quote

Got it working,

thanks a lot.

Amazing what can be done with a hotkey tool. Smile
Next objective: run in background and execute loop whenever a file has changed. guess I gonna read the date-of-change into a variable.

skyd1v3r

EDIT:
If anyone needs this script, please consider to use
Code:

   Loop, Read, %A_LoopFileLongPath%     ;Read each line in the file
      {
   tempContents .= A_LoopReadLine       ;Append each line to the variable
      tempContents .= "`r`n" ; New line after each line, otherwise all text wil be in one single line
   } 
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group