AutoHotkey Community

It is currently May 27th, 2012, 2:32 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: February 12th, 2010, 6:19 am 
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..


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2010, 6:26 am 
You can use fileread with a loop,

Code:
FileRead, x, %filepath%
FileDelete, %filepath%

Loop, Parse, x, `n
{
   newline = "%A_loopfield%"   
   fileappend, %newline%, %filepath%
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2010, 6:31 am 
... same concept as above ...
Code:
Loop, Read, file.txt
   data .= """" A_LoopReadLine """`n"
FileDelete, file.txt
FileAppend, %data%, file.txt


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2010, 6:32 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
That's not so hard :D
Code:
Loop 4 ; the number of lines in your file
{
FileReadLine FileText, Files.txt, %A_Index%
FileAppend , "%FileText%"`n, Files2.txt
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2010, 6:37 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6071
Location: San Diego, California
Code:
infile=inf.txt
outfile=outf.txt

Loop, read, %infile%, %outfile%
    FileAppend, "%A_LoopReadLine%"`n


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 12th, 2010, 12:41 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 ... gExReplace

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 4:14 am 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
@ 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 4:32 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 5:15 am 
Offline

Joined: December 18th, 2009, 6:08 pm
Posts: 63
Code:
Loop, Read, %FilePath%
{
   NewLine = "%A_LoopReadLine%"
   FileAppend, %NewLine% `n, Output.txt
}
FileMove, Output.txt, %FilePath%,1


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 5:23 am 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
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 :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 7:59 am 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
@ 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 :P 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 8:51 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 2:48 pm 
Offline

Joined: December 18th, 2009, 6:08 pm
Posts: 63
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2010, 3:12 pm 
ton80 wrote:
Works like a charm here..

Same here. So does Leef_me's and mine :wink: .


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2010, 1:14 am 
Offline

Joined: December 6th, 2007, 12:48 pm
Posts: 364
But its ok gentlemen. Thanks for all your help in this. :) I really appreciate!!

_________________
AHK is perfect.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Alpha Bravo, Bing [Bot], LazyMan, rbrtryn, Yahoo [Bot] and 19 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