# \_DIRECTORY regular expression

**URL:** https://community.mp3tag.de/t/directory-regular-expression/6916
**Category:** Support
**Created:** [June 7, 2008, 7:14am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916 "2008-06-07T07:14:28Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 7, 2008, 7:14am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/1 "2008-06-07T07:14:28Z")

</div>

Can't work this one out. I'm trying to remove leading "The " from every folder in the path.

So, I have e:\<u\>The Beatles\The Beatles [Disc 1]\01-back in the ussr.m4a etc

If I right-click the top folder (underlined above), enter mp3tag, and run my action...

Replace with regular expression  
Field: \_DIRECTORY  
Regular expression: ^The\s+  
Replace matches with: (i.e. nothing)

...I end up with...  
e:\The Beatles\Beatles [Disc 1]\01-back in the ussr.m4a

...but I want...  
e:\Beatles\Beatles [Disc 1]\01-back in the ussr.m4a

...am I missing something? Thanks

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [June 7, 2008, 1:27pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/2 "2008-06-07T13:27:15Z")

</div>

\_DIRECTORY only refers to the directory where the audio files are in.

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 7, 2008, 2:39pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/3 "2008-06-07T14:39:35Z")

</div>

Ah, I see. Is there something I can use instead that will produce the effect I'm after? If not, can I file a request?! Thanks!

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 10, 2008, 9:09am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/4 "2008-06-10T09:09:05Z")

</div>

Answering your own post - the first sign of madness? 😃

I don't think I can do what I need using mp3tag (ie run a replace with reg ex on all folders in the path, not just the one that contains the audio files) so I found this freeware utility which does batch file/folder renaming and can use regular expressions. Thought I'd post a link in case anyone has a similar query in future....

[http://hp.vector.co.jp/authors/VA014830/english/FlexRena/](http://hp.vector.co.jp/authors/VA014830/english/FlexRena/)

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [June 10, 2008, 10:52am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/5 "2008-06-10T10:52:55Z")

</div>

> [@moley6knipe](#):
>
> ... I don't think I can do what I need using mp3tag (ie run a replace with reg ex on all folders in the path, not just the one that contains the audio files) ...

Converter/Tag-Filename  
Formatstring: **$regexp(%\_folderpath%,The\s,)%\_filename%**

DD.20080610.1452.CEST

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 9:18am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/6 "2008-06-11T09:18:01Z")

</div>

Thanks, but that removes every instance of "The", whereas I only want to remove leading "The " from the beginning of a folder name. I tried $regexp(%\_folderpath%, **^** The\s,)%\_filename% but that doesn't work. Plus, I'd like to be able to run this as part of my actions, and I can't work out how to do a tag\>filename as a user-defined action?

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 9:32am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/7 "2008-06-11T09:32:00Z")

</div>

Ah, I _can_ create an action, sorry...

Format Value  
Field: \_FILENAME  
Format string: $regexp(%\_folderpath%,The\s,)%\_filename%

...but I can't work out how to tell it to only remove "The" when it occurs at the beginning of a folder. Can anyone point me to this? Normally ^ is used for beginning of line, isn't it?

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [June 11, 2008, 10:09am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/8 "2008-06-11T10:09:44Z")

</div>

> [@moley6knipe](#):
>
> ... but I can't work out how to tell it to only remove "The" when it occurs at the beginning of a folder. Can anyone point me to this? ...

$regexp($regexp($regexp(%\_folderpath%,\\,\n),^The\s,),\n,)%\_filename%

DD.20080611.1409.CEST

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 11:14am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/9 "2008-06-11T11:14:30Z")

</div>

Ah, thanks _so_ much! I'd never have got that in a million years! I'm off to read up on regular expressions some more!! Thanks again!

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 12:07pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/10 "2008-06-11T12:07:36Z")

</div>

Ah, I spoke too soon. This regex works, but creates a new folder structure with leading The removed. I'd like to keep the folders and files where they are, just remove the leading The.

According to C:/Program%20Files/Mp3tag/help/options\_converter.html#ttf "You can mark directories via the backslash within the format string. Please note that Mp3tag won't rename directories but will create new directories below the current working directory if you use backslashes within the new filename pattern." - which as I read it means I'm out of luck, I think?

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [June 11, 2008, 12:38pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/11 "2008-06-11T12:38:34Z")

</div>

