Page 1 of 1
Script to rename a folder and make it read only?
Posted: 05 Jul 2016 12:17
by Do_c
Hello,
I'm trying to make a script, but since I'm a newbie, I could really use some help. I want to do the following:
I have a folder structure with versions of a certain folder:
"random name R01"
"random name R02"
"random name Rxx"
I want to change the Rxx folder to R(latest number) and make the folder a read only. I also like to put a shortcut of this folder in the Rxx folder one level above, so a sibling folder of the parent folder. An existing shortcut of a previous version should be replaced.
I also need a script to make a new Rxx folder. This means making a copy of the R(latest number) folder, renaming it to Rxx and making it read and write again.
If anyone could help me with a start, that would be much appreciated!
Re: Script to rename a folder and make it read only?
Posted: 05 Jul 2016 16:01
by highend
"random name" is different for all (in your example: 3) folders?
And the destination name of the Rxx folder is really Rxx (without the random name part)?
Re: Script to rename a folder and make it read only?
Posted: 05 Jul 2016 17:57
by Do_c
Yes. This could be an example:
[img]
[img]https://s31.postimg.org/728rxnikn/example.png[/img][/img]
So basically a shortcut of "documents mechanics r02" should be put in "mechanics rxx". And when you use the script on "mechanics rxx" a shortcut of it (new name "mechanics r03") should be put in "rxx".
To avoid problems, it shouldn't be allowed to use the script on an Rxx folder when there is a Rxx folder a level lower/deeper in the folder structure.
Re: Script to rename a folder and make it read only?
Posted: 06 Jul 2016 16:13
by highend
Code: Select all
// Find *Rxx folder in current path
$curFolders = listfolder(, , 2, <crlf>);
$curRxxFolder = FindRxxFolder($curFolders);
if !($curRxxFolder) { status "No *Rxx folder found, aborted!", "8B4513", "stop"; end 1==1; }
// Find *Rxx folder one level deeper
if (FindRxxFolder(listfolder($curRxxFolder, , 2, <crlf>))) { status "A *Rxx subfolder was found, aborted!", "8B4513", "stop"; end 1==1; }
// Find *Rxx folder in parent path
$parentRxxFolder = FindRxxFolder(listfolder(regexmatches(<curpath>, "^.*(?=\\)"), , 2, <crlf>));
// Find the next ascending number
$matches = regexmatches($curFolders, "R\d+$", <crlf>, 1);
if !($matches) { status "No *R<number> folder found, aborted!", "8B4513", "stop"; end 1==1; }
$lastNumber = trim(gettoken($matches, -1, <crlf>), "R", "L");
$countNext = format(eval($lastNumber) + 1, strrepeat("0", strlen($lastNumber)));
// Rename Rxx + make it read only
$leadingFolderName = regexreplace(gpc($curRxxFolder, "base"), "^(.*?)(\sRxx)$", "$1");
$newRnumberName = "$leadingFolderName R$countNext";
renameitem($newRnumberName, "$curRxxFolder");
attrstamp("r", 1, "<curpath>\$newRnumberName");
// Create shortcut
$shortcut = "$parentRxxFolder\$newRnumberName";
if (exists("$shortcut.lnk") == 1) { delete 1, 0 , "$shortcut.lnk"; }
new($shortcut, "link", "<curpath>\$newRnumberName");
function FindRxxFolder($path) {
return formatlist($path, "f", <crlf>, "*Rxx", "f");
}
Re: Script to rename a folder and make it read only?
Posted: 06 Jul 2016 17:29
by Do_c
Thank you! Some of the features don't work like I intended to, but you gave me good start to work on. The issues I got with this script:
- The find Rxx folder one level deeper only looks in the folder itself. I would like it to search in the sibling folders as well. So in the example the Documents mechanics Rxx folder is in a sibling folder of Mechanics Rxx.
- The shortcut replaces shortcuts of the same name, but it doesn't replace shortcuts of previous versions (Mechanics R03.Ink doesn't replace Mechanics R02.Ink).
Re: Script to rename a folder and make it read only?
Posted: 06 Jul 2016 18:22
by highend
Simple changes...
Code: Select all
setting "BackgroundFileOps", 0;
// Find *Rxx folder in current path
$curFolders = listfolder(, , 2, <crlf>);
$curRxxFolder = FindRxxFolder($curFolders);
if !($curRxxFolder) { status "No *Rxx folder found, aborted!", "8B4513", "stop"; end 1==1; }
// Find *Rxx folder one level deeper
foreach($curFolder, $curFolders, <crlf>) {
if (FindRxxFolder(listfolder($curFolder, , 2, <crlf>))) { status "A *Rxx subfolder was found, aborted!", "8B4513", "stop"; end 1==1; }
}
// Find *Rxx folder in parent path
$parentRxxFolder = FindRxxFolder(listfolder(regexmatches(<curpath>, "^.*(?=\\)"), , 2, <crlf>));
// Find the next ascending number
$matches = regexmatches($curFolders, "R\d+$", <crlf>, 1);
if !($matches) { status "No *R<number> folder found, aborted!", "8B4513", "stop"; end 1==1; }
$lastNumber = trim(gettoken($matches, -1, <crlf>), "R", "L");
$countNext = format(eval($lastNumber) + 1, strrepeat("0", strlen($lastNumber)));
// Rename Rxx + make it read only
$leadingFolderName = regexreplace(gpc($curRxxFolder, "base"), "^(.*?)(\sRxx)$", "$1");
$newRnumberName = "$leadingFolderName R$countNext";
renameitem($newRnumberName, "$curRxxFolder");
attrstamp("r", 1, "<curpath>\$newRnumberName");
// Create shortcut
$shortcut = "$parentRxxFolder\$newRnumberName";
delete 1, 0, formatlist(listfolder($parentRxxFolder, , 1), "f", "|", "*.lnk", "f");
new($shortcut, "link", "<curpath>\$newRnumberName");
function FindRxxFolder($path) {
return formatlist($path, "f", <crlf>, "*Rxx", "f");
}
Re: Script to rename a folder and make it read only?
Posted: 07 Jul 2016 01:06
by Do_c
Thanks a lot! I'll try it out tomorrow.
I don't have any experience in scripting, so I'm trying to understand it. The create shortcut part, it seems to me that this will delete all shortcuts and not just the ones with a similar name (i.e. a previous version) or am I wrong?
I'm trying to learn using scripts on this forum, but it seems that this will take a long time. So basically I'm asking for help one more time:
- An addition to this script: I would like the user to add a comment to the new Rlatestnumber folder. Maybe by typing it in a pop up window?
- A simple script: Copy Rlatestnumber (with all its content), paste it in the same folder, make it Rxx, remove the read only attribute. This should not be allowed when there is no Rxx folder in the parent folder.
You would help me a lot!
Re: Script to rename a folder and make it read only?
Posted: 07 Jul 2016 04:50
by highend
or am I wrong?
No, you aren't
If you need to delete only those .lnk files that correspond to the leading part of the folder name instead of all:
Code: Select all
delete 1, 0, formatlist(listfolder($parentRxxFolder, "*.lnk", 1), "f", "|", "$leadingFolderName*", "f");
I would like the user to add a comment to the new Rlatestnumber folder. Maybe by typing it in a pop up window?
Script commands: input() and tag
- A simple script: Copy Rlatestnumber (with all its content), paste it in the same folder, make it Rxx, remove the read only attribute. This should not be allowed when there is no Rxx folder in the parent folder.
In addition what's already in the script,
Script command: copyitem
Re: Script to rename a folder and make it read only?
Posted: 11 Jul 2016 10:48
by Do_c
Thanks highend, you're a boss. I had to tweak the script a little bit in order for it to work when the Rxx folder is going to be the first version (R01). I did it probably in a really ugly way, but it seems to work.
Code: Select all
setting "BackgroundFileOps", 0;
// Find *Rxx folder in current path
$curFolders = listfolder(, , 2, <crlf>);
$curRxxFolder = FindRxxFolder($curFolders);
if !($curRxxFolder) { status "No *Rxx folder found, aborted!", "8B4513", "stop"; end 1==1; }
// Find *Rxx folder one level deeper
foreach($curFolder, $curFolders, <crlf>) {
if (FindRxxFolder(listfolder($curFolder, , 2, <crlf>))) { status "A *Rxx subfolder was found, aborted!", "8B4513", "stop"; end 1==1; }
}
// Find *Rxx folder in parent path
$parentRxxFolder = FindRxxFolder(listfolder(regexmatches(<curpath>, "^.*(?=\\)"), , 2, <crlf>));
// Find the next ascending number
$matches = regexmatches($curFolders, "R\d+$", <crlf>, 1);
$first = 0;
if !($matches) { $matches = "R00"; $first = 1 }
$lastNumber = trim(gettoken($matches, -1, <crlf>), "R", "L");
$countNext = format(eval($lastNumber) + 1, strrepeat("0", strlen($lastNumber)));
// Rename Rxx + make it read only
$leadingFolderName = regexreplace(gpc($curRxxFolder, "base"), "^(.*?)(\sRxx)$", "$1");
$newRnumberName = "$leadingFolderName R$countNext";
$a = input("Enter Comment", , , "s");
tag "$a", $curRxxFolder, 2;
renameitem($newRnumberName, "$curRxxFolder");
attrstamp("r", 1, "<curpath>\$newRnumberName");
// Create shortcut
if ($first) {
if ($parentRxxFolder) {$shortcut = "$parentRxxFolder\$newRnumberName";
new($shortcut, "link", "<curpath>\$newRnumberName");}
}
if !($first) {
if ($parentRxxFolder) {$shortcut = "$parentRxxFolder\$newRnumberName";
delete 1, 0, formatlist(listfolder($parentRxxFolder, "$leadingFolderName*", 1), "f", "|", "*.lnk", "f");
new($shortcut, "link", "<curpath>\$newRnumberName");}
}
function FindRxxFolder($path) {
return formatlist($path, "f", <crlf>, "*Rxx", "f");
}
Re: Script to rename a folder and make it read only?
Posted: 11 Jul 2016 10:58
by highend
You'd use a if / else construct normally. One check less if the if clause matches

