# Renaming tag folders via script

**URL:** <https://forum.inductiveautomation.com/t/renaming-tag-folders-via-script/22877>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [February 26, 2019, 2:36pm UTC](https://forum.inductiveautomation.com/t/renaming-tag-folders-via-script/22877 "2019-02-26T14:36:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marioquark](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@marioquark](https://forum.inductiveautomation.com/u/marioquark)\
**Post date:** [February 26, 2019, 2:36pm UTC](https://forum.inductiveautomation.com/t/renaming-tag-folders-via-script/22877/1 "2019-02-26T14:36:45Z")

</div>

Any chance to rename a tag folder via scripting?  
A specific function is not documented, so I’m analyzing a creative solution inspired by  
`system.tag.editTag(tagPath="MyTag", attributes={"Name": "MyNewTag"})`

I’m trying with  
`system.tag.editTag(tagPath="MyTag", attributes={"fullPath": "MyNewPath"})`  
or  
`attributes={"Path": "MyNewPath"})`  
or  
`attributes={"path": "MyNewPath"})`  
with no luck.  
Creating a new tag and removing the old one is not a good way, since I loose the tag history.

Any advice?  
Thank you

---

<div class="post-metadata">

**Author:** ![pthatcher](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pthatcher](https://forum.inductiveautomation.com/u/pthatcher)\
**Post date:** [December 23, 2019, 9:57pm UTC](https://forum.inductiveautomation.com/t/renaming-tag-folders-via-script/22877/2 "2019-12-23T21:57:32Z")

</div>

Only thing I’ve found so far is your first solution

```auto
system.tag.editTag(tagPath="MyTag", attributes={"Name": "MyNewTag"})

```

and following that up with an update query. Example python script can be placed in Script Library and called after system.tag.editTag

```auto
def RenameTag(tag):
		#tag = {OldPath: 'Path/To/Tag',NewTagName: 'NewTag'}
		# renames 'Path/To/Tag' to 'Path/To/NewTag'
		err = None
		if not tag.get('OldPath'):
			err = 'OldPath not defined'
		elif not system.tag.exists(tag['OldPath']):
			err = 'OldPath Tag not found: ' + tag.get('OldPath')
		
		NewPath = '/'.join(tag['OldPath'].split('/')[:-1] + [tag['NewTagName']])
		
		if system.tag.exists(tag.get('NewPath')):
			err = 'Tag already exists: ' + tag.get('NewPath')
		if err:
			raise ValueError({'message':err})
		
		system.tag.editTag(tagPath=tag['OldPath'], attributes={'Name':tag['NewTagName']})
		system.db.runNamedQuery('api','RenameTagHistory',{'OldPath':tag['OldPath'], 'NewPath':NewPath})

```

and the NamedQuery RenameTagHistory (in the ‘api’ project):  
Parameters:  
QueryString | OldPath | String  
QueryString | NewPath | String  
Query:

```auto
update [dbo].[sqlth_te] 
SET tagpath = REPLACE(tagpath,
					  LOWER('{OldPath}'),
					  LOWER('{NewPath}')
			  ) 
WHERE tagpath LIKE '{OldPath}%'

```

If you need to run this in the gateway scope, tag provider will need to be prepended to OldPath, and stripped back out before calling the runNamedQuery (sqlth\_te table doesnt utilize tag provider in tagpath)

---

<div class="post-metadata">

**Author:** ![pthatcher](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pthatcher](https://forum.inductiveautomation.com/u/pthatcher)\
**Post date:** [December 23, 2019, 10:07pm UTC](https://forum.inductiveautomation.com/t/renaming-tag-folders-via-script/22877/3 "2019-12-23T22:07:54Z")

</div>

I have noticed issues with this method though, using attributes: {Name} to rename a folder or tag has odd results. Sometimes it renames the tag, sometimes it copies it, sometimes it renames it and everything underneath it disappears. I wouldn’t recommend it.
