AutoHotkey Community

It is currently May 26th, 2012, 6:47 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: manipulate .txt files
PostPosted: June 18th, 2009, 11:26 pm 
Offline

Joined: June 18th, 2009, 11:13 pm
Posts: 73
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 :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 18th, 2009, 11:57 pm 
Offline

Joined: September 28th, 2007, 3:56 am
Posts: 279
Location: New York
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 12:24 am 
Offline

Joined: June 18th, 2009, 11:13 pm
Posts: 73
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 12:31 am 
Offline

Joined: June 8th, 2006, 2:41 am
Posts: 285
what you need is "A_LoopReadLine" so make it:

tempContents := A_LoopReadLine


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 12:34 am 
Offline

Joined: September 28th, 2007, 3:56 am
Posts: 279
Location: New York
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 1:24 am 
Offline

Joined: June 18th, 2009, 11:13 pm
Posts: 73
Got it working,

thanks a lot.

Amazing what can be done with a hotkey tool. :)
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
   } 


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: dra, Google [Bot], rbrtryn and 65 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group