Title: ClearCase label parent folders of a file
Tags: batch-file;clearcase
Question: With a given file path, how to label all parent folders until the VOB level?

for example, file path: \VOB1\dir1\subdir1\moredir1\file1.xml

The following elements I want to be labelled with LABEL1:

```\VOB1\dir1\subdir1\moredir1\file1.xml
\VOB1\dir1\subdir1\moredir1
\VOB1\dir1\subdir1
\VOB1\dir1
```

With mklabel command, easy to do:

```cleartool mklabel LABEL1 \VOB1\dir1\subdir1\moredir1\file1.xml \VOB1\dir1\subdir1\moredir1 \VOB1\dir1\subdir1 \VOB1\dir1
```

However, I want the paths to be calculated intelligently.

The parameter of mklabel -rec don't suit this purpose because the top parent folder may containt lots of other files/dirs.

Any ideas?


Here is the accepted answer: ```@echo off
setlocal EnableDelayedExpansion

set "filePath=\VOB1\dir1\subdir1\moredir1\file1.xml"
set "wantedParent=VOB1"

set "thisPath="
set "labelPaths="
set "labelThisPath="
if "%filePath:~0,1%" equ "\" set "filePath=%filePath:~1%"
for %%a in ("%filePath:\=" "%") do (
   set "thisPath=!thisPath!\%%~a"
   if defined labelThisPath (
      set "labelPaths=!thisPath! !labelPaths!"
   ) else if "%%~a" equ "%wantedParent%" (
      set "labelThisPath=true"
   )
)

ECHO cleartool mklabel %labelPaths%
```

Output:

```cleartool mklabel \VOB1\dir1\subdir1\moredir1\file1.xml \VOB1\dir1\subdir1\moredir1 \VOB1\dir1\subdir1 \VOB1\dir1
```


Here is another answer: Since there is no native way to get the list of parent folders (unless ```cleartool lsfolder -ancestor``` works), you would simply 'cd ..' until mklabel fails (which would mean you are outside of the vob)

```cleartool mklabel LABEL1 . || exit
cd ..
```

In bash:

```while true; do cleartool mklabel LABEL1 . || exit; cd ..; done
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/15/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<batch-file><clearcase>', 'Title': 'ClearCase label parent folders of a file', 'ViewCount': 149, 'id': 34809154, 'user_AboutMe': '', 'user_AccountId': 1782794.0, 'user_DisplayName': 'dellair', 'user_Reputation': 407.0, 'user_Views': 148.0, 'user_id': 1625076.0}}