Jump to content

Cut text x spaces


 Share

Recommended Posts

I have the following text...

Last logon for Jsmith was from mightymouse on Tue Jan 22 2013 at 10:32 AM

I can get it into a file using autoit, but what I would like is to be able to only put some text in a file.

How do I cut the text at say the 8th space and put the rest into a variable.

eg I just want the text "Tue Jan 22 2013 at 10:32 AM" which of course will change all the time based on date.

I will take the text and then put it in a variable which I will write out to a file.

I have looked at various helps and tried a number of things but cant seem to find what I want. I am not a programer but pretty good at taking other peoples scripts and using them for my own use.

Thanks.

Link to comment
Share on other sites

Using a non-RegEx version of the string functions, you can do it this way.

$String = "Last logon for Jsmith was from mightymouse on Tue Jan 22 2013 at 10:32 AM"
ConsoleWrite(StringMid($String, StringInStr($String, " ", 0, 8) + 1) & @CRLF) ; prints out the string from the 8th space char plus one to the end of the string

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

;You have moved me along a considerable amount.

;However its not quite working the way I think it should.

I run this command that gives me the sentence that I mention in my OP.

It all runs fine, including your component. Accpet for these things.

I had to change the number from 8 to 15. This seems to cut the string where I want it.

When I run it without the Second msgbox the debug window shows the right edited string.

However when I add the second msgbox the output to the msgbox is 31. I dont get that.

RunWait(@ComSpec & " /c C:WindowsSystemtruelast.exe jsmith >> C:tempa.log" ,"",@SW_HIDE)

$OutPut = FileRead( "c:temp" & "a.log",FileGetSize("c:temp" & "a.log")) ; Puts into varable

FileDelete( "c:temp" & "a.log") ; Once its in a varable now delete the file.

MsgBox(4096, "Results", $OutPut ) ; Test to see if the variable is there.

$NewOut=ConsoleWrite(StringMid($OutPut, StringInStr($OutPut, " ", 0, 15) + 1) & @CRLF)

MsgBox(4096, "Results", $NewOut ) ; Test to see if the variable is there.

Regards

Shalom

Edited by stevwolf
Link to comment
Share on other sites

#include <String.au3>
$string = "Last logon for Jsmith was from mightymouse on Tue Jan 22 2013 at 10:32 AM"
$result = StringStripWS(_StringReverse(StringLeft(_StringReverse($string), StringInStr(_StringReverse($string)," ",0,7))), 3)
ConsoleWrite($result & @CRLF)

Never mind... basically the same solution as BrewManNH.

Edited by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

However when I add the second msgbox the output to the msgbox is 31. I dont get that.

That's because what you're printing in your second MsgBox is the return code from the ConsoleWrite command, which is the number of characters written to the console at your line...

$NewOut=ConsoleWrite(StringMid($OutPut, StringInStr($OutPut, " ", 0, 15) + 1) & @CRLF)

Edit: I hate how when you paste text, it sometimes adds the font strings and colors. I need to remember to do a "Paste as plain text" from Chrome.

Edited by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

stevwolf,

This works, however, it depend on the string format remaining constant.

; create test file

local $str = 'Last logon for Jsmith was from mightymouse on Tue Jan 22 2013 at 10:32 AM'
filewrite(@scriptdir & '\a.log',$str)

;RunWait(@ComSpec & " /c C:\Windows\System\truelast.exe jsmith >> C:\temp\a.log" ,"",@SW_HIDE)

;$OutPut = FileRead( "c:\temp" & "\a.log")                                              ; < ---------------- This will populate your variable
$OutPut = FileRead(@scriptdir & "\a.log")                                               ; < ---------------- This will populate your variable

FileDelete(@scriptdir & "\a.log") ; Once its in a varable now delete the file.

MsgBox(4096, "Results", $OutPut ) ; Test to see if the variable is there.

$NewOut=StringMid($OutPut, StringInStr($OutPut, " ", 0, 8) + 1)                         ; <------------ create $NewOut with partial string
ConsoleWrite($NewOut & @CRLF)                                                           ; <------------ This will write to the console

MsgBox(4096, "Results", $NewOut )                                                       ; <------------ This will write it to a msgbox

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...