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 

Editing lines in a text file without opening it

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






PostPosted: Fri Feb 12, 2010 5:19 am    Post subject: Editing lines in a text file without opening it Reply with quote

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






PostPosted: Fri Feb 12, 2010 5:26 am    Post subject: Reply with quote

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





PostPosted: Fri Feb 12, 2010 5:31 am    Post subject: Reply with quote

... 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

PostPosted: Fri Feb 12, 2010 5:32 am    Post subject: Reply with quote

That's not so hard Very Happy
Code:
Loop 4 ; the number of lines in your file
{
FileReadLine FileText, Files.txt, %A_Index%
FileAppend , "%FileText%"`n, Files2.txt
}
Back to top
View user's profile Send private message
Leef_me



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Fri Feb 12, 2010 5:37 am    Post subject: Reply with quote

Code:
infile=inf.txt
outfile=outf.txt

Loop, read, %infile%, %outfile%
    FileAppend, "%A_LoopReadLine%"`n
Back to top
View user's profile Send private message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Fri Feb 12, 2010 11:41 am    Post subject: Reply with quote

A one liner, does require TF Lib http://www.autohotkey.com/forum/viewtopic.php?t=46195
Code:
TF_RegExReplace("file.txt", "im)^(.*)$","""$1""") ; will generate file_copy.txt change "file.txt" to "!file.txt" to write changes back to source file
similar to the example in the documentation which wraps lines in [] http://www.autohotkey.net/~hugov/tf-lib.htm#TF_RegExReplace
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Sat Feb 13, 2010 3:14 am    Post subject: Reply with quote

@ 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 Sad

@ 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. Sad
_________________
AHK is perfect.
Back to top
View user's profile Send private message
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Sat Feb 13, 2010 3:32 am    Post subject: Reply with quote

Ok I'll add some code to count lines Smile
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
View user's profile Send private message
ton80



Joined: 18 Dec 2009
Posts: 62

PostPosted: Sat Feb 13, 2010 4:15 am    Post subject: Reply with quote

Code:
Loop, Read, %FilePath%
{
   NewLine = "%A_LoopReadLine%"
   FileAppend, %NewLine% `n, Output.txt
}
FileMove, Output.txt, %FilePath%,1
Back to top
View user's profile Send private message
OceanMachine



Joined: 15 Oct 2007
Posts: 780
Location: England

PostPosted: Sat Feb 13, 2010 4:23 am    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Sat Feb 13, 2010 6:59 am    Post subject: Reply with quote

@ 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... Sad

@ 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 Razz 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 Smile
_________________
AHK is perfect.
Back to top
View user's profile Send private message
None



Joined: 28 Nov 2009
Posts: 3086

PostPosted: Sat Feb 13, 2010 7:51 am    Post subject: Reply with quote

Quote:
Didn't work at all! When I executed the script, it was like it ignored the file.

Confused 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
View user's profile Send private message
ton80



Joined: 18 Dec 2009
Posts: 62

PostPosted: Sat Feb 13, 2010 1:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
answer4u
Guest





PostPosted: Sat Feb 13, 2010 2:12 pm    Post subject: Reply with quote

ton80 wrote:
Works like a charm here..

Same here. So does Leef_me's and mine Wink .
Back to top
Da Rossa



Joined: 06 Dec 2007
Posts: 361

PostPosted: Sun Feb 14, 2010 12:14 am    Post subject: Reply with quote

But its ok gentlemen. Thanks for all your help in this. Smile I really appreciate!!
_________________
AHK is perfect.
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