Jump to content

[Resolved] Strip extension and derive 2 different filenames?


Recommended Posts

Always trying to learn how to streamline in AI. If I can figure out this next step, that means there will be no manual workarounds in my script. The entire process will just take one paste.

I've done searches and looked at the help file, but I guess it's because I don't know what it would be called.

If I have am inputbox that requests the full path of a file and I put in, oh,

"d:\folder\subfolder\Research Info 1.txt".
Is there a way that AI could use that information as needed to access the file but then produce a second filename from that:
"d:\folder\subfolder\Research Info 1.jpg"
substituting the TXT extension for the JPG one ...

Why?: To keep research text and pertinent images together, I name them identically except for the extension.

What would the script function, or whatever it would be termed, be called to do the extension switcheroo? Anyone know of something?

Cheers. <g>

Edited by Diana (Cda)
Link to comment
Share on other sites

  • 2 weeks later...

<gr> I give up. So many attempts over several days but I still can't get the _pathsplit to work and there is absolutely _nothing_ in any of the examples I've looked at (and the help file just doesn't make it clear) on how to do what I need. It's used a lot to do much more complex things but no example shows anything this simple, often a problem w/AI since it's geared much more to people who are good at or who know some sort of scripting.

I thought I knew what AI is trying to do but since I'm not resolving the issue, I must be wrong. I believe the root of the problem is illustrated by this rudimentary script below. It fails as the second box, the message box, shows the icon and the title and the OK button but there's no text, nothing. It's blank. So the clue to what I'm missing must lie there:

#include <file.au3>
Dim $szDrive, $szDir, $szFName, $szExt
$PATH = InputBox("Please enter path to the file.","Path",""," ","450","125","700","150")
$TestPath = _PathSplit($PATH, $szDrive, $szDir, $szFName, $szExt)
msgbox(64,"Path",$TestPath)
I believe that the message box should return the file path that I input. Is this correct? If so, can anyone say what's wrong here, pls?

Once I can get the thing to work, I'll try to figure out how to get AI to turn this

c:\filepath\filename.txt (example drive only; path to any text file, not just on C)

into

c:\filepath\filename.jpg

in the script so that it uses both files but with only one user input. It's not a script that changes anything by "moving" (renaming), it's a script that will dump the contents of a text file and then will use that name to extract the filename of its accompanying jpg and will reference that filepath. But that's after _pathsplit works! <g>

Thx. <g>

Edited by Diana (Cda)
Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

Local $msg

GUICreate("My GUI") ; will create a dialog box that when displayed is centered

$input_selected_file = GUICtrlCreateInput("", 10, 20, 300, Default, $ES_READONLY)
$button_select_file = GUICtrlCreateButton("...", 320, 17, 30)

GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
   
    If $msg = $button_select_file Then

        $message = "Choose input TXT file."

        $selected_file_txt = FileOpenDialog($message, @WindowsDir & "\", "Text (*.txt)", 1)

        If @error Then
            MsgBox(4096, "", "No File(s) chosen")
        Else
            $selected_file_txt = StringReplace($selected_file_txt, "|", @CRLF)
            $selected_file_jpg = StringLeft($selected_file_txt, StringInStr($selected_file_txt, ".", 0, -1) - 1) & ".jpg"
            GUICtrlSetData($input_selected_file, $selected_file_jpg)
        EndIf



    EndIf
   
WEnd
GUIDelete()

Edit: Sry, first read, than post (did say that already today once :) ), adjusted example above.

Edited by KaFu
Link to comment
Share on other sites

$message = "Hold down Ctrl or Shift to choose multiple files."

$var = FileOpenDialog($message, @WindowsDir & "\", "Images (*.jpg)", 1 + 4 )

If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
    $var = StringReplace($var, "|", @CRLF)
    $var2 = StringLeft($var,StringInStr($var,".",0,-1)-1)   & ".txt"
    MsgBox(4096,"",$var & @crlf & $var2)
EndIf
Hi, thanks for your response! Much appreciated.

I have to admit that this is so very confusing. I was given pathsplit before to do this and now a whole new other way is presented <g>. I don't churn out code at the drop of a hat like you guys (I WISH!!) <g>. But in the output this does seem to do what's needed. This'll require some study to see how to modify it to the needs of this script.

Back to the drawing board <lol>. Though this time, I just might get something that works.

Thanks!! <g>

Link to comment
Share on other sites

Well, that certainly went much better than _pathsplit!! This looks very promising and works by itself very well:

;--------------------------------------------------------------------------------------------------
$PATH_txt = InputBox("Please enter path to the text file ...","Path",""," ","450","125","700","150")
;--------------------------------------------------------------------------------------------------
If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
    $PATH_txt = StringReplace($PATH_txt, "|", @CRLF)
    $PATH_jpg = StringLeft($PATH_txt,StringInStr($PATH_txt,".",0,-1)-1)   & ".jpg"
    MsgBox(4096,"Paths to text and jpg files ...",$PATH_txt & @crlf & $PATH_jpg)
EndIf

Now I'll see how well I can incorporate it into the rest of a larger script I'm working on <g>. Perhaps there is finally a light at the end of the tunnel! <g>

p.s., Sorry, I have to make things really simple and put descriptions in variables rather than just plain $var.

Link to comment
Share on other sites

Well, that certainly went much better than _pathsplit!! This looks very promising and works by itself very well:
;--------------------------------------------------------------------------------------------------
$PATH_txt = InputBox("Please enter path to the text file ...","Path",""," ","450","125","700","150")
;--------------------------------------------------------------------------------------------------
If @error Then
    MsgBox(4096,"","No File(s) chosen")
Else
    $PATH_txt = StringReplace($PATH_txt, "|", @CRLF)
    $PATH_jpg = StringLeft($PATH_txt,StringInStr($PATH_txt,".",0,-1)-1)   & ".jpg"
    MsgBox(4096,"Paths to text and jpg files ...",$PATH_txt & @crlf & $PATH_jpg)
EndIfoÝ÷ ØÚ0#eÇ0zYHq©âÊ+¦Úµè­{h¶«zËh}©Z®«±Êâ¦Ò7öl(®H§å¶-=êáj-êÞÇâ©eÉ©bZ¶Ø^zwh~Ø^¶éçz]÷Ø ¶¢ºò"¯zÚ&jG­)ಷ¬jey©Ý¦ë]zÇ+b¢{"ö«¦åzÊÚ¶«¶§ë-¦V¢Mú½ªÿªê-x¬¶Úªâ'è­û­º·«y÷«zw¶¢ºÞrÛ¬zÏjØR¦X­jwOjØLjG­¡ÈZ©j»l¡öºY_W©jØHØb±Æ¬zÚ­æ­y©ÞÂv¦{­;¬¶Ø^{^بÈZN«yªÞ¢Ø^¯²²Úr¾)ඬnëhê¶vö§µ¨²î²)à=«aJbµ©Ý=«a1©ËajØ­z{.­ë-«méÞ­+¢ºÞrÙr~æjÛ^v)ìz»b
=Ù·­Á秶©jØij»ljÊÞªè«yÔá{MúMë-=«a½ªâi¹^É趷§yç^v)í+jǺجiªëk'(Ö¢)දj[±«4ß«3¸¯{Mú³0â¯Mú³1Mjg´ß«3pØyzW¦z{l«{

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

<dancing jig><dancing jig>THANK YOU!!! Phew, no wonder I wasn't getting anywhere! _PathMake was missing and nowhere did I find a direct link to that information. I didn't realize that _PathSplit needed to work in conjunction with _PathMake. d'uh!

This now seems to be almost fully automated. It's absolutely amazing! All I need to do is input the path of a text file, which is easy to do with a shell extension context app I use that allows me to get full path to any file via a simple right click. I dump that into an inputbox and the rest is like magic. An html page is produced on the desktop that opens up, ready for printing.

Finetuning at some future date will be to change the simple inputbox to a GUI and to add an "Add path from clipboard button" to that.

Here is the script:

;
; AutoIt 3x
;
#include <_PartitionLetters.au3>     ; this replaces drive letters
#include<_WEReplacement.au3>     ; references a Windows Explorer replacement program
#NoTrayIcon     ; AutoIt's icon doesn't show in systray
TraySetIcon("Shell32.dll", 153)     ; changes the icon displayed in the systray
AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!


;===== Edit here below only: ===========================================
$WebpageTitlebarName = "(chg name here)"
$WebpageTitle        = "(chg name here)"
;===============================================================


;-----------------------------------------------------------------------------------------------------------------------------------------------
;  1.  HTML page contents from a text file:
    #include <File.au3>
    $PATH = InputBox("Please enter path to the text file.","Path",""," ","450","125","700","150")
    $TxtFile = $PATH
    ; replace regular hard returns in text file with html line breaks, <br>:
    $sStr = FileRead($TxtFile)
    $sStr = StringStripCR($sStr)
    ClipPut (StringReplace($sStr, @LF, "<br>"))
;-----------------------------------------------------------------------------------------------------------------------------------------------
;  2.  Path to accompanying graphic via inputbox text filepath:
    #include <file.au3>
    Dim $szDrive, $szDir, $szFName, $szExt
    $Graphic_IfExists = _PathSplit($PATH, $szDrive, $szDir, $szFName, $szExt)
    $PathToGraphic = _PathMake($szDrive, $szDir, $szFName, "jpg")
;-----------------------------------------------------------------------------------------------------------------------------------------------
;  3.  Create HTML file on desktop and send information to it:
    $TempFILE = @DesktopDir & "\HTMLfile- Temporary.html"
    FileDelete($TempFILE)
    $TEXTdump = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' & @CRLF & _
                '<HTML><HEAD><TITLE>' & $WebpageTitlebarName & '</TITLE>' & @CRLF & _
                '<META content="text/html; charset=windows-1252" http-equiv=Content-Type>' & @CRLF & _
                '<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>' & @CRLF & _
                '<BODY aLink=#ff9933 bgColor=#C8B89B link=#003399 text=#000000 vLink=#996633>' & @CRLF & @CRLF & _
                '<font color="#804040" face="times new roman"></font>' & @CRLF & @CRLF & '<a name="linkTop"><center><h1><b><u>' & $WebpageTitle & _
                '</u></b></h1></center>' & @CRLF & @CRLF & _
                ClipGet() & @CRLF & _
                '<br><br><center><img src="' & $PathToGraphic & @CRLF & _
                '" alt=""></center><br>' & @CRLF & '</CENTER></BODY></HTML>'
    FileWrite($TempFILE, $TEXTdump)
;-----------------------------------------------------------------------------------------------------------------------------------------------
;  4.  Launch HTML file in browser and in notepad:
;  -  get rid of notepad opening at some point?
    If FileExists($TempFILE) Then ShellExecute("notepad.exe", $TempFILE, "", "Open", @SW_MAXIMIZE)
        WinWait("Notepad")
    If FileExists($TempFILE) Then ShellExecute($TempFILE, "", "", "Open", @SW_MAXIMIZE)
        WinWait($TempFILE)

Thanks! I'll finetune but this looks good as it stands. Testing will prove. I'll be using this extensively. I foresee this being the single AI script that I use the most from now on!

Cheers. <g>

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...