Sublime Forum

Find/Replace: how to add edited file parent dir in the replace string?

#1

In a “main directory” I have a lot of files, in a lot of subdirectories. Something like
C:\FirstDir\SecondDir\MainDir\MainSubDir\ <specific_subdirs> \*.edt

I need to find all the .edt files containing this line:
reference = someotherfile.ref
to replace it with the line:
reference = MainDir\RefSubDir\ <specific_subdirs> \someotherfile.ref

I was able to find&replace in a single directory, and then using this expressions:

find:    (reference = )(?!<this_dir>)(.*\.ref)
replace: \1MainDir\\RefSubDir\\<this_dir>\\\2

(where <this_dir> is the folder i’m searching in)
but it requires me to open one folder at time, copy/paste the folder name in the replace expression, replace all, and start again.

I have more than 150 folders to look.

There’s a way to put “automagically” the file parent directory in the replace string, so i can automate the whole process?
(or the full path, i can do more than one pass, as long the passes are less than the folders i have to do :smiley: )

Thanks

0 Likes

#2

Sublime can’t reference the path that a file comes from in the replacement text as far as I know, so for something like that you’d either need a package/plugin that adds that capability or to write a script externally that does it for you.

0 Likes

#3

you would be better off asking your question on stackoverflow, as this will require finding files, reading them, comparing string, replacing and rewriting the file…
on linux i would use find, grep, cat, | and > from a command line (terminal).

0 Likes

#4
0 Likes