Jump to content

StringRegExpReplace question


 Share

Recommended Posts

I have a folder structure with variable depth. For ex,

C:\folder1\folder2\folder3\test.txt

C:\folder1\testTwo.txt

C:\folder1\folder2\somefiletest.txt

 

I have a need to get rid of the *.txt file from this folder path. So for example, for above paths, I want to see following,

C:\folder1\folder2\folder3\

C:\folder1\

C:\folder1\folder2\

 

How do I write AutoIt script to do this? I am pretty new to AutoIt and not good with RegEx at all. Please help me out, thank you

Link to comment
Share on other sites

usustarr,

Is this what you are looking for?

#include <array.au3>

local $aFiles = [ _
    'C:\folder1\folder2\folder3\test.txt', _
    'C:\folder1\testTwo.txt', _
    'C:\folder1\folder2\somefiletest.txt']

_arraydisplay($aFiles)

for $i = 0 to ubound($aFiles) - 1
    $aFiles[$i] = StringRegExpReplace($aFiles[$i],'(.*\\).*','$1')
Next

_arraydisplay($aFiles)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

And the whole "don't use regex unless you have too" rule.

Here is a alternative.

#include <array.au3>

local $aFiles = [ _
    'C:\folder1\folder2\folder3\test.txt', _
    'C:\folder1\testTwo.txt', _
    'C:\folder1\folder2\somefiletest.txt']

_arraydisplay($aFiles)

for $i = 0 to ubound($aFiles) - 1
$iPos = StringInStr($aFiles[$i], "\", 0, -1)
$aFiles[$i] = StringLeft($aFiles[$i], $iPos)
Next

_arraydisplay($aFiles)

 

As for your list of files, I am unsure if you have them already, but I assume no.  So something like FileListToArrayRec() with a *.txt filter would work.  Then you would want to clean out the duplicates.

Edited by ViciousXUSMC
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

×
×
  • Create New...