Re: Script to rename a folder and make it read only?
Posted: 11 Jul 2016 12:32
by Do_c
Yeah that was lazy of me

.
Re: Script to rename a folder and make it read only?
Posted: 11 Jul 2016 13:16
by Do_c
So I made a second script to copy the latest folder and make it Rxx again:
Code: Select all
setting "BackgroundFileOps", 0;
// Find *Rxx folder in current path
$curFolders = listfolder(, , 2, <crlf>);
$curRxxFolder = FindRxxFolder($curFolders);
if ($curRxxFolder) { status "There already is a new release, aborted!", "8B4513", "stop"; end 1==1; }
// Find the next ascending number
$matches = regexmatches($curFolders, "R\d+$", <crlf>, 1);
$lastNumber = trim(gettoken($matches, -1, <crlf>), , "L");
$curLatestFolder = formatlist ($curFolders, "f", <crlf>, "*$lastNumber", "f");
$leadingFolderName = regexreplace(gpc($curLatestFolder, "base"), "^(.*?)(\s$lastNumber)$", "$1");
// Find *Rxx folder in parent path
if ($leadingFolderName != "Product") {
$parentRxxFolder = FindRxxFolder(listfolder(regexmatches(<curpath>, "^.*(?=\\)"), , 2, <crlf>));
if !($parentRxxFolder) { status "There is no new release in the parent folder, aborted!", "8B4513", "stop"; end 1==1; }
}
// Copy the latest version folder
copyitem "$curLatestFolder";
$newCurFolders = listfolder(, , 2, <crlf>);
$newFolder = formatlist ($newCurFolders, "f", <crlf>, "*$lastNumber-01", "f");
$newName = "$leadingFolderName Rxx";
renameitem ($newName, $newFolder);
attrstamp("r", 4, "<curpath>\$newName");
function FindRxxFolder($path) {
return formatlist($path, "f", <crlf>, "*Rxx", "f");
}
This works fine. However, after the script is finished I need to refresh my pane in order to see the newly created folder. Is there a way to refresh it in the script? I couldn't find it. Thank you in advance!
Re: Script to rename a folder and make it read only?
Posted: 11 Jul 2016 13:20
by highend
#485;
Menu - Help - List All Commands...