magOO Posted March 15, 2004 Posted March 15, 2004 Hello!I get a Filename from the commandline to my ScriptHow can I get now the Path, the Filename and the Fileextension from the StringExample: $filename = $CmdLine[1]When $filename is C:\Program Files\Directory\Filename.exe then i want to get these Strings:$Path = C:\Program Files\Directory\$Filename = Filename.exe$Filetitle = FilenameI hope somebody can help!Thanks for your Help!magOO
ezzetabi Posted March 15, 2004 Posted March 15, 2004 Just check how many \ are in the string, look for the last one and trim everything on its left and you have the file name. Check how many . there are in the filename, look for the last one and trim everything on its left and you have the extension. If the file has no extension and there are dot in the filename you can have problem... lastly check the filename lenght and trim this lenght from the full filename right to have the path.
Developers Jos Posted March 15, 2004 Developers Posted March 15, 2004 This is a script that can filter out the field you want: $TEST='C:\Program Files\Directory\Filename.exe' $FILENAME="" $PATH="" $TITLE="" $EXT_START=0 $FILE_START=0 For $X = StringLen($TEST) To 2 Step -1 If StringMid($TEST,$X,1) = "." And $EXT_START = 0 Then $EXT_START = $X If StringMid($TEST,$X,1) = "\" And $FILE_START = 0 Then $FILE_START = $X If $FILE_START > 0 Then $FILENAME = StringTrimLeft($TEST,$FILE_START) $TITLE = StringLeft($FILENAME, $EXT_START - $FILE_START -1) $PATH = StringLeft($TEST,$FILE_START) ExitLoop EndIf Next MsgBox(0,'test',"filename:" & $FILENAME & @CR & "Title:" & $TITLE & @CR & "Path:" & $PATH) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Valik Posted March 15, 2004 Posted March 15, 2004 Use this function (Watch wordwrap). expandcollapse popup;=============================================================================== ; ; Description: Splits a path into the drive, directory, file name and file extension parts ; Parameter(s): $szPath - IN - The path to be split (Can contain a UNC server or drive letter) ; $szDrive - OUT - String to hold the drive ; $szDir - OUT - String to hold the directory ; $szFName - OUT - String to hold the file name ; $szExt - OUT - String to hold the file extension ; Requirement(s): None ; Return Value(s): Array with 5 elements where 0 = original path, 1 = drive, 2 = directory, 3 = filename, ; 4 = extension ; Note(s): An empty string denotes a missing part ; ;=============================================================================== Func _SplitPath($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt) ; Set local strings to null (We use local strings in case one of the arguments is the same variable) Local $drive = "" Local $dir = "" Local $fname = "" Local $ext = "" Local $i ; For Opt("MustDeclareVars", 1) ; Create an array which will be filled and returned later Dim $array[5] $array[0] = $szPath; $szPath can get destroyed, so it needs set now ; Get drive letter if present (Can be a UNC server) If StringMid($szPath, 2, 1) = ":" Then $drive = StringLeft($szPath, 2) $szPath = StringTrimLeft($szPath, 2) ElseIf StringLeft($szPath, 2) = "\\" Then $szPath = StringTrimLeft($szPath, 2) ; Trim the \\ $pos = StringInStr($szPath, "\") If $pos = 0 Then $pos = StringInStr($szPath, "/") If $pos = 0 Then $drive = "\\" & $szPath; Prepend the \\ we stripped earlier $szPath = "" ; Set to null because the whole path was just the UNC server name Else $drive = "\\" & StringLeft($szPath, $pos - 1) ; Prepend the \\ we stripped earlier $szPath = StringTrimLeft($szPath, $pos - 1) EndIf EndIf ; Set the directory and file name if present For $i = StringLen($szPath) To 0 Step -1 If StringMid($szPath, $i, 1) = "\" OR StringMid($szPath, $i, 1) = "/" Then $dir = StringLeft($szPath, $i) $fname = StringRight($szPath, StringLen($szPath) - $i) ExitLoop EndIf Next ; If $szDir wasn't set, then the whole path must just be a file, so set the filename If StringLen($dir) = 0 Then $fname = $szPath ; Check the filename for an extension and set it For $i = StringLen($fname) To 0 Step -1 If StringMid($fname, $i, 1) = "." Then $ext = StringRight($fname, StringLen($fname) - ($i -1)) $fname = StringLeft($fname, $i - 1) ExitLoop EndIf Next ; Set the strings and array to what we found $szDrive = $drive $szDir = $dir $szFName = $fname $szExt = $ext $array[1] = $drive $array[2] = $dir $array[3] = $fname $array[4] = $ext Return $array EndFunc; _SplitPath() Usage: Global $szFullPath = "C:\Test\Path\To\file.txt" Global $szDrive Global $szDir Global $szFName Global $szExt _SplitPath($szFullPath, $szDrive, $szDir, $szFName, $szExt) ; Results in: ; $szDrive = "C:" ; $szDir = "\Test\Path\To\" ; $szFName = "file" ; $szExt = "txt"
ioliver Posted March 15, 2004 Posted March 15, 2004 I'm very new to this, but couldn't you just use StringSplit() to split the path by "\". Then use a UBound -1 to get the last part of the array, the filename w/ exe. Then trim off the last 4 chars to s to the filename wo/ the exe. Ian "Blessed be the name of the Lord" - Job 1:21Check out Search IMF
Valik Posted March 15, 2004 Posted March 15, 2004 Random musing: Nobody ever takes Unix paths (/ instead of \) into consideration, even though Windows accepts those...
Nutster Posted March 15, 2004 Posted March 15, 2004 Random musing:Nobody ever takes Unix paths (/ instead of \) into consideration, even though Windows accepts those...Guess where Micro$oft got the idea from in the first place.Unix (remove a lot of stuff) -> CPM (add a little bit) -> DOS (add a GUI) -> Windows But it is still missing the good stuff, including security and 700 utilities. Windows does seem to accept the / in windows explorer, but the command prompt only lets / be an option marker. David NuttallNuttall 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...
Valik Posted March 15, 2004 Posted March 15, 2004 Guess where Micro$oft got the idea from in the first place.Unix (remove a lot of stuff) -> CPM (add a little bit) -> DOS (add a GUI) -> Windows But it is still missing the good stuff, including security and 700 utilities. Windows does seem to accept the / in windows explorer, but the command prompt only lets / be an option marker.On XP Pro SP-1, I can use \ or / interchangeably (Or mixed for that matter) in both explorer and the command line. Some applications may have a problem with *nix style, though because they are looking for a /<switch> notation. But that's an application incompatibility and not really a Windows/MS-DOS one.
CyberSlug Posted March 16, 2004 Posted March 16, 2004 I'm very new to this, but couldn't you just use StringSplit() to split the path by "\". Then use a UBound -1 to get the last part of the array, the filename w/ exe. Then trim off the last 4 chars to s to the filename wo/ the exe.Since Unix-style paths could be used, you should StringSplit by "/\"Stripping off the last four characters works if all parameters will be EXE files but will cause problems it HTML of JPEG could be paramters. Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Josbe Posted March 16, 2004 Posted March 16, 2004 Interesting point. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
Nutster Posted March 16, 2004 Posted March 16, 2004 I'm very new to this, but couldn't you just use StringSplit() to split the path by "\". Then use a UBound -1 to get the last part of the array, the filename w/ exe. Then trim off the last 4 chars to s to the filename wo/ the exe. IanExtensions are not always 4 characters (.xxx). How about: expandcollapse popup Func GetFile($FilePath) Local $x, $f, $i $x = StringSplit($filepath, "/") if $x[0]=1 Then $f=$FilePath Else $f = $x[$x[0]] Endif $x = StripSplit($f, ".") $f = $x[1] For $i = 2 to $x[0] $f = $f & "." & $x[$i] Next Return $f EndFunc David NuttallNuttall 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... 1 year later... dem3tre Posted June 9, 2005 dem3tre Active Members 25 Posted June 9, 2005 This is nice but I would recommend changing the code that breaks out the file extension to search from the left rather than the right since files can have multiple extensions to them (ie., testimage.jpg.bak)Change: ; Check the filename for an extension and set it For $i = StringLen($fname) To 0 Step -1To:; Check the filename for an extension and set it For $i = 0 To StringLen($fname)I don't know if Autoit is smart enough to only evaluate StringLen once or if that is done on each trip through the loop, will have to test that one day.<{POST_SNAPBACK}>
/dev/null Posted June 9, 2005 Posted June 9, 2005 Random musing:Nobody ever takes Unix paths (/ instead of \) into consideration, even though Windows accepts those...even M$ admits that "/" works. From: UNIX Application Migration Guide"In most cases, Windows can also handle the forward slash (/) as a path separator. However, when building cross-platform paths, scripting language compilers can misinterpret even correctly used file path separators or methods."CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *
Valik Posted June 9, 2005 Posted June 9, 2005 This is nice but I would recommend changing the code that breaks out the file extension to search from the left rather than the right since files can have multiple extensions to them (ie., testimage.jpg.bak)Change:; Check the filename for an extension and set it For $i = StringLen($fname) To 0 Step -1To:; Check the filename for an extension and set it For $i = 0 To StringLen($fname)I don't know if Autoit is smart enough to only evaluate StringLen once or if that is done on each trip through the loop, will have to test that one day.<{POST_SNAPBACK}><{POST_SNAPBACK}>Thats one of the two reasons getting the extension is done from the right (The other is a small performance gain). The extension of the file name you use as an example is ".bak". If performed from the left, the extension would be ".jpg.bak" which is not correct. If you are basing your logic on the assumption that anything after the first period is the file extension, then take a look at a file with the name, "Photo.Of.Me.On.My.Vacation.jpg". Using the flawed logic of starting from the left, the extension would end up being ".Of.Me.On.My.Vacation.jpg". Quite obviously, that is not correct. The way the function is written now is the correct way.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now