Jump to content

help parsing file name


sbrady
 Share

Recommended Posts

below is a bunch of code that takes desktop files selected in a window and displays a message box if the name contains the word "mix". The result returned (on line 40) is something like this:

C:\Documents and Settings\sebrad\Desktop\shawn\AR123 Smith, Bob v1 FULL MIX.txt

I just need to make

AR123 Smith, Bob v1 FULL MIX.txt

into this

AR123 Smith, Bob FULL MIX v1.txt

any ideas. Thanks.

#include <Array.au3>
Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")$aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")
For $i = 1 To $aWinList[0][0]
    ; Get base folder and selections
    $aSelection = _ExplorerWinGetSelectedItems($aWinList[$i][1])
   
; Display as strings
    ConsoleWrite("Explorer Window: " & $aSelection[1] & " Selection(s)" & @CRLF)
    For $j = 2 To $aSelection[0] + 1
        ConsoleWrite(@TAB & $aSelection[$j] & @CRLF)
    Next
    ; Display as array
    ;_ArrayDisplay($aSelection, "Explorer Instance #" & $i & " / " & $aWinList[$i][0])




; START SHAWN's CODE____________________________
; repeat with the selected items
For $File_Name = 1 To $aSelection; check if file name contains "Full MIX" or "SOT MIX" or "M&E MIX"
$result = StringInStr($File_Name, "mix",0,1)
If $result > 0 Then
    MsgBox(4096, "THE result", $File_Name);C:\Documents and Settings\sebrad\Desktop\shawn\AR123 Smith, Bob v1 FULL MIX.txt
    ; rename the variable $File_Name which is,   AR123 Smith, Bob v1 FULL MIX.txt    to this    AR123 Smith, Bob FULL MIX v1.txt

EndIf
  
Next
; END SHAWN's CODE________________________Next







; ==========================================================================================================================; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================
Func _ObjectSHFolderViewFromWin($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,0)
    Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView    ; Shell Object
    $oShell=ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Return SetError(2,0,0)
;   Get a 'ShellWindows Collection' object
    $oShellWindows = $oShell.Windows()
    If Not IsObj($oShellWindows) Then Return SetError(3,0,0);   Iterate through the collection - each of type 'InternetExplorer' Object
    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            ; InternetExplorer->Document = ShellFolderView object
            $oSHFolderView=$oIEObject.Document
            If IsObj($oSHFolderView) Then Return $oSHFolderView
            Return SetError(4,0,0)
        EndIf
    Next    Return SetError(-1,0,0)
EndFunc
; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================Func _ExplorerWinGetSelectedItems($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
    Local $oSHFolderView
    Local $iSelectedItems,$iCounter=2,$aSelectedItems[2] = [0, ""]
    $oSHFolderView=_ObjectSHFolderViewFromWin($hWnd)
    If @error Then Return SetError(@error,0,'');   SelectedItems = FolderItems Collection object->Count
    $iSelectedItems = $oSHFolderView.SelectedItems.Count
    Dim $aSelectedItems[$iSelectedItems+2]  ; 2 extra -> 1 for count [0], 1 for Folder Path [1]    $aSelectedItems[0]=$iSelectedItems
;   ShellFolderView->Folder->Self as 'FolderItem'->Path
    $aSelectedItems[1]=$oSHFolderView.Folder.Self.Path
;   ShellFolderView->SelectedItems = FolderItems Collection object
    $oSelectedFolderItems = $oSHFolderView.SelectedItems#cs
    ; For ALL items in an Explorer window (not just the selected ones):
    $oSelectedFolderItems = $oSHFolderView.Folder.Items
    ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce
    For $oFolderItem In $oSelectedFolderItems
        $aSelectedItems[$iCounter] = $oFolderItem.Path
        $iCounter += 1
    Next    Return SetExtended($iCounter-2,$aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems
Func _ComErrFunc($oError)
    ConsoleWrite("COM Error occurred:"  & @CRLF & _
        "Number: " & @TAB & $oError.number & @CRLF & _
        "Windescription:" & @TAB & $oError.windescription & @CRLF & _
        "Description is: " & @TAB & $oError.description & @CRLF & _
        "Source is: " & @TAB & $oError.source & @CRLF & _
        "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc
Link to comment
Share on other sites

  • Moderators

sbrady,

Try this: ;)

; Full MIX or SOT MIX or M&E MIX

$sPath = "C:Documents and SettingssebradDesktopshawnAR123 Smith, Bob v1 FULL MIX.txt"
$sNewPath = StringRegExpReplace($sPath, "(?i)(.*)(sv.*)(s.*sMIX)(.txt)", "$1$3$2$4")
ConsoleWrite($sNewPath & @CRLF)

SRER explanation:

(?i)        - Case insensitive
(.*)        - Capture any number of characters until we find and...
(sv.*)     - Capture a space followed by a "v" followed by whatever there is until we find and...
(s.*sMIX) - Capture a space followed by any number of characters up to another space and "MIX"
(.txt)     - Capture ".txt"

$1$3$2$4    - Rewrite the captured groups in the required order

It works for me when I test it - how about for you? :)

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

Do you even test this stuff before posting it?

You have the start of your function inside a comment block which causes this script to be unusable as-is. Plus you forgot a couple of Next's in there as well.

This section of code was written wrong, it's not doing what you're thinking it's doing.

For $File_Name = 1 To $aSelection[0]; check if file name contains "Full MIX" or "SOT MIX" or "M&E MIX"
 $result = StringInStr($aSelection[$File_Name], "mix", 0, 1)
 If $result > 0 Then
  MsgBox(4096, "THE result", $File_Name);C:Documents and SettingssebradDesktopshawnAR123 Smith, Bob v1 FULL MIX.txt
  ; rename the variable $File_Name which is,   AR123 Smith, Bob v1 FULL MIX.txt    to this    AR123 Smith, Bob FULL MIX v1.txt
 EndIf
Next

Try it this way, might work for you.

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

Link to comment
Share on other sites

M23 thanks for the help, I am outta here for the weekend and am booked with other projects on Monday and Tuesday, I'll be back at it on Wed. thanks for the help. I'll see if I can figure out your code. I have made some progress on my end but will be asking for guidance when I post. M23 Can you put a stop to the bullying. Brewman......I have to block you from my posts, so might as well not even try, I wont even see your posts, you are just too mean.

Link to comment
Share on other sites

  • Moderators

sbrady,

M23 Can you put a stop to the bullying

I wish I could put a stop to your constant complaining when people try to help you. :mad:

What you got there is called "constructive criticism" and you even got a suggestion as to how you might fix the code. If you call that bullying, how on earth do you live in the real world? :huh:

Grow up or go away. :naughty:

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

thanks, it works, cool trick, where is this in the help file?

(?i) - Case insensitive (.*) - Capture any number of characters until we find and...

(sv.*) - Capture a space followed by a "v" followed by whatever there is until we find and...

(s.*sMIX) - Capture a space followed by any number of characters up to another space and "MIX"

(.txt) - Capture ".txt" $1$3$2$4 - Rewrite the captured groups in the required order

$1$3$2$4 - Rewrite the captured groups in the required order

Link to comment
Share on other sites

  • Moderators

sbrady,

where is this in the help file?

Perhaps if you looked in the Help file under the StringRegExpReplace function name? ;)

Although most of the details are under StringregExp itself - the 2 pages are linked. But be warned, SREs are possibly the hardest thing I have ever tried to learn in computing and I am still far from expert in their use. I always point people to www.regular-expressions.info/tutorial.html to learn more about them. Good luck if you decide to have a go - you will need it. :D

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

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...