Jump to content

repitition


Recommended Posts

Is there a way to write this code below without so much repitition?

$Title = _GetTitle("D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\0001.txt")
;MsgBox(0,'',$Title)
sleep(3000)
FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index1.htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)
sleep(3000)
$Title = _GetTitle("D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\0002.txt")
;MsgBox(0,'',$Title)
sleep(3000)
FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index2.htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)
sleep(3000)
$Title = _GetTitle("D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\0003.txt")
;MsgBox(0,'',$Title)
sleep(3000)
FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index3.htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)
sleep(3000)
Func _GetTitle($File)
    $text = FileRead($File)

    $posStart = StringInStr($text,'<title>')
    $posEnd   = StringInStr($text,'</title>')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title
    Else
        $result = StringMid($text,$posStart+7,$posEnd-($posStart+7))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)
EndFunc
Link to comment
Share on other sites

First step I would do is have variables for your paths. 'D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages' is used a lot. A step further would be to put a counter in a loop so it will be like _FileMove(path & i & rest of path, other path & i & rest of other path) where i is your counter.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

While I'm presuming that the 0001 would go beyond and this code wouldn't work, this sample for loop should give you an idea of how the repetition can be used in a loop.

For $i = 1 to 3
    $Title = _GetTitle("D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\000"&$i&".txt")
    ;MsgBox(0,'',$Title)
    sleep(3000)
    FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index"&$i&".htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)
    sleep(3000)
Next

Func _GetTitle($File)
    $text = FileRead($File)

    $posStart = StringInStr($text,'<title>')
    $posEnd   = StringInStr($text,'</title>')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title
    Else
        $result = StringMid($text,$posStart+7,$posEnd-($posStart+7))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)
EndFunc
Link to comment
Share on other sites

Thank you code master Stampy and Bo8ster. Your help is greatly appreciated.

While I'm presuming that the 0001 would go beyond and this code wouldn't work, this sample for loop should give you an idea of how the repetition can be used in a loop.

For $i = 1 to 3
    $Title = _GetTitle("D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\000"&$i&".txt")
    ;MsgBox(0,'',$Title)
    sleep(3000)
    FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index"&$i&".htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)
    sleep(3000)
Next

Func _GetTitle($File)
    $text = FileRead($File)

    $posStart = StringInStr($text,'<title>')
    $posEnd   = StringInStr($text,'</title>')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title
    Else
        $result = StringMid($text,$posStart+7,$posEnd-($posStart+7))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)
EndFunc

Link to comment
Share on other sites

Thank you code master Stampy and Bo8ster. Your help is greatly appreciated.

Code Master Stampy,

The code runs fine with the exception of 0002.txt, and 0010 thru 0020. For some strange reason, the script ignores 0002.txt. I even isolated 0002.txt and ran it by itselfs ....and it still would'nt FileMove the string.

When AutoIT gets to 0010.txt thru 0020.txt, it says ERROR no title when in fact it does have a title string within it to be extracted.(I opened it up and did a manual examination). What can a brotha do in this situation?

Link to comment
Share on other sites

Use ConsoleWrite to print out what is being passed to File Move. I suspect that you need to add additional single quotes as the file name has spaces. Think of passing that string to a dos window as a path, its not going to be able to handle it. example ' "path' "

Edit: Added spaces for clarity - remove the spaces when using the path example.

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

I'm not to sure on how to use ConsoleWrite.....Could ya guide me a little?

FileMove("D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\index2.htm", "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"&$title&".htm",1)

ConsoleWrite("&$title&".htm")

Link to comment
Share on other sites

I've just been given this script, but its not exactly doing what I thought it was going to do, ......

#Include <Array.au3>





$sKeywordsDir = "D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\"
$sIndexDir = "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"
$sTitleDir = "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"

;$sKeywordsDir = "C:\Temp\Extracted Keywords\"
;$sIndexDir = "C:\Temp\New Folder\"
;$sTitleDir = "C:\Temp\New Folder\"

$search = FileFindFirstFile($sKeywordsDir&"*.*")
Dim $FileCount[999]
$FileCount[0] = '0'
While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    $FileCount[0] = $FileCount[0]+1
    $FileCount[$FileCount[0]] = $file
WEnd
FileClose($search)
_ArrayDisplay($FileCount)
For $i = 1 to $FileCount[0]
    $Title = _GetTitle($sKeywordsDir&$FileCount[$i])
    sleep(250)
    MsgBox(0,$i,$sIndexDir&$FileCount[$i]&@CRLF&$sIndexDir&$title&".htm")
    $Title = StringReplace($Title,'"',"'")
    $Title = StringReplace($Title,'?','')
    FileMove($sIndexDir&$FileCount[$i], $sTitleDir&String($title)&".htm",1)
Next




Func _GetTitle($File)
    $text = FileRead($File)

    $posStart = StringInStr($text,'<title>')
    $posEnd   = StringInStr($text,'</title>')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title
    Else
        $result = StringMid($text,$posStart+7,$posEnd-($posStart+7))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)
EndFunc

Its not changing the .htm files like I want like in the script below.

With the script below ,I can execute it and I can see the script go to work and the index.htm files changing to what each .txt file is . What could be the problem?

$sKeywordsDir = "D:\Documents and Settings\Taevon Jones\Desktop\Extracted Keywords\"
$sIndexDir = "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"
$sTitleDir = "D:\Documents and Settings\Taevon Jones\My Documents\One a be HyperVRE Webpages\christmas greetings\"

$search = FileFindFirstFile($sKeywordsDir&"*.*")
$FileCount = '1'
While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    ;MsgBox(4096, "File:", $file)
    $Title = _GetTitle($sKeywordsDir&$file)
    ;MsgBox(0,'',$Title)
    sleep(3000)
    FileMove($sIndexDir&"index"&$FileCount&".htm", $sTitleDir&$title&".htm",1)
    sleep(5000)
    $FileCount = $FileCount+1
WEnd
FileClose($search)


Func _GetTitle($File)
    $text = FileRead($File)

    $posStart = StringInStr($text,'<title>')
    $posEnd   = StringInStr($text,'</title>')

    If $posStart = 0 or $posEnd = 0 Then
        MsgBox(0,'ERROR','This File doesnt Contain a Title')
        $result = '' ; This File doesnt Contain a Title
    Else
        $result = StringMid($text,$posStart+7,$posEnd-($posStart+7))
    EndIf
    $result= StringLower($result)
    Return $result
EndFunc

Func _SendTitle($Title)
    Sleep(250)
    ControlSend("Notepad",'','', String($Title))
    ControlSend('[Class:Notepad]', '', 'Edit1', $Title)
    Sleep(250)
EndFunc
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...