Jump to content

Accessing PathCleanupSpec with AutoIT


Recommended Posts

Hey guys,

I hope I'm posting this in the correct place.

 

I am trying to access PathCleanupSpec in AutoIT, much in the way these things are called in the WinAPIShPath.au3 file. It's not working out.

Here is some code...

Local $Badstring = 'jo|h*nj/oh<n:jo>h?n"j|oh/njo*h>nj:oh"n|j*ohnj/o?hnj|o:h?njohn'

Local $Goodstring = 'Placeholder'

_WinAPI_PathCleanupSpec($Badstring, $Goodstring)

MsgBox(0, 'john!', $Goodstring)

Exit

Func _WinAPI_PathCleanupSpec($sInFilePath, $sOutFilePath)
    MsgBox(0, 'john!', $sInFilePath & ' ' & $sOutFilePath) ; DEBUG

    Local $aRet = DllCall('shlobj_core.dll', 'int', 'PathCleanupSpec', 'wstr', $sInFilePath, 'wstr', $sOutFilePath)                         

    MsgBox(0, 'john!', $sInFilePath & ' ' & $sOutFilePath) ; DEBUG

    Return $sOutFilePath
EndFunc   ;==>_WinAPI_PathCleanupSpec

I tried some stuff, but I feel like I might be out of my depth.

Does anyone have any wisdom to share?

 

Is it even possible? (because I'll take no for an answer in that regard...)

Thank you for your time!

Link to post
Share on other sites

There are many errors in your code.

First you don't follow the API specifications: read again https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-pathcleanupspec

The first argument is a fully qualified valid path.  The second argument is a filename to be cleaned; this string is "cleaned" on-place.

The dll to use is shell32.dll as specified in the function description.

You need to retrieve the changed file name from the array returned by DllCall.

Working code:

Local $sPath = 'D:\Placeholder\subdir'
Local $sFile = 'jo|h*nj/oh<n:jo>h?n"j|oh/njo*h>nj:oh"n|j*ohnj/o?hnj|o:h?njohn'

Local $sFixedFile = _WinAPI_PathCleanupSpec($sPath, $sFile)
ConsoleWrite($sPath & '\' & $sFixedFile)

Func _WinAPI_PathCleanupSpec($sPathName, $sFileName)
    Local $aRet = DllCall('shell32.dll', 'int', 'PathCleanupSpec', 'wstr', $sPathName, 'wstr', $sFileName)
    Return $aRet[2]
EndFunc   ;==>_WinAPI_PathCleanupSpec

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

Welcome to AutoIt and the forum!

As far as I understand DLLCall you should

  • check for errors after the call (@error <> 0)
  • get the result of the function returned in an array

This is described in the help file.

BTW: "It's not working out." doesn't help much. Describe what you expect and what you get as a result (wrong string, error code etc.)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

Fair critisism.

In this small example, what I want to happen is have a string returned without any special characters (This string would be "johnjohnjohnjohnjohnjohnjohnjohnjohnjohn").

This is not working out, by it not seeming to change the string at all.

Sorry for leaving this out.

 

I will do my best to attempt to apply your advice...

Link to post
Share on other sites

Did you at least run my code?

Another way to achieve the same thing PathCleanupSpec() does (except the 8.3 part, but who uses old-FAT now?):

Local $sPath = 'D:\Placeholder\subdir'
Local $sFile = 'jo|h*nj/oh<n:jo>h?n"j|oh/njo*h>nj:oh"n|j*ohnj/o?hnj|o:h?n\john'

Local $sFixedFile = StringRegExpReplace(StringReplace($sFile, '/', '-'), '[\\:*?"<>|]', '')
ConsoleWrite($sPath & '\' & $sFixedFile & @LF)

Local $sFixedFile = _WinAPI_PathCleanupSpec($sPath, $sFile)
ConsoleWrite($sPath & '\' & $sFixedFile & @LF)

Func _WinAPI_PathCleanupSpec($sPathName, $sFileName)
    Local $aRet = DllCall('shell32.dll', 'int', 'PathCleanupSpec', 'wstr', $sPathName, 'wstr', $sFileName)
    Return $aRet[2]
EndFunc   ;==>_WinAPI_PathCleanupSpec

If you insist on removing forward slashes instead of replacing them by a dash, then simply use this:

Local $sPath = 'D:\Placeholder\subdir'
Local $sFile = 'jo|h*nj/oh<n:jo>h?n"j|oh/njo*h>nj:oh"n|j*ohnj/o?hnj|o:h?n\john'

Local $sFixedFile = StringRegExpReplace($sFile, '[\\/:*?"<>|]', '')
ConsoleWrite($sPath & '\' & $sFixedFile & @LF)

 

Edited by jchd
Forgot the 8.3 case

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
  • Moderators

Graesholt,

The path returned from the DLL call is :

Quote

johnj-ohnjohnjoh-njohnjohnjohnj-ohnjohnjohn+

all the remaining characters are legal in a Windows filename, so the call is doing exactly what you ask it to do.

if you want to get rid of all non-alphabetic characters then you need a RegEx like this:

Local $sFile = 'jo|h*nj/oh<n:jo>h?n"j|oh/njo*h>nj:oh"n|j*ohnj/o?hnj|o:h?njohn'
Local $sCleaned = StringRegExpReplace($sFile, "[^[:alpha:]]", "")
ConsoleWrite($sCleaned & @CRLF)

But no doubt a RegEx guru will be along soon to tell me there is a better way - as usual!

M23

Edit: Or even before I get around to posting! 

Edited by Melba23

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 post
Share on other sites

To be honest, the real truth is a bit more involved.  Windows is Unicode-aware and supports a very large number of characters in filenames (I'm restricting to FAT32 and NTFS here, of course).

[:alpha:] isn't enough as it wouldn't allow the dot(s) of extension(s) as well as many other commonly used characters like - or _ and many others.

Yet not all symbols are valid in filenames.

FileWrite("e=mc².123€.©®.⅜+⅝=1.‟†‡”.ℙℚℝℤℕ.47Ω.txt", "Eureka") works fine but not all symbols do.

Ideally one would also need to filter out many Unicode control characters and possibly more things.

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

jchd, I don't know what happened, the error was probably on my end, but I didn't see your first response when I responded myself (thus I only intended to respond to water).

My sincere apologies.
 

But oh my holy stars, thanks a bunch, man!

You really broke this one down to a level I could follow.

Thank you so much for your time, everyone, and have a GREAT day!

Link to post
Share on other sites

No worry.  Glad it works for you.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...