Jump to content
gillesg

pb _PathSplit

Recommended Posts

gillesg

Hello,

Using _PathSplit, is it normal that _Pathsplit remove spaces before filename

 

Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "",$sPATH
$sPATH = "c:\xx\yy\  space 2 before 2 after  .au3  "
Local $aPathSplit = _PathSplit($sPATH, $sDrive, $sDir, $sFileName, $sExtension)
ConsoleWrite(_ArrayToString($aPathSplit, "%%")&"%%"&@CRLF)

Result

>Running:(3.3.14.3):D:\Users\gigros\Personnel\Liberkey\MyApps - Direct Access\Autoit3.3.14.3 # Portable\App\autoit3_x64.exe "D:\Users\gigros\Desktop\Perso\Coding local\test_PathSplit.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
c:\xx\yy\  space 2 before 2 after  .au3  %%c:%%\xx\yy\%%space 2 before 2 after  %%.au3  %%

 

Am I doing something wrong ?

 

regards

Share this post


Link to post
Share on other sites
Melba23

gillesg,

It seems the RegEx inside _PathSplit will remove spaces at the beginning and,  in some circumstances, the end of the path elements. But how do you get spaces at the beginning of a path element in the first place - I thought that they were not permitted by the OS and so were automatically stripped when the path was created? That is certainly what happened when I tested just now under both DOS and Windows.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
gillesg

Hello,

I read documentation many times.

Deep diving into the UDF, i find the culprit is \h in the regexp

in the line 

Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)

Should be 

Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)

 

Or may be it is intendionnal ?

Regards

Share this post


Link to post
Share on other sites
Melba23

gillesg,

See my comment above.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
gillesg
7 minutes ago, Melba23 said:

gillesg,

It seems the RegEx inside _PathSplit will remove spaces at the beginning and,  in some circumstances, the end of the path elements. But how do you get spaces at the beginning of a path element in the first place - I thought that they were not permitted by the OS and so were automatically stripped when the path was created? That is certainly what happened when I tested just now under both DOS and Windows.

M23

I think some tools allow it to happend. Automatic renaming tools I suppose.
I am build a tools using reading file on MTP device. And there i found some file like this.

 

Edited by gillesg

Share this post


Link to post
Share on other sites
Bilgus

So.. Roll your own and be sure to post it for the next person :D

Share this post


Link to post
Share on other sites
gillesg
12 minutes ago, gillesg said:

Hello,

I read documentation many times.

Deep diving into the UDF, i find the culprit is \h in the regexp

in the line 

Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)

