#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2009
    Posts
    23
    Rep Power
    0

    Script to move text around...


    Hopefully I am posting this in the right place...

    I have some text, of about 700 lines like this:

    <a href="https://twitter.com/twitterhandle/status/534642248095">#301</a> Beastie Boys - Netty's Girl
    <a href="https://twitter.com/twitterhandle/status/222113034104">#300</a> TLC - No Scrubs
    <a href="https://twitter.com/twitterhandle/status/893745637382">#299</a> Oasis - Supersonic
    <a href="https://twitter.com/twitterhandle/status/747456265217">#298</a> Elvis Costello - Human Touch

    And I want to change it all to this:

    #301 <a href="https://twitter.com/twitterhandle/status/534642248095">Beastie Boys - Netty's Girl</a>
    #300 <a href="https://twitter.com/twitterhandle/status/222113034104">TLC - No Scrubs</a>
    #299 <a href="https://twitter.com/twitterhandle/status/893745637382">Oasis - Supersonic</a>
    #298 <a href="https://twitter.com/twitterhandle/status/747456265217">Elvis Costello - Human Touch</a>

    My Googling says maybe a batch script, but I don't know how to go about writing that. So any help would be greatly appreciated!
  2. #2
  3. ASP Hero (2000 - 2499 posts)

    Join Date
    Jun 2006
    Location
    New Springfield, OH
    Posts
    2,045
    Rep Power
    1052
    This script will work. Save it with a .vbs extension and make sure the variables point to real files.

    Code:
    ' Set paths to the files
    strInFile = "C:\path\to\original\file.txt"
    strOutFile = "C:\path\to\output\file.txt"
    
    ' Open the file for reading
    Const ForReading = 1
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objInFile = objFso.OpenTextFile(strInFile, ForReading)
    
    ' Create an output file
    Set objOutFile = objFso.CreateTextFile(strOutFile)
    
    ' Read in the file contents one line at a time
    Do While Not objInFile.AtEndOfStream
    	' Get a line of text from the original file
    	strText = objInFile.ReadLine
    	
    	' Write a corrected line to the new file
    	objOutFile.WriteLine FixLink(strText)
    Loop
    
    ' Cleanup
    objInFile.Close
    objOutFile.Close
    
    Function FixLink(strInput)
    	If strInput <> "" Then
    		' Use a regular expression to identify all of the pieces
    		With New RegExp
    			.IgnoreCase = vbTrue
    			.Global = vbTrue
    			.Pattern = "^(<.*?>)(#\d+)(</a>) (.*)$"
    			Set colMatches = .Execute(strInput)
    		End With
    		Set colParts = colMatches(0).SubMatches
    
    		' Put the pieces back in the right order
    		strOutput = colParts(1) & " " & colParts(0) & colParts(3) & colParts(2)
    	Else
    		strOutput = ""
    	End If
    	
    	' Send the corrected link back to the main script
    	FixLink = strOutput
    End Function
    Last edited by Nilpo; January 10th, 2013 at 03:26 PM.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!!
  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2009
    Posts
    23
    Rep Power
    0
    Thanks! Will give it a go! (Sorry for the slow reply!)
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2009
    Posts
    23
    Rep Power
    0
    Couldn't get it to work

    Should I be running it from cmd with the correct directory and cscript script.vbs? Thanks!

Similar Threads

  1. Replies: 0
    Last Post: August 8th, 2012, 05:12 PM
  2. Script to read a CSV and move files based on paths
    By drl213 in forum Windows Scripting
    Replies: 0
    Last Post: April 28th, 2011, 12:06 PM
  3. Need script to move (video)files once they have been watched
    By JohnLisle in forum Windows Scripting
    Replies: 1
    Last Post: September 24th, 2010, 11:54 PM
  4. Is This Possible to Color a text using VB SCRIPT
    By MRRaja in forum ASP Development
    Replies: 4
    Last Post: August 24th, 2010, 07:03 AM
  5. move text from text box to a table (add a record).
    By tbeersma in forum Microsoft Access Help
    Replies: 3
    Last Post: May 27th, 2004, 09:56 AM

IMN logo majestic logo threadwatch logo seochat tools logo