Jump to content


Photo

Scripting advice


  • Please log in to reply
6 replies to this topic

#1 Zzozzach

Zzozzach

    Éclair

  • Atomican
  • 3,250 posts
  • Gender:Male
  • Location:SE Qld
  • Interests:I love torrenting MAME and Pinball emulators. One day I may even use the software.<br /><br />*****<br /><br />Queensland Fire and Rescue Service auxiliary firefighter:<br />Don't play with matches, only WORKING smoke alarms save lives, if your clothes are on fire then stop, drop, and roll.<br /><br />It's the greatest job in the world. Our best days happen when someone else is having their worst one. It feels good to help.<br /><br />*****<br /><br />My day job is primarily concerned with networking and Internet related technologies.<br /><br />I'm an email expert: if you want to send a lot of email, talk to me. I know people.<br /><br />If you have a question related to sending or receiving email, ask me and I can probably help.

Posted 31 January 2012 - 11:47 PM

I'm hoping to get some advice for building a script here. My script-fu doesn't extend beyond Windows batch files, and I think what I'm trying to do is too complicated for simple DOS commands. I have a text file with 1100 email addresses. I want to parse the file to extract one email address at a time, write that address to a specific spot in a pre-defined block of text, and then save the text to a unique file. The script then needs to loop back and go onto the next address. At the end I want to have 1100 unique text files, each with one address in it. Ideas? Note that I don't have Python installed on my computer. I do have Perl, if anyone is so inclined. Alternatively, if this can be done with a batch file that would be most excellent (only because it will Just Plain Work on my Windows computer, plus I may actually have a chance of understanding and learning from it!). Thanks in advance for any help.
The poster formerly known as Chazzozz. * Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software.

#2 kikz

kikz

    Canelé

  • Hero
  • 19,267 posts

Posted 01 February 2012 - 06:11 AM

Sounds like a job for Microsoft Word's Mail Merge :p

#3 Zzozzach

Zzozzach

    Éclair

  • Atomican
  • 3,250 posts
  • Gender:Male
  • Location:SE Qld
  • Interests:I love torrenting MAME and Pinball emulators. One day I may even use the software.<br /><br />*****<br /><br />Queensland Fire and Rescue Service auxiliary firefighter:<br />Don't play with matches, only WORKING smoke alarms save lives, if your clothes are on fire then stop, drop, and roll.<br /><br />It's the greatest job in the world. Our best days happen when someone else is having their worst one. It feels good to help.<br /><br />*****<br /><br />My day job is primarily concerned with networking and Internet related technologies.<br /><br />I'm an email expert: if you want to send a lot of email, talk to me. I know people.<br /><br />If you have a question related to sending or receiving email, ask me and I can probably help.

Posted 01 February 2012 - 08:14 AM

Sounds like a job for Microsoft Word's Mail Merge :p

You know, I did briefly consider it, but I'm feeding the resulting text files into an SMTP stream so it's imperative they're plain-text and nothing else. Experience has taught that Word loves to sneak in the odd invisible binary character when you least expect it, and I'm not keen to hand-edit 1100 files.
The poster formerly known as Chazzozz. * Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software.

#4 linke

linke

    Banoffee Pie

  • Atomican
  • 5,156 posts

Posted 01 February 2012 - 11:06 AM

Powershell?

$text = "This is a pre-defined block of text 
with an email in it for replacement"

$emails = Get-Content emails.txt
$filecount=0
foreach($email in $emails){
	$text -replace "email",$email >> ('email_' + $filecount + '.txt')
	$filecount++
}

That will grab the emails from a file (I tested using a file with 1 email per line) and output them into individual files with name "email_0.txt. email_1.txt" etc.

Change the filenaming and text block to suit.

#5 Zzozzach

Zzozzach

    Éclair

  • Atomican
  • 3,250 posts
  • Gender:Male
  • Location:SE Qld
  • Interests:I love torrenting MAME and Pinball emulators. One day I may even use the software.<br /><br />*****<br /><br />Queensland Fire and Rescue Service auxiliary firefighter:<br />Don't play with matches, only WORKING smoke alarms save lives, if your clothes are on fire then stop, drop, and roll.<br /><br />It's the greatest job in the world. Our best days happen when someone else is having their worst one. It feels good to help.<br /><br />*****<br /><br />My day job is primarily concerned with networking and Internet related technologies.<br /><br />I'm an email expert: if you want to send a lot of email, talk to me. I know people.<br /><br />If you have a question related to sending or receiving email, ask me and I can probably help.

Posted 03 February 2012 - 11:26 PM

Most Excellent!!

Thanks very much, linke, that gave me exactly the starting advice I was looking for. Since I was adding the email address into the middle of a multi-line text file I had to find something else that would work with a larger block of text. I did some further investigation and it turns out the Set-Content cmdlet was what I needed to use, so I adapted your example script and came up with this:

# Specify the source file here
$emails = Get-Content addy.txt

# Specify the destination file here
$body = Get-Content body.txt

# This initialises the incrementation to be used in file names
$filecount=0

# Here is where the addresses from the source file are used
# to replace the placeholder in the destination file, then
# written to a new file.
foreach($email in $emails){
	$body -replace "email",$email | Set-Content ('sub_' + $filecount + '.txt') 
	$filecount++
}

# The above function only works with files in the same directory.
# To copy the resulting files to another directory, uncomment
# the function below and specify the target directory at the
# '-destination' parameter.
Copy-Item -path .\*.txt -destination C:\example

# To delete files from the original source directory uncomment
# the function below.
#
# USE AT YOUR OWN RISK!
Remove-Item .\*.txt

As you can see, when I execute the script it only writes files back to the same directory. I tried using the -path parameter in the Set-Content cmdlet like so:

Set-Content -path C:\example\('sub_' + $filecount + '.txt')

...but PowerShell didn't like the incrementing file names function and threw an exception. Do you know if there's a way to specify a path and still increment the names?
The poster formerly known as Chazzozz. * Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software.

#6 chrisbrownie

chrisbrownie

    Banoffee Pie

  • Atomican
  • 4,227 posts

Posted 14 March 2012 - 10:24 PM

sc -pat $("C:\example\sub_{0}.txt" -f $filecount)

#7 nicephotog-jvm.net

nicephotog-jvm.net

    Bearclaw

  • Atomican
  • 390 posts

Posted 01 June 2012 - 11:11 AM

For perl , is that what you mean?

#!c:/perl/bin/perl -w
#
$addrCount=0;
$nm=0;
$addresses = "c:/myaddresses.txt";
$template = "c:/mytemplatetext.txt";
$outfilename="c:/outfileprefix";
$templtxt = "";

open(TEMPLATE,"<${template}");
@templtxt=<TEMPLATE>;
$lp=scalar(@templtxt);
while($nm<$lp){
$templtxt.=" ".$templtxt[$nm];
$nm+=1;
}
#endwhile
close(TEMPLATE);
#
open(ADDRESS,"<${addresses}");
#
@addr=<ADDRESS>;
close(ADDRESS);
$addrCount=scalar(@addr);
$nm=0;
while($nm<$addrCount){
##
#
#   match or substitute and cut substrings here of $templtxt to insert $addr[$nm]
#  reassign to $templtxt and can be used below to write to file
##
$outfilename.="".$nm.".txt";
# create file
open(OUTFILE,"<${outfilename}");
close(OUTFILE);
# end create file
# open file for writing to
open(OUTFILE,">${outfilename}");
print $templtxt."\n";
close(OUTFILE);

$nm+=1;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users