Should be 

Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]*)?((?:\h*[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)

 

Or may be it is intendionnal ?

Regards

Edit forgot to rewrite \h*

Share this post


Link to post
Share on other sites
gillesg
24 minutes ago, Melba23 said:

gillesg,

See my comment above.

M23

I understand your comment, but see my screen capture.
It's a standard USB disk with NTFS file system.

So in some way it is possible to create such files.

I don't know what to say about the OS restriction. Thru Explorer , yes it is protected, but thru other tools may be not.

 

Gilles

Name with space.jpg

Share this post


Link to post
Share on other sites
gillesg

After keeping on investigating here is a proposal of a modified _PathSplit Function

 

; #FUNCTION# ====================================================================================================================
; Author ........: Valik
; Modified.......: DXRW4E - Re-wrote to use a regular expression; guinness - Update syntax and structure.
; Modified.......: GillesG - Preserving space in filename, and dir.
; ===============================================================================================================================
Func _PathSplitPreserving($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\])?((?:\h*[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
    If @error Then ; This error should never happen.
        ReDim $aArray[5]
        $aArray[$PATH_ORIGINAL] = $sFilePath
    EndIf
    $sDrive = $aArray[$PATH_DRIVE]
    If StringLeft($aArray[$PATH_DIRECTORY], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "[\/\\]+", "\/")
    Else
        $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "[\/\\]+", "\\")
    EndIf
    $aArray[$PATH_DIRECTORY] = $sDir
    $sFileName = $aArray[$PATH_FILENAME]
    $sExtension = $aArray[$PATH_EXTENSION]

    Return $aArray
EndFunc   ;==>_PathSplitPreserving

If some needs it. Or to include in UDF ?

Share this post


Link to post
Share on other sites
Melba23

gillesg,

Open a ticket in Trac - then the Devs can take a look and decide whether to alter the function.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Bowmore

You might find it interesting to read the discussions at these two links about the issues you will face on windows if you mange to create or copy files/folders  from elsewhere that contain leading or trailing spaces. 

https://superuser.com/questions/565334/rename-delete-windows-x64-folder-with-leading-and-trailing-space
    
https://bytes.com/topic/python/answers/460800-spaces-ends-filenames-directory-names-win32


"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

Share this post


Link to post
Share on other sites
jpm

Hi,

I don't fully understand what this function is really doing as the example is OK 

but as you can reproduce with the following script just ceating Under the scriptdir folder and file does not work for the file

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.3
 Author:         Jpm

 Script Function:
    store and run it in a specific folder to ease cleaning of folder with trailing blanks

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

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

; CHANGE To use propose one ====================================================
Local $bProposePreserving = False

Local $aPathSplit, $aFileSplit, $aFullPathSplit
Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""

;To CHECK different cases ======================================================
;~ Local $sPath = @ScriptDir & "\  1 2  " ; FileWrite will fail when creating file in the folder
;~ Local $sPath = @ScriptDir & "\  1 2"; splitting does not contail $sFileName and $sExtension
Local $sPath = @ScriptDir & "\12"; not really working either

ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath = "' & $sPath & '"' & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console
Local $iRemove = DirRemove($sPath, 1)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iRemove = ' & $iRemove & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console

Local $iDir = DirCreate($sPath)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iDir = ' & $iDir & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console

If $bProposePreserving Then
    $aPathSplit = _PathSplitPreserving($sPath, $sDrive, $sDir, $sFileName, $sExtension)
Else
    $aPathSplit = _PathSplit($sPath, $sDrive, $sDir, $sFileName, $sExtension)
EndIf
$aPathSplit[3] = '"' & $aPathSplit[3] & '"'
_ArrayDisplay($aPathSplit, "_PathSplit of " & @ScriptFullPath)

;To CHECK different cases ======================================================
;~ Local $sFile = $sPath & "\  x y  "
Local $sFile = $sPath & "\xy.txt"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sFile = "' & $sFile & '"' & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console


If $bProposePreserving Then
    $aFileSplit = _PathSplitPreserving($sFile, $sDrive, $sDir, $sFileName, $sExtension)
Else
    $aFileSplit = _PathSplit($sFile, $sDrive, $sDir, $sFileName, $sExtension)
EndIf
$aFileSplit[3] = '"' & $aFileSplit[3] & '"'

Local $iFile = FileWrite($sFile, "test")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iFile = ' & $iFile & @CRLF & '>Error code: ' & @error & '    Extended code: ' & @extended & ' (0x' & Hex(@extended) & ')' & @CRLF) ;### Debug Console

_ArrayDisplay($aPathSplit, "_FileSplit of " & @ScriptFullPath)

; strangely working in all case ================================================================================================
If $bProposePreserving Then
    $aFullPathSplit = _PathSplitPreserving(@ScriptFullPath, $sDrive, $sDir, $sFileName, $sExtension)
Else
    $aFullPathSplit = _PathSplit(@ScriptFullPath, $sDrive, $sDir, $sFileName, $sExtension)
EndIf
_ArrayDisplay($aFullPathSplit, "Always OK _PathFullSplit of " & @ScriptFullPath)

; #FUNCTION# ====================================================================================================================
; Author ........: Valik
; Modified.......: DXRW4E - Re-wrote to use a regular expression; guinness - Update syntax and structure.
; Modified.......: GillesG - Preserving space in filename, and dir.
; ===============================================================================================================================
Func _PathSplitPreserving($sFilePath, ByRef $sDrive, ByRef $sDir, ByRef $sFileName, ByRef $sExtension)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\])?((?:\h*[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", $STR_REGEXPARRAYMATCH)
    If @error Then ; This error should never happen.
        ReDim $aArray[5]
        $aArray[$PATH_ORIGINAL] = $sFilePath
    EndIf
    $sDrive = $aArray[$PATH_DRIVE]
    If StringLeft($aArray[$PATH_DIRECTORY], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "[\/\\]+", "\/")
    Else
        $sDir = StringRegExpReplace($aArray[$PATH_DIRECTORY], "[\/\\]+", "\\")
    EndIf
    $aArray[$PATH_DIRECTORY] = $sDir
    $sFileName = $aArray[$PATH_FILENAME]
    $sExtension = $aArray[$PATH_EXTENSION]

    Return $aArray
EndFunc   ;==>_PathSplitPreserving

 

Share this post


Link to post
Share on other sites
gillesg

Hello,

I know that normal interface does not allow to create such path of folder or file, but some tools may allow the user to create such path.

Then _PathSplit break them which might be an issue for the user.

The script allows you to create
  + Folder "  12"            [impossible to create thru wiindows Explorer] => should be "12" 
  + FIle "   xy   .txt"        [impossible to create thru wiindows Explorer] => should be "xx   .txt"

The script does not allow to create

  - Folder "12   "          [impossible to create thru wiindows Explorer]

 

You can create a File and folder pattern that _PathSplit breaks.

Share this post


Link to post
Share on other sites
BrewManNH
1 hour ago, gillesg said:

but some tools may allow the user to create such path.

Stop using those tools as they are broken and don't follow the Windows standards.

If that's not an option, roll your own pathsplit function, as the one included with AutoIt doesn't allow the violation of Windows standards, so your broken tools and it are incompatible.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
BrewManNH

Just as an experiment, I renamed a file on my computer and added a space before its name, I had to use the command line REN command, because Windows doesn't allow it otherwise. It caused all sorts of issues with the file system. OneDrive won't sync the file because of the leading space, and you can't rename it using File Explorer's right click menu.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
funkey
21 hours ago, BrewManNH said:

Just as an experiment, I renamed a file on my computer and added a space before its name, I had to use the command line REN command, because Windows doesn't allow it otherwise. It caused all sorts of issues with the file system. OneDrive won't sync the file because of the leading space, and you can't rename it using File Explorer's right click menu.

You can rename the files with 7Zip and WinRAR in this way too. On my system this causes no problems (no OneDrive installed ;)), but when renaming again with Explorer the leading spaces disappear. I think Win7 has lot more problems with this filenames than Win10.


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

Share this post


Link to post
Share on other sites
BrewManNH

When I tried to rename it with File Explorer it told me that the file name already exists, even though I removed the leading space.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

×