 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
skyd1v3r
Joined: 18 Jun 2009 Posts: 73
|
Posted: Thu Jun 18, 2009 10:26 pm Post subject: manipulate .txt files |
|
|
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  |
|
| Back to top |
|
 |
ribbs2521
Joined: 28 Sep 2007 Posts: 273 Location: New York
|
Posted: Thu Jun 18, 2009 10:57 pm Post subject: |
|
|
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 |
|
 |
skyd1v3r
Joined: 18 Jun 2009 Posts: 73
|
Posted: Thu Jun 18, 2009 11:24 pm Post subject: |
|
|
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.
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 |
|
 |
d-man
Joined: 08 Jun 2006 Posts: 285
|
Posted: Thu Jun 18, 2009 11:31 pm Post subject: |
|
|
what you need is "A_LoopReadLine" so make it:
tempContents := A_LoopReadLine |
|
| Back to top |
|
 |
ribbs2521
Joined: 28 Sep 2007 Posts: 273 Location: New York
|
Posted: Thu Jun 18, 2009 11:34 pm Post subject: |
|
|
| 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 |
|
 |
skyd1v3r
Joined: 18 Jun 2009 Posts: 73
|
Posted: Fri Jun 19, 2009 12:24 am Post subject: |
|
|
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
}
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|