 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Feb 12, 2010 5:19 am Post subject: Editing lines in a text file without opening it |
|
|
Say that I have a txt file with a list of file paths like:
| Code: | C:\WINDOWS
D:\My Pictures\me.jpg
E:\Photos from 1999\New York\Central Park\me again.jpg
E:\Art\Picasso |
This text file with the above list is stored in a constant location in my computer.
I'd like to do something to, without opening and editing manually the file, put quotation marks in each line. The output would be:
| Code: |
"C:\WINDOWS"
"D:\My Pictures\me.jpg"
"E:\Photos from 1999\New York\Central Park\me again.jpg"
"E:\Art\Picasso" |
Maybe the FileRead command, but I wouldn't know what to do next. Also RegEx is my nightmare, nightmare which I'd really want to kill, but the only reference I have (regular-expressions.info) is too much for my disadvantaged brain.
Someone please help.. |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Feb 12, 2010 5:26 am Post subject: |
|
|
You can use fileread with a loop,
| Code: |
FileRead, x, %filepath%
FileDelete, %filepath%
Loop, Parse, x, `n
{
newline = "%A_loopfield%"
fileappend, %newline%, %filepath%
} |
|
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Fri Feb 12, 2010 5:31 am Post subject: |
|
|
... same concept as above ...
| Code: | Loop, Read, file.txt
data .= """" A_LoopReadLine """`n"
FileDelete, file.txt
FileAppend, %data%, file.txt |
|
|
| Back to top |
|
 |
None
Joined: 28 Nov 2009 Posts: 3086
|
Posted: Fri Feb 12, 2010 5:32 am Post subject: |
|
|
That's not so hard
| Code: | Loop 4 ; the number of lines in your file
{
FileReadLine FileText, Files.txt, %A_Index%
FileAppend , "%FileText%"`n, Files2.txt
} |
|
|
| Back to top |
|
 |
Leef_me
Joined: 08 Apr 2009 Posts: 5336 Location: San Diego, California
|
Posted: Fri Feb 12, 2010 5:37 am Post subject: |
|
|
| Code: | infile=inf.txt
outfile=outf.txt
Loop, read, %infile%, %outfile%
FileAppend, "%A_LoopReadLine%"`n
|
|
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
Da Rossa
Joined: 06 Dec 2007 Posts: 361
|
Posted: Sat Feb 13, 2010 3:14 am Post subject: |
|
|
@ Guest: your example only put the quote mark at the beginning and at the end of the txt file, not in each line. I replaced %filepath% with "text.txt" which lies on the desktop, along with the ahk script you provided.
@ answer4u: the same as above.
@ None: your example has the limitation that I have to previously know the # of lines of the txt file and edit it in the script every time. I couldn't figure out a method to make this detection automatic and store the output number into a variable, and I suspect that this would bring extra unecessary complication
@ Leef_me. Didn't work. I substituted "infile" to "text.txt" and kept outfile intact.
@ hugoV: bringing in knowledge of another variable or function I don't know yet is not helping me. Also, this use not only RegEx, which is messy to me (shouldn't be, I thought I was a smart guy), but also something beyond regex:
| Quote: | * Purpose: Find and Replace text in entire file (using RegExReplace)
* Parameters: Text, NeedleRegEx, Replacement
* Credits: Heresy / HugoV
TF_RegExReplace("File.txt","im)^(.*)$","[$1]") ; pass on file, wrap all lines in [] |
And sorry for not identifying me at the beginning of the topic.  _________________ AHK is perfect. |
|
| Back to top |
|
 |
