Jump to content

_PathSplit help


Recommended Posts

How do you retrieve the file name only?

I have tried path split with various examples, but mine does stile output the files with .wim, which i don't want. :(

; Get wim files from image folder
$WimDir = "Z:\images\*.wim"
_GUICtrlComboBox_AddDir ( $sSelectOS, $WimDir )
_GUICtrlComboBox_SetCurSel ( $sSelectOS, 0 )

What do I need for using _PathSplit with my code?

 

Thanks in advance! :)

Edited by doestergaard
Link to comment
Share on other sites

  • Moderators

doestergaard,

Personally I use these SREs from Malkey: :)

Local $sFile = "C:\Program Files\Another Dir\AutoIt3\AutoIt3.chm"

; Drive letter -                                     Example returns     "C"
Local $sDrive = StringRegExpReplace($sFile, ":.*$", "")

; Full Path with backslash -                         Example returns     "C:\Program Files\Another Dir\AutoIt3\"
Local $sPath = StringRegExpReplace($sFile, "(^.*\\)(.*)", "\1")

; Full Path without backslash -                      Example returns     "C:\Program Files\Another Dir\AutoIt3"
Local $sPathExBS = StringRegExpReplace($sFile, "(^.*)\\(.*)", "\1")

; Full Path w/o drive letter with backslash -        Example returns     "\Program Files\Another Dir\AutoIt3\"
Local $sPathExDr = StringRegExpReplace($sFile, "(^.:)(\\.*\\)(.*$)", "\2")

; Path w/o drive letter w/o backslash -              Example returns     "Program Files\Another Dir\AutoIt3"
Local $sPathExDrBS = StringRegExpReplace($sFile, "(^.:\\)(.*)(\\.*$)", "\2")

; File name with ext -                               Example returns     "AutoIt3.chm"
Local $sFileName = StringRegExpReplace($sFile, "^.*\\", "")

; File name w/o ext -                                Example returns     "AutoIt3"
Local $sFilenameExExt = StringRegExpReplace($sFile, "^.*\\|\..*$", "")

; Dot Ext -                                          Example returns     ".chm"
Local $sDotExt = StringRegExpReplace($sFile, "^.*\.", ".$1")

; Ext -                                              Example returns     "chm"
Local $sExt = StringRegExpReplace($sFile, "^.*\.", "")

MsgBox(0, "Path File Name Parts", _
        "Drive             " & @TAB & $sDrive & @CRLF & _
        "Path              " & @TAB & $sPath & @CRLF & _
        "Path w/o\         " & @TAB & $sPathExBS & @CRLF & _
        "Path w/o Drv      " & @TAB & $sPathExDr & @CRLF & _
        "Path w/o Drv or \ " & @TAB & $sPathExDrBS  & @CRLF & _
        "File Name         " & @TAB & $sFileName & @CRLF & _
        "File Name w/o Ext " & @TAB & $sFilenameExExt & @CRLF & _
        "Dot Extension     " & @TAB & $sDotExt & @CRLF & _
        "Extension         " & @TAB & $sExt & @CRLF)
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23

trytest "C:Program FilesAnother DirAutoIt3/AutoIt3.chm" ehhhhh, the _PathSplit() is a function much much better, calculates many more scenarios ect ect ect ect, is better (recommend) for all users always use the _PathSplit() to be more practicalfasterSafe

@doestergaard

http://www.autoitscript.com/autoit3/docs/libfunctions/_PathSplit.htm

#include <Array.au3>
#include <File.au3>

Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
Local $aPathSplit = _PathSplit("Z:\images\*.wim", $sDrive, $sDir, $sFilename, $sExtension)
_ArrayDisplay($aPathSplit, "_PathSplit of Z:\images\*.wim")


;;$aPathSplit
;~ Row|Col 0
;~ [0]|Z:\images\*.wim
;~ [1]|Z:
;~ [2]|\images\
;~ [3]|*
;~ [4]|.wim

