Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Needing to strip text out of a file and save it into different file  (Read 3631 times)

0 Members and 1 Guest are viewing this topic.

jeff g

    Topic Starter


    Newbie

    OK, it's been a very long time since I've done batch programming and the EX gave away most of my old books from 10 years ago.  Now I'm stuck...

    I'm trying to write a batch program that will strip a text file of everything before the second quote mark and then save the remainder into a file.

    Here's the application.  I wrote (modified) a batch program that will take pictures that I email from my cell phone to myself and directly upload them to a website.  I have it where it will take the text of the email and upload that also, but it uploads all the header information also. 

    What the batch program currently does is rename the attachment, append the new filename to the index.html file, append the .mbx file (only the one message in it) to the index.html file and then upload the new index.html file to my web server.  Works like a champ except the .mbx file contains all the header information and that ends up on the webpage.

    The header contains, as an example,

    From ???@??? Mon Dec 01 15:56:52 2008 X-Eon-Dm: dm0204 Return-Path: <[email protected]> Received: from atlmtaow01.cingularme.com (66.102.165.6 [66.102.165.6]) by dm0204.mta.everyone.net (EON-INBOUND) with ESMTP id dm0204.492ccd9f.45848e7 for <[email protected]>; Mon, 1 Dec 2008 12:56:26 -0800 X-Mms-MMS-Version: 18 Date: Mon, 1 Dec 2008 14:59:45 -0600 X-Nokia-Ag-Internal: ; smiltype=true; internaldate=1228165185853 Content-Type: multipart/mixed; boundary="----=_Part_29471057_6914833.1228165185861" X-Mms-Delivery-Report: 1 Received: from schagw01 ([209.183.32.189]) by atlmtaow01.cingularme.com (InterMail vM.6.01.04.00 201-2131-118-20041027) with ESMTP id <20081201205625.JHPZ480.atlmtaow01.cingu larme.com@schagw01> for <[email protected]>; Mon, 1 Dec 2008 15:56:25 -0500 X-Mms-Transaction-ID: E13AB57E7FD01E From: <[email protected]> To: [email protected] X-Mms-Message-Class: 0 X-Mms-Read-Reply: 1 Mime-Version: 1.0 Message-ID: <32216880.1228165185861.JavaMail.wluser@schagw01> X-Mms-Priority: 1 X-Mms-Message-Type: 0 Subject: Multimedia message X-Nokia-Ag-Version: 2.0 Attachment Converted: "c:\eudora\attach\Multimedia message.jpg" Wild turkey

    I want to delete everything but the end, "wild turkey"

    How do I do this?

    Thanks in advance,

    Jeff
    « Last Edit: December 03, 2008, 10:53:34 AM by jeff g »

    Dias de verano

    • Guest
    Re: Needing to strip text out of a file and save it into different file
    « Reply #1 on: December 01, 2008, 03:33:31 PM »
    Is this path liable to change?

    "c:\eudora\attach\

    Try this. Header is in htest.txt and appears to be one long line

    %var% may be what you want.

    Quick hack.

    Code: [Select]
    @echo off
    for /f "tokens=1-4 delims=\" %%A in (htest.txt) do (
    echo %%D> temp.txt
    )

    for /f "tokens=1,2,3* delims=. " %%A in (temp.txt) do (
    set var=%%D
    )
    echo %var%
    « Last Edit: December 01, 2008, 03:47:30 PM by Dias de verano »

    jeff g

      Topic Starter


      Newbie

      Re: Needing to strip text out of a file and save it into different file
      « Reply #2 on: December 02, 2008, 08:29:10 PM »
      It didn't work.  Turns out that the header is not all 1 line as it appears above.  :-[  Thanks for the help, though...it helped me work out a solution.

      I did some more research and between last night and today (2 days off sick) I came up with a simple, but inelegant solution that almost does what I want it to.   Eudora is used as the mail package simply because it saves the email as ASCII text and saves attachments in a separate directory.  Cingular (my cellphone carrier) names picture attachments as "multimedia message.jpg"

      The desired result is to mail a pic from my cellphone to myself and have it upload to my website...blog if you wish, and include the SMS message text with the photo.  I copied the basic batch file from a website where the owner uploads his pictures in a link format.  I didn't want that, so I modified the batch file to create the index.html file to directly display the pics.  That portion works fine.  However, just copying the message text to the index.html file brings all the header information with it and that was not acceptable.

      So, after not being able to get Dias' script to work (I gave inaccurate parameters with my original question) I've got this, which requires that I have to start the text message with '..'.  The double period is not found in any of the header information.

      @echo off

      :start

      :: set the variables
      set MAILDIR=c:\eudora\attach ::(your default directory goes here)
      set WEBSITE=-----
      set USERNAME=-----
      set PASSWORD=-----

      :: if there is no new picture, quit
      IF not exist %maildir%\multim~1.jpg GOTO end

      :: all subsequent functions will occur in the default directory
      cd %maildir%

      :: Finds the date and formats it appropriately
      :getdate
      date < NUL | find "current" > date.txt
      for /f "tokens=1,2,3,4,5,6,7" %%a in (date.txt) do echo %%f > phone.date
      for /f "delims=/ tokens=1,2,3" %%a in (phone.date) do set PHONEDATE=2008%%a%%b


      :: finds the time and formats it appropriately
      :gettime
      time < NUL | find "current" > time.txt
      for /f "tokens=1,2,3,4,5" %%a in (time.txt) do echo %%e > phone.time
      for /f "delims=:. tokens=1,2,3" %%a in (phone.time) do set PHONETIME=%%a%%b%%c


      :: sets the filename to the date and time and renames the picture to the new filename
      :renamefiles
      set PHONEFILE=%PHONEDATE%%PHONETIME%
      rename multim~1.jpg %PHONEFILE%.jpg > NUL


      :: appends the picture to the index.html and automatically sizes it to 800X600
      :: right clicking on the picture gives the option of viewing it full size.
      :addpicstomenu
      echo ^<img style="width: 800px; height: 600px;" alt="" src="%PHONEFILE%.jpg"^>^<br^> >> index.html

      :: grabs the text from the SMS message and strips the ".." from it.  Then the text is appended to the index.html file and 2 line breaks are added.
      :: this puts the text directly under the photo and leaves a couple of blank spaces before the next picture

      :getpicturedescription
      find ".." <c:\eudora\in.mbx> picturetext.txt
      for /f "tokens=1-25" %%a in (picturetext.txt) do echo %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y > phone.pic
      for /f "delims=. tokens=1" %%a in (phone.pic) do set pictext=%%a
      echo %pictext% > pictxt.txt
      type pictxt.txt >> index.html
      echo ^<br^>^<br^> >> index.html

       
      :: ftp.txt holds the inputs for the ftp command to log in and upload the files to the web server
      :ftpfilesup

      echo open %WEBSITE%> ftp.txt
      echo %USERNAME%>> ftp.txt
      echo %PASSWORD%>> ftp.txt
      echo put %PHONEFILE%.jpg >> ftp.txt
      echo put index.html >> ftp.txt
      echo quit >> ftp.txt
      ftp -s:ftp.txt

      :: deletes all the temp files
      :cleanup
      del phone.pic
      del pictxt.txt
      del picturetext.txt
      del phone.date
      del phone.time
      del date.txt
      del time.txt
      del ftp.txt
      del picture.jpg
      del c:\eudora\in.mbx


      :end


      The above script is the final product.  To make the script work as a blog source, I have the bat file set to run once every 5 minutes (lowest interval allowed by Vista's scheduling program) and Eudora is set to pull mail every other minute.

      The whole script works like a champ and the only issue that I'll have with it is to remember to start all the messages I send with '..' or there won't be any text under the pictures.  The only downsides are that I can't use a '.' at the end of a sentence or the rest of the message gets truncated and I'm limited to 24 words in the SMS message, but that's not an issue as descriptions should be short and sweet.

      Like I said, it's not pretty, but it works.  I've cleaned it up and posted the whole script here in case anyone wants to have a cell phone picture blog on their own website without having to play with some of the blogging software that is out there.  Besides, being able to do a photo blog with your own hosting service means that you don't have to worry about 1 step of the commercial blogging industry going out of business and messing you up.






      « Last Edit: December 03, 2008, 06:51:52 PM by jeff g »