Jump to content

replace different multiple paths with standard one


autog
 Share

Recommended Posts

Hi,

I need to replace full paths from a list of path-files, like:

D:\folder\file with spaces.ext

E:\folder\subfolder\another file.ext

\\networkdrive\folder\yet another file.ext

to become, e.g.

Z:\MyFolder\file with spaces.ext

Z:\MyFolder\another file.ext

Z:\MyFolder\yet another file.ext

Totally non-standard list, mapped or network drives, I have to rely on string manipulation.

I know a bit of cmd it can easily spot paths (%~pa - expands %a to a path only), but cmd looks "disabled" when it comes to manipulating/replacing strings.

As for an autoit algorithm, I was thinking:

- search extension, get its position

- search backwards for backslash (is it possible?, how?) and get its position on line $posslash

- [if string exists] replace string between $pos1=>$posslash with "Z:\MyFolder"

I would very much appreciate your help with some hints/example.

Link to comment
Share on other sites

  • Moderators

autog,

I wiuld extract the filename plus extension and then add the new path: :oops:

Global $aFiles[3] = ["D:folderfile with spaces.ext", " E:foldersubfolderanother file.ext", "networkdrivefolderyet another file.ext"]
$sBase_Path = "Z:MyFolder"

For $i = 0 To 2
    ; Extract filename with extension
    $sFileName = StringRegExpReplace($aFiles[$i], "^.*", "")
    ; Add to base path
    ConsoleWrite("New path = " & $sBase_Path & $sFileName & @CRLF)
Next

All clear? :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

That was fast :D, thanks for "extracting" idea, but I do need to replace because I need final list to keep the same line sequence.

My mistake, didn't explain completely, original list contains multiple lines I don't want to touch:

line01 random content

line02 random content

D:folderfile with spaces.ext

line04 random content

line05 random content

line06 random content

line07 random content

E:foldersubfolderanother file.ext

line09 random content

line10 random content

networkdrivefolderyet another file.ext

line12 random content

line13 random content

etc

Result needed is all lines intact, except paths changed to mine.

Hmm.. maybe to read each line, write it to new list and if found extension, do your extraction/add solution?

I'd find easier to grab paths in a list and loop-replace them until none found, but no idea how to do it.

In the meantime I'll also work on Melba23's idea.

Link to comment
Share on other sites

  • Moderators

autog,

My mistake, didn't explain completely

Always helps! :)

So test the line before you alter it: :rip:

Global $aLines[13] = ["line01 random content", _
                    " line02 random content", _
                    " D:folderfile with spaces.ext", _
                    " line04 random content", _
                    " line05 random content", _
                    " line06 random content", _
                    " line07 random content", _
                    " E:foldersubfolderanother file.ext", _
                    " line09 random content", _
                    " line10 random content", _
                    " networkdrivefolderyet another file.ext", _
                    " line12 random content", _
                    " line13 random content"]

$sBase_Path = "Z:MyFolder"

For $i = 0 To UBound($aLines) - 1

    If StringRegExp($aLines[$i], "(?i)((w:|).+.w{3})") Then ; See if it holds a path
        ; And change it if it does
        $sFileName = StringRegExpReplace($aLines[$i], "^.*", "")
        ConsoleWrite($sBase_Path & $sFileName & @CRLF)
    Else
        ; Or leave it alone if not
        ConsoleWrite($aLines[$i] & @CRLF)
    EndIf
Next

Any better? :D

M23

P.S. Welcome to the AutoIt forum, by the way! :oops:

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

  • Moderators

autog,

SRE explanations: :oops:

SRE: 
(?i)                       - Case insensitive  
((w:|).+.w{3})")  - This is what we look for - made up of
(w:|)               - Either a letter followed by : or (separated by a "|") a ||
.+                         - At least one character followed by
.w{3}                    - a . and 3 letters

SRER:
.*                       - Look for any number of characters followed by a backslash
""                         - Remove those characters

I leant a lot about SREs from this site and I still use it lot. :rip:

M23

Edit: Wrong explanation. :D

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