Sign in to follow this  
Followers 0
Chimaera

Quick Reg Exp Question

11 posts in this topic

Been looking through all my saved stuff trying to find a piece of code that removes a trailing slash on the end of a local address.

Trouble is in the early days i didnt comment all my stuff as i used it and i cant find the piece

Now i did find this in my folders but im not sure what it does, even with the help file, it has something to do with \

StringRegExpReplace($soft_path, "^(.*)\\.*$", "$1\\")

Does this remove the trailing slash?

Problem is i get some links with a "\" and some without

What im after is if i had

Z:\Lessons\

It would make it

Z:\Lessons

Or

Z:

etc

Or the other way round so that i get a consistent approach to the location and then can add the slash after manually so i can guarantee it works

Share this post


Link to post
Share on other sites



Look at getdirectoryformat in my signature.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for that but not working for some reason

Here is a reproducer so you can see what im doing

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
_GetDirectoryFormat($sDestSelect, 1)

$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR

If you click a root it still shows C:

If you go deeper it doesn't add the username etc properly and i end up with a missing

Eg below

Z:Jez11.08.2012

Z:LessonsJez11.08.2012

Im wondering if its to do with the file select folder bit Edited by Chimaera

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That removes the last backslash and everything after it then adds the backslash in again although it would be better written with (.+) instead of (.*) although in the case of a folder path it won't really matter. What are you trying to get?

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi George

Im trying to get this

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR

So if i click a top level in th file select folder window

It gives me C:

Or If i go lower For example C:Lessons It then adds the before adding this to the end of it

@UserName & "" & @MDAY & "." & @MON & "." & @YEAR

I think the origanal i posted was probably one of yours

StringRegExpReplace($soft_path, "^(.*).*$", "$1")

To sum up im trying to make sure at the end of $sDestSelect It has a before i add the extra folder info

Edited by Chimaera

Share this post


Link to post
Share on other sites

Thats what it should be doing as it sits now except it will also remove the last folder in the path if there is no trailing backslash.

Will the paths always be just folders or will you be pointing to a file and getting back the folder structure? That's what that regex is designed to do.

If they are always folders then you don't need a regexp.

If NOT StringRight($sDestSelect, 1) = "" Then $sDestSelect &= ""

You could also check that it is a folder with FileGetAttrib()


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Its basically selecting a drive or a folder to backup no files

I started with this on a fresh sheet and it still doesn't work

Is this a bug?

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
If NOT StringRight($sDestSelect, 1) = "" Then $sDestSelect &= ""

$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR

ConsoleWrite($sDestination & @CRLF)

If you select just C for eg you get

C:Jez11.08.2012

If i doubleclick c and go a folder below it then i get this

C:ATIJez11.08.2012

There is supposed to be a between ATI & Jez eg ATIJez

Can anyone else make this happen?

Btw im win 7 64 bit not that it should matter really

Edited by Chimaera

Share this post


Link to post
Share on other sites

I screwed up. This works

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
If StringRight($sDestSelect, 1) <> "" Then $sDestSelect &= ""
$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR
ConsoleWrite($sDestination & @CRLF)

And this also works

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
If StringRegExp($sDestSelect, "^.+[[:alnum:]]$") Then $sDestSelect &= ""
$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR
ConsoleWrite($sDestination & @CRLF)

and that would be considered abuse of a RegEx.

But the really good news is that FileSelectFolder() never adds the backslash so you don't even have to check for it.

$sDestination = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") & "" & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR
If @Error Then ;; In case someone clicks the cancel button or closes the FileSelectFolder() window
ConsoleWrite("Error" & @CRLF)
Else
ConsoleWrite($sDestination & @CRLF)
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

The reason is Forum Software!! It removed the backslash in my SRE that's why it didn't work, darn. I have updated it.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thx guys at least its not me going crazy lol

This worked for me

$sDestSelect = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
If StringRight($sDestSelect, 1) <> "" Then $sDestSelect &= ""
$sDestination = $sDestSelect & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR
ConsoleWrite($sDestination & @CRLF)

However this one did not

$sDestination = FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") & "" & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR
If @Error Then ;; In case someone clicks the cancel button or closes the FileSelectFolder() window
ConsoleWrite("Error" & @CRLF)
Else ConsoleWrite($sDestination & @CRLF)
EndIf

If you select just C: for eg it gives a double in the path

eg

C:Jez11.08.2012

But either way it appears to be sorted

Thx again

Edited by Chimaera

Share this post


Link to post
Share on other sites

First thing to learn about SREs is when to use them and this wasn't something that required it at all but this would have worked fine

$sDestination = StringRegExpReplace(FileSelectFolder("Choose a drive.", "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"), "(.)?$", "$1") & @UserName & "" & @MDAY & "." & @MON & "." & @YEAR

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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