Hiker86
Hiker86

Reputation: 76

regex help for replacing directories under certain conditions

I need to write a regex to update some registry files for work for an upcoming migration

I have to dynamically update all strings in files where:

  1. has a directory path that starts with C:
  2. directory does not have "xyz" or "abc" in the path
  3. if the string in the file contains a file [EX *.ext], the only thing that is updated is the directory path and not the filename & extension so those are left alone
  4. does not replace but instead keeps any appending information in the string [EX: " " " or ")"] that are used to close out the string parameters in the files

I have got it doing 1 and 2, but not 3 and 4. I'm using PowerShell 4.0, recursively checking files in a directory and producing a new file with the changes for each file found in each subdirectory.

The conversion should look like:

FROM: setting1="\"C:\\\\this\\\\is\\\\\adirectory\"" 

TO: setting1="\"O:\\\\\New_Directory_Path\""


FROM: othersetting="\"C:\\\\this\\\\abc\\\\directory"" 

TO: UNCHANGED


FROM: thisfile="\"(C:\\\\this\\\\directory\\\\somefile.some_ext)""

TO: thisfile="\"(O:\\\\New_Directory_Path\\\\somefile.some_ext)""


I'm at my wits end on this one, this is what I have so far, in terms of regex:

gc $file | % {$_ -replace "C:(?!.*abc.*)(?!.*xyz.*)(?!.*\..*).*","O:\\\\New_Directory_Path\"} | out-file "$filePath\$newFileName"

Hoping someone on here can lend a hand. Also, this is my first time posting on here, sorry for not putting my code in tags.

Upvotes: 0

Views: 28

Answers (1)

alpha bravo
alpha bravo

Reputation: 7948

try this pattern
Edited:

C:(?!.*(abc|xyz)).*?(?=[^\\]+\.[^\\]+|$)  

Demo

Upvotes: 1

Related Questions