Sign in to follow this  
Followers 0
emmanuel

Strip A String Back To The First Backslash?

12 posts in this topic

Larry, I bet you could write this in your sleep...

to properly do the dircreate based on the $sztarget var, I need to strip the string of the filename at the end... how? I can almost see the answer in my head... but the examples in the helpfile are all so simple, they don't seem to apply...

Func getfile($szFile, $szTarget)
   If Not FileExists($szDownloadDir) Then
      DirCreate($szDownloadDir)
   EndIf
   If Not FileExists($szTarget) Then
      DirCreate($szTarget)
   EndIf
   TrayTip("Downloading...", $szFile, 10)
   $IntErrCheck = URLDownloadToFile($szBaseURL & $szfile,$szDownloadDir & "\" & $szTarget)
   if $IntErrCheck = 0 then 
      $IntErrResponse = MsgBox(2,"Error Downloading " & $szFile, "There was an error downloading " & $szFile)
      If $IntErrResponse = 3 then 
         Exit;abort
      ElseIf $IntErrResponse = 4 Then
         getfile($szFile,$szTarget)
      ElseIf $IntErrResponse = 5 Then
      EndIf
   EndIf
   
EndFunc  ;==>getfile

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Use the _SplitPath() function I've posted. It seperates a path into the 4 logical components:

Drive

Directory

File name

Extension

It's being used in the FileInstall helper script so you can get it from there. You can just recombine the Drive & Directory after splitting the path.

Edit: See here for the full source to the function and an example of usage.

Edited by Valik

Share this post


Link to post
Share on other sites

thank you very much for pointing to the full version, needed the explanations... will this work on something without a drive? like a vlaue of "new\Aut2exe\AutoItSC.bin"


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

what did I do wrong?

---------------------------

AutoIt Error

---------------------------

Line 37 (File "C:\Documents and Settings\&eplesh\My Documents\script scraps\update autoit\get and install latest autoit.au3"):

_SplitPath($szTarget, ByRef $szTargetDrive, ByRef $szTargetDir, ByRef $szTargetFName, ByRef $szTargetExt)

_SplitPath($szTarget, ^ ERROR

Error: Expected a variable in user function call.

---------------------------

OK

---------------------------

I removed the ByRefs from that, got this:

---------------------------

AutoIt Error

---------------------------

Line 37 (File "C:\Documents and Settings\&eplesh\My Documents\script scraps\update autoit\get and install latest autoit.au3"):

_SplitPath($szTarget, $szTargetDrive, $szTargetDir, $szTargetFName, $szTargetExt)

_SplitPath($szTarget, ^ ERROR

Error: Variable used without being declared.

---------------------------

OK

---------------------------


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

Dim $szTargetDrive, $szTargetDir, $szTargetFName, $szTargetExt

fixed that...


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

though I am apparently not getting back either $szTargetDir or $szTarget[2] with the folder info.


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

It works fine for me. As far as I know, 1 or more parts can be missing and it will report what is there. I tested it with your path you showed and it worked fine.

$path = "new\Aut2exe\AutoItSC.bin"
Local $d, $dr, $fn, $ext

_SplitPath($path, $d, $dr, $fn, $ext)

MsgBox(4096, "", $d & @CRLF & $dr & @CRLF & $fn & @CRLF & $ext)

Share this post


Link to post
Share on other sites

The current "unstable" AutoIt build has improved StringInStr so that it has an "occurrence" parameter. StringTrimLeft($filename, StringInStr($filename,"\",0,-1)

Or for a slightly better version:

MsgBox(4096,"Example", stripPath("C:\Windows\System32\calc.exe"))

Func stripPath($filename)
    $filename = StringReplace($filename, "/", "\");in case Unix-style path
    Local $pos = StringInStr($filename,"\",0,-1)
    Return StringTrimLeft($filename,  $pos)
EndFunc

Come on, you know you want the "unstable" download :D


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

yeah, you know what's funny? I'm writing this script to automatically download the unstable version and install it. So, funny enough, looks like I will have to install the unstable version to script the install of the unstable version. :D I believe that's called irony.


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I think the term is ugh. but seriously. What's people's feelings on pasting whole scripts up here when asking for help? Is it rude? Is it asking too much?

Edited by emmanuel

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

What's people's feelings on pasting whole scripts up here when asking for help?  Is it rude?  Is it asking too much?

It depends on size. Try to keep it small. Try to isolate the one section that is giving you the most grief. If that doesn't help enough, you may get asked to post more of the script.

If you want to post a working script (Way to go! :D), put it in Scripts and Scraps.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

It depends on size.  Try to keep it small.  Try to isolate the one section that is giving you the most grief.  If that doesn't help enough, you may get asked to post more of the script. 

If you want to post a working script (Way to go! :D), put it in Scripts and Scraps.

:huh2: thanks, that's basically what I've been aiming at... and now that I've started to grok functions it's easier to just post what the problem is instead of the whole script...

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
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
Sign in to follow this  
Followers 0