None
Joined: 28 Nov 2009 Posts: 3086
|
Posted: Sat Feb 13, 2010 3:32 am Post subject: |
|
|
Ok I'll add some code to count lines
| Code: | FileRead FileText, Files.txt ;read whole file to var
StringReplace, FileText, FileText, `r`n , ,,UseErrorLevel ;count the nunber of enters
Enters=%ErrorLevel% ;store number of enters in a var
Loop %Enters%
{
FileReadLine FileText, Files.txt, %A_Index%
FileAppend , "%FileText%"`n, Files2.txt
} |
|
|
| Back to top |
|
 |
ton80
Joined: 18 Dec 2009 Posts: 62
|
Posted: Sat Feb 13, 2010 4:15 am Post subject: |
|
|
| Code: | Loop, Read, %FilePath%
{
NewLine = "%A_LoopReadLine%"
FileAppend, %NewLine% `n, Output.txt
}
FileMove, Output.txt, %FilePath%,1 |
|
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 780 Location: England
|
Posted: Sat Feb 13, 2010 4:23 am Post subject: |
|
|
| Da Rossa wrote: | | @ Leef_me. Didn't work. I substituted "infile" to "text.txt" and kept outfile intact. |
Not sure what you mean - Leef_me's code should work just fine. Maybe you didn't change the variables correctly?
I have put some comments.
| Code: | infile=c:\inf.txt ; change this "c:\inf.txt" to be the name of your text file that contains all your 'unquoted' locations.
outfile=c:\outf.txt ; change this "c:\outf.txt" to be the file name where you would like the finished 'quoted' results to be saved to. If the file already exists, the results will be added to the end of the file.
; now run all this code, and look for the out file that will have been created where you specified in 'outfile' above.
IfNotExist, %infile%
MsgBox, Infile "%infile%" not found.
Else
{
Loop, read, %infile%, %outfile%
FileAppend, "%A_LoopReadLine%"`n
MsgBox, File %outfile% should contain your results. Opening file now.
Run, %outfile%
} |
Sorry if I made it too simple, but it worked fine for me so I just wanted to explain just in case  |
|
| Back to top |
|
 |
Da Rossa
Joined: 06 Dec 2007 Posts: 361
|
Posted: Sat Feb 13, 2010 6:59 am Post subject: |
|
|
@ None:
| Quote: | | Ok I'll add some code to count lines Smile |
Didn't work at all! When I executed the script, it was like it ignored the file. I left the script and the txt file both in the desktop, and renamed the txt to "Files.txt" just to match with the filename you used. It was like a stab in the wind...
@ ton80
Same as the friends above: it quotes at the beginning and at the end of the text, but not each line...
@ OceanMachine
| Quote: | Not sure what you mean - Leef_me's code should work just fine. Maybe you didn't change the variables correctly?
I have put some comments.
|
Sorry, I might have made a mistake! Sorry for the need for you to put those comments Now it works!
| Quote: | | Sorry if I made it too simple, but it worked fine for me so I just wanted to explain just in case Smile |
I appreciate that. I like the things from A-B-C  _________________ AHK is perfect. |
|
| Back to top |
|
 |
None
Joined: 28 Nov 2009 Posts: 3086
|
Posted: Sat Feb 13, 2010 7:51 am Post subject: |
|
|
| Quote: | | Didn't work at all! When I executed the script, it was like it ignored the file. |
it worked when I tried it the only thing I can think of is to try replacing `r`n with just `n or just `r. If anybody else knows why mine might not work please post I'm Curious. |
|
| Back to top |
|
 |
ton80
Joined: 18 Dec 2009 Posts: 62
|
Posted: Sat Feb 13, 2010 1:48 pm Post subject: |
|
|
| Quote: | @ ton80
Same as the friends above: it quotes at the beginning and at the end of the text, but not each line...
|
Works like a charm here.. I get the feeling you aren't defining the filepath correctly. |
|
| Back to top |
|
 |
answer4u Guest
|
Posted: Sat Feb 13, 2010 2:12 pm Post subject: |
|
|
| ton80 wrote: | | Works like a charm here.. |
Same here. So does Leef_me's and mine . |
|
| Back to top |
|
 |
Da Rossa
Joined: 06 Dec 2007 Posts: 361
|
Posted: Sun Feb 14, 2010 12:14 am Post subject: |
|
|
But its ok gentlemen. Thanks for all your help in this. I really appreciate!! _________________ AHK is perfect. |
|
| 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
|