what is wrong ??????

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

@Melba23

The code does remove the ".wim" extension, but only if I give it a direct point to the file, I need this to be done for all files the script finds in that folder, that have ".wim" extension.

@DXRW4E

Tried your code, but I don't know how to get it to display in the combo box - also, will _pathsplit do this for all files in the folder?

 

Thank you both :)

Link to comment
Share on other sites

I have no idea what you want to do them, but here is a general example

#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <File.au3>

Example()

Func Example()
    Local $hCombo

    ; Create GUI
    GUICreate("ComboBox Set Cur Sel", 400, 296)
    $hCombo = GUICtrlCreateCombo("", 2, 2, 396, 296)
    GUISetState(@SW_SHOW)

    ; Add files
    _GUICtrlComboBox_BeginUpdate($hCombo)
    _GUICtrlComboBox_AddDir($hCombo, "D:\IRM_CCSA_X64FRE_EN-US_DV5\sources\*.wim")
    _GUICtrlComboBox_EndUpdate($hCombo)

    ; Select Item
    _GUICtrlComboBox_SetCurSel($hCombo, 1)

    ; Get Cur Sel
    MsgBox($MB_SYSTEMMODAL, "Information", "Cur Sel: " & _GUICtrlComboBox_GetCurSel($hCombo))

    Local $aPathSplit, $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
    $aList = _GUICtrlComboBox_GetListArray($hCombo)
    For $x = 1 To $aList[0]
        $aPathSplit = _PathSplit("D:\IRM_CCSA_X64FRE_EN-US_DV5\sources\" & $aList[$x], $sDrive, $sDir, $sFilename, $sExtension)
        _ArrayDisplay($aPathSplit, "_PathSplit of " & $aList[$x])
        ;MemoWrite($aList[$x])
    Next

    ;or
    Local $sText
    _GUICtrlComboBox_GetLBText($hCombo, _GUICtrlComboBox_FindString($hCombo, "install"), $sText)
    $aPathSplit = _PathSplit("D:\IRM_CCSA_X64FRE_EN-US_DV5\sources\" & $sText, $sDrive, $sDir, $sFilename, $sExtension)
    _ArrayDisplay($aPathSplit, "_PathSplit of " & $sText)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>Example
Ciao. Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

@DXRW4E

I want it to remove the file extension for every file in "Z:images" - doesn't matter if they are .wim files or whatever, just want them gone without having to point to every single file  :)

But as I can see, vbscript would do the trick much easier, so I will have to find a way to use that instead

Thank you for your help :)

Edited by doestergaard
Link to comment
Share on other sites

#include <File.au3>
#include <Array.au3>

Local $sFile, $aFileList = _FileListToArray("Z:\images\", "*",1, True)
If @Error Then
    ;;;error
Else
    For $i = 1 To $aFileList[0]
        ;FileMove($aFileList[$i], StringRegExpReplace($aFileList[$i] & " ", "(\.[^\.\/\\]*)?\h$", ""))   ; does not make sense to do so ????????
        ;;or
        $aFileList[$i] = StringRegExpReplace($aFileList[$i] & " ", "(\.[^\.\/\\]*)?\h$", "")
    Next
EndIf
;;; do what you want with (arrays) $aFileList
_ArrayDisplay($aFileList)
Or Search SubDirectory _FileListToArrayEx '?do=embed' frameborder='0' data-embedContent>>

#include <Array.au3>

$aFileList = _FileListToArrayEx("Z:\images\", "*", 13)
If @Error Then
    ;;;error
Else
    For $i = 1 To $aFileList[0]
        ;FileMove($aFileList[$i], StringRegExpReplace($aFileList[$i] & " ", "(\.[^\.\/\\]*)?\h$", ""))   ; does not make sense to do so ????????
        ;;or
        $aFileList[$i] = StringRegExpReplace($aFileList[$i] & " ", "(\.[^\.\/\\]*)?\h$", "")
    Next
EndIf
;;; do what you want with (arrays) $aFileList
_ArrayDisplay($aFileList)


; #FUNCTION# =======================================================================================================================================================
; Name...........: _FileListToArrayEx
; Description ...: Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)
; Syntax.........: _FileListToArrayEx($sPath[, $sFilter = "*"[, $iFlag = 0]])
; Parameters ....: $sPath   - Path to generate filelist for.
;                 $sFilter - Optional the filter to use, default is *. (Multiple filter groups such as "All "*.png|*.jpg|*.bmp") Search the Autoit3 helpfile for the word "WildCards" For details.
;                 $iFlag   - Optional: specifies whether to return files folders or both Or Full Path (add the flags together for multiple operations):
;                 |$iFlag = 0 (Default) Return both files and folders
;                 |$iFlag = 1 Return files only
;                 |$iFlag = 2 Return Folders only
;                 |$iFlag = 4 Search SubDirectory
;                 |$iFlag = 8 Return Full Path
;                 |$iFlag = 16 $sFilter do Case-Sensitive matching (By Default $sFilter do Case-Insensitive matching)
;                 |$iFlag = 32 Disable the return the count in the first element - effectively makes the array 0-based (must use UBound() to get the size in this case).
;                   By Default the first element ($array[0]) contains the number of file found, the remaining elements ($array[1], $array[2], etc.)
;                 |$iFlag = 64 $sFilter is REGEXP Mod, See Pattern Parameters in StringRegExp (Can not be combined with flag 16)
;                 |$iFlag = 128 Return Backslash at the beginning of the file name, example Return "\Filename1.xxx" (Can not be combined with flag 8)
; Return values .: Failure - @Error
;                 |1 = Path not found or invalid
;                 |2 = Invalid $sFilter
;                 |3 = No File(s) Found
; Author ........: DXRW4E
; Modified.......:
; Remarks .......: The array returned is one-dimensional and is made up as follows:
;                               $array[0] = Number of Files\Folders returned
;                               $array[1] = 1st File\Folder
;                               $array[2] = 2nd File\Folder
;                               $array[3] = 3rd File\Folder
;                               $array[n] = nth File\Folder
; Related .......:
; Link ..........:
; Example .......: Yes
; Note ..........: Special Thanks to SolidSnake & Tlem
; ==================================================================================================================================================================
Func _FileListToArrayEx($sPath, $sFilter = "*", $iFlag = 0)
    $sPath = StringRegExpReplace($sPath & "\", "(?!\A)[\\/]+\h*", "\\")
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRegExp($sFilter, StringReplace('^\s*$|\v|[\\/:><"]|^\||\|\||\|$', "[" & Chr(BitAND($iFlag, 64) + 28) & '\/:><"]|^\||\|\||\|$', "\\\\")) Then Return SetError(2, 2, "")
    Local $hSearch, $sFile, $sFileList, $sSubDir = BitAND($iFlag, 4), $sDelim = "|", $sDirFilter = StringReplace($sFilter, "*", "")
    $hSearch = FileFindFirstFile($sPath & "*")
    If @Error Then Return SetError(3, 3, "")
    Local $hWSearch = $hSearch, $hWSTMP, $SearchWD, $Extended, $iFlags = StringReplace(BitAND($iFlag, 1) + BitAND($iFlag, 2), "3", "0")
    If BitAND($iFlag, 8) Then $sDelim &= $sPath
    If BitAND($iFlag, 128) Then $sDelim = "|\"
    If Not BitAND($iFlag, 64) Then $sFilter = StringRegExpReplace(BitAND($iFlag, 16) & "(?i)(", "16\(\?\i\)|\d+", "") & StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace($sFilter, "[^*?|]+", "\\Q$0\\E"), "\\E(?=\||$)", "$0\$"), "(?<=^|\|)\\Q", "^$0"), "\*+", ".*") & ")"
    While 1
        $sFile = FileFindNextFile($hWSearch)
        If @Error Then
            If $hWSearch = $hSearch Then ExitLoop
            FileClose($hWSearch)
            $hWSearch -= 1
            $SearchWD = StringLeft($SearchWD, StringInStr($SearchWD, "\", 1, -2))
        ElseIf $sSubDir Then
            $Extended = @Extended
            If ($iFlags + $Extended <> 2) Then
                If $sDirFilter Then
                    If StringRegExp($sFile, $sFilter) Then $sFileList &= $sDelim & $SearchWD & $sFile
                Else
                    $sFileList &= $sDelim & $SearchWD & $sFile
                EndIf
            EndIf
            If Not $Extended Then ContinueLoop
            $hWSTMP = FileFindFirstFile($sPath & $SearchWD & $sFile & "\*")
            If $hWSTMP = -1 Then ContinueLoop
            $hWSearch = $hWSTMP
            $SearchWD &= $sFile & "\"
        Else
            If ($iFlags + @Extended = 2) Or StringRegExp($sFile, $sFilter) = 0 Then ContinueLoop
            $sFileList &= $sDelim & $sFile
        EndIf
    WEnd
    FileClose($hSearch)
    If Not $sFileList Then Return SetError(3, 3, "")
    Return StringSplit(StringTrimLeft($sFileList, 1), "|", StringReplace(BitAND($iFlag, 32), "32", 2))
EndFunc    ;==>_FileListToArrayEx
Ciao. Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

If anyone want the complete solution how to filter files from a specific folder, in this case .Wim files, remove the file extension and display it in a combo box, here it is:

; Create combobox
$sSelectOS = GUICtrlCreateCombo ( "", 106, 167, 412, 300, $CBS_DROPDOWNLIST )

; Get wim files from folder
$sWMIService = ObjGet ( "winmgmts:\\" & @ComputerName & "\root\cimv2" )

If IsObj ( $sWMIService ) Then

   $colItems = $sWMIService.ExecQuery ( "SELECT * FROM CIM_Datafile WHERE DRIVE='z:' AND PATH='\\images\\' AND EXTENSION='wim'" )

   For $objItems In $colItems

      $sGetWimFile = $objItems.FileName
      $sWimFile = _StringProper ( $sGetWimFile )
      _GUICtrlComboBox_InsertString ( $sSelectOS, $sWimFile )
      _GUICtrlComboBox_SetCurSel ( $sSelectOS, 0 )

      Next

EndIf

And it was simple too!  :sweating:

Edited by doestergaard
Link to comment
Share on other sites

  • Moderators

doestergaard,

 

@Melba23

The code does remove the ".wim" extension, but only if I give it a direct point to the file, I need this to be done for all files the script finds in that folder, that have ".wim" extension.

_FileListToArray. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23

I read the basics about _FileListToArray from the help file and created this:

#include <GUIComboBox.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

; Create combobox
$SelectOS = GUICtrlCreateCombobox ( "", 106, 167, 412, 200, $CBS_DROPDOWNLIST )

; Get wim files from folder
$WimPath = "Z:\images"
$WimFiles = _FileListToArray ( $WimPath, "*.wim" )
_ArraySort ( $WimFiles, 0, 1 )

For $i = 1 To UBound ( $WimFiles ) -1

   $ListOS = StringRegExpReplace ( $WimFiles[$i], "^.*\\|\..*$", "" )
   GUICtrlSetData ( $SelectOS, $ListOS )

Next

_GUICtrlComboBox_SetCurSel ( $SelectOS, 0 )

If $WimFiles = "" Then

   MsgBox ( $MB_ICONWARNING, "OS Deployment", "WARNING! No operating systems found!" )

EndIf

It worked, thanks!

Edited by doestergaard
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...