> [@moley6knipe](#):
>
> Ah, thanks _so_ much! I'd never have got that in a million years! ...

There is a simple short version too:  
$regexp(%\_folderpath%,\\The\s,)%\_filename%

DD.20080611.1630.CEST

> [@moley6knipe](#):
>
> ... I'm out of luck, I think?

Hmm, I don't think so ... the only thing that will make some trouble is ... how many other non music files are left in the old folder structure, which you have to move manually into the new (renamed) folders?

The forum contains an interesting thread about some experimental method to move non music files by Mp3tag:  
[Cover mit verschieben](https://community.mp3tag.de/t/6024/1)

DD.20080611.1638.CEST

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 2:20pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/12 "2008-06-11T14:20:27Z")

</div>

All files in all folders are music only - my problem is, I don't want to create a new set of folders with this regular expression, I want to rename the existing ones _without_ creating new folders and/or moving files.

Doing it this way means I'm left with the old, empty, folders which I need to delete manually, which defeats the point of trying to create this action to automate things!

I _think_ what I need is to be able to create...

Action: Replace with regular expression  
Field: \_FOLDERPATH  
Regular expression: ^The\s  
Replace matches with: (i.e. nothing)

...and of course my problem is that \_FOLDERPATH is not an available Field!

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [June 11, 2008, 4:20pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/13 "2008-06-11T16:20:11Z")

</div>

> [@moley6knipe](#):
>
> ... Doing it this way means I'm left with the old, empty, folders which I need to delete manually ...

This small hta script removes empty folders.  
[pcwEmptyFolder.zip](https://community.mp3tag.de/uploads/default/original/2X/3/3c56ed5be1b40f3ec8aad84a9c0347c05d4cf5af.zip) (2.52 KB)

> [@moley6knipe](#):
>
> ... I want to rename the existing ones without creating new folders and/or moving files. ...

So you think Mp3tag should have find some smart way to detect if a folder in a (renamed restructured) folderpath does exist and then rename it, and if not, create new folder and hook it into the foldertree and move all files from old folder to new folder and remove old folder if empty?  
Sounds rather heavy to me.

DD.20080611.2030.CEST

[pcwEmptyFolder.zip](https://community.mp3tag.de/uploads/default/original/2X/3/3c56ed5be1b40f3ec8aad84a9c0347c05d4cf5af.zip) (2.52 KB)

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 11, 2008, 8:20pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/14 "2008-06-11T20:20:48Z")

</div>

Well, no!

This...

Action: Replace with regular expression  
Field: \_DIRECTORY  
Regular expression: ^The\s  
Replace matches with: (i.e. nothing)

...simply renames the folder containing the music files. If I right-clicked on a folder called E:\music\the beatles\the beatles [disc 1] to launch mp3tag it's reasonable for an action to be able to affect all folders in that specific path. Doesn't need any logic, really, it's just a rename.

I'm not talking about file moving, simply a rename of an existing folder, in the path that the user would enter.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [June 12, 2008, 3:18am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/15 "2008-06-12T03:18:41Z")

</div>

> [@moley6knipe](#):
>
> ... I'm not talking about file moving, simply a rename of an existing folder, in the path that the user would enter.

How to address one folder in the folderpath from the view of the file?  
For now Mp3tag provides two system meta tag fields:  
%\_parent\_directory%  
%\_directory%

I could think of something like this to read folder names:  
$folder(-n)  
...  
$folder(-2)  
$folder(-1) (same as %\_parent\_directory%)  
$folder(0) (same as %\_directory%)

Or simply use positive numbers:  
0 the folder where the music file resides  
1 one folder up  
2 two folders up  
and so on.

Adding an optional parameter can rename a folder name:  
$folder(n,'new name')

DD.20080612.0717.CEST

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 12, 2008, 8:46am UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/16 "2008-06-12T08:46:50Z")

</div>

For completeness, here's my workaround. So, I run this action...

Format Value  
Field: \_FILENAME  
Format string: $regexp(%\_folderpath%,\\The\s,)%\_filename%

...which creates new folder structure without leading "The ", leaving the old empty folders behind.

I've created a .bat file in C:\Program Files and set it to run as a scheduled task overnight, just after the one that wakes the PC to do my overnight housekeeping (Windoze updates etc). .bat file contains....

_@echo off_

CD /D C:\documents and settings\username\my music\itunes music\blah blah  
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"\>NUL

CD /D C:\documents and settings\username\my music\itunes music\blah blah  
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"\>NUL

CD /D C:\documents and settings\username\my music\itunes music\blah blah  
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"\>NUL

...out of paranoia I've set three lines in this batch file to target the three specific folders that contain my music - this batch would work higher-up the folder chain and do it all in one go (it won't delete folders with anything in them, natch) but I'd rather avoid touching the ludicriously complex set of folders that iTunes uses for Album Artwork, just in case. Not fatal if they're deleted, but an iTunes artwork scan takes a while on my library!

Another advantage of doing this as a .bat file is that I can set it to run as a Tool in mp3tag, so I can double-click or right-click my music files and run it.

Code came from here... [http://blogs.msdn.com/oldnewthing/archive/...17/8399914.aspx](http://blogs.msdn.com/oldnewthing/archive/2008/04/17/8399914.aspx)

@DetlevD - huge thanks for the regex code!

---

<div class="post-metadata">

### Author: ![aladoro](https://community.mp3tag.de/user_avatar/community.mp3tag.de/aladoro/32/64_2.png) [@aladoro](https://community.mp3tag.de/u/aladoro)
#### Post date: [June 13, 2008, 12:08pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/17 "2008-06-13T12:08:44Z")

</div>

> For completeness, here's my workaround  
> for completeness I'd recommend to have a look at a renamer tool like [ReNamer](http://www.den4b.com/downloads.php?project=ReNamer) ...

HTH, andreas

---

<div class="post-metadata">

### Author: ![moley6knipe](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@moley6knipe](https://community.mp3tag.de/u/moley6knipe)
#### Post date: [June 13, 2008, 8:54pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/18 "2008-06-13T20:54:38Z")

</div>

Thanks Andreas - I like my batch file idea, means I can run it from mp3tag. But that renamer will certainly come in useful, thanks!

---

<div class="post-metadata">

### Author: ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)
#### Post date: [February 9, 2026, 12:20pm UTC](https://community.mp3tag.de/t/directory-regular-expression/6916/19 "2026-02-09T12:20:59Z")

</div>


