Share/Unshare folder
Posted: 02 May 2013 20:20
[Initial Release]:
+ Toggle share/unshare status of selected folder
[Update 1.1]:
+ Process folders only, do nothing on files
+ No longer rely on Nircmd
[Update 1.2]:
+ If <curitem> is not folder, then list all shared folders on this computer. User can select which folder to unshare
This list shows full path and permission (Windows and XYplorer have never show these important info before)
!Bug fix: Have separate treatment for fixed drives
[Update 1.3]: !Code cleanup and bug fix
[Update 1.4]:
!Code cleanup and bug fix
+Easily switch between using Nircmd and Windows' CMD (just un-comment the last line)
[Version 2]Major change!
+ Compare <curitem> with net share result in very accurate detection
+ Run net share only once, thus improve speed a lot
+ Fool-proof in many cases, but report me if you found any exceptionKnown limitations:
-Rely on Nircmd.exe to execute cmd command. Fixed
- Have to export to a text file in temp folder in order to obtain info from cmd window.
-Check <curbase> against names in \\%ComputerName% is not reliable because shared name may differ from folder name. Fixed
- Net share offers many more variable; most notablely, /Grant:Everyone which can be other user groups but I haven't figure out how to retrieve all user group.
All comments to improve this script is wellcome.
---------------
P.S: Refresh tree is very slow on my computer because I mapped some network drive which point to company's network. Is there any way to disable refresh drive?
+ Toggle share/unshare status of selected folder
[Update 1.1]:
+ Process folders only, do nothing on files
+ No longer rely on Nircmd
[Update 1.2]:
+ If <curitem> is not folder, then list all shared folders on this computer. User can select which folder to unshare
This list shows full path and permission (Windows and XYplorer have never show these important info before)
!Bug fix: Have separate treatment for fixed drives
[Update 1.3]: !Code cleanup and bug fix
[Update 1.4]:
!Code cleanup and bug fix
+Easily switch between using Nircmd and Windows' CMD (just un-comment the last line)
[Version 2]Major change!
+ Compare <curitem> with net share result in very accurate detection
+ Run net share only once, thus improve speed a lot
+ Fool-proof in many cases, but report me if you found any exception
Code: Select all
//Declare variables
perm $Command;
$waittime = 170; //How long does it take to run net share CMD command, depend on each computer
$BaseName = getpathcomponent("<curitem>","base");
$ItemType = property("Type","<curitem>");
//Step 1: Obtain list of all shared resources on this machine
if(exists("%temp%\Netshare.txt") ==0) {new("%temp%\Netshare.txt")};
$Command = "Net share > ""%temp%\Netshare.txt"""; sub "_RunCMD"; wait $waittime;
$AllShare = readfile("%temp%\Netshare.txt");
$Count = gettoken($AllShare,"count","<crlf>");
$Index = 5;
$VisibleShare=;
$HiddenShare=;
While($Index < $Count) {
$CurItem = gettoken($AllShare,$Index,"<crlf>",t);
$PathDisk = strpos($CurItem,":\");
$PathNetwork = strpos($CurItem,"\\");
if($PathDisk != -1 || $PathNetwork != -1) {
$Name = trim(substr($CurItem,0,$PathDisk+$PathNetwork-1));
$PathAndComment = trim(substr($CurItem,$PathDisk+$PathNetwork));
$Separator = strpos($PathAndComment," ");
if($Separator == -1) {$Path = $PathAndComment; $Comment = };
else {$Path = trim(substr($PathAndComment,0,$Separator));
$Comment = trim(substr($PathAndComment,$Separator))};
if( substr($Name,0,3) == substr($Path,0,3)) {$Name = $PrevItem}; //Windows' CMD generate excessive linebreak if $Name is too long, I made a rudiment workaround aghhhhh
if(substr($Name,-1,1) == "$") {$HiddenShare = $HiddenShare<crlf>$Name|$Path|$Comment};
else {$VisibleShare = $VisibleShare<crlf>$Name|$Path|$Comment};
}
$PrevItem = $CurItem;
incr $Index;}
$VisibleShare = trim($VisibleShare,"<crlf>"); //<crlf> separated list of shared resources, each line contain $Name|$Path|$Comment
$HiddenShare = trim($HiddenShare,"<crlf>"); //<crlf> separated list of shared resources, each line contain $Name|$Path|$Comment
//Step 2: If current item is Folder/Drive => check <curpath> against the list above. If exist, then unshare, else share.
if($ItemType == "File folder" || strpos($ItemType,"Disk") >-1 || strpos($ItemType,"Drive") >-1) {
$Count = gettoken($VisibleShare,"count","<crlf>");
$Index = 1;
While ($Index <= $Count) {
$CurLine = gettoken($VisibleShare,$Index,"<crlf>");
$Path = gettoken($CurLine,2,"|");
$Name = gettoken($CurLine,1,"|");
if(<curitem> == $Path) {
msg Unshare this folder?,1;
$Command = "Net share ""$Name"" /delete"; sub "_RunCMD"; wait $waittime;
#482; #486; //Refresh Tree & list
end 1};
incr $Index};
$ShareName = replacelist(input(Share as this name:,,$BaseName,s,,600,400),'|\/,?":');
$Permission2 = inputselect("Read/write permission", "Full|Change|Read","|",4,,600,400,"Set permission");
If( $Permission2 == "") {$Permission2 = Full};
If( strlen("<curitem>") == 3) {$Command = "Net share ""$ShareName""=<curitem> /Grant:Everyone,$Permission2"; sub "_RunCMD"}; wait $waittime;
Else {$Command = "Net share ""$ShareName""=""<curitem>"" /Grant:Everyone,$Permission2"; sub "_RunCMD"}; wait $waittime;
#482; #486}; //Refresh Tree & list
// If <curitem> is not folder/drive: Display list all shared folders on this machine
Else {
$FullList = replace("$VisibleShare<crlf>$HiddenShare","|","<tab>|<tab>");
$ToDel = inputselect("Shared name<tab>|<tab>Full path<tab>|<tab>Permission", "$FullList", "<crlf>",2,,600,400, "Select which item to unshare");
$Count = gettoken($ToDel,"count","<crlf>");
if( $Count ==1) {
$ItemToDel = trim(gettoken($ToDel,1,"|"),"<tab>");
$Command = "Net share ""$ItemToDel"" /delete"; sub "_RunCMD"; wait $waittime;
#482; #486; //Refresh Tree & list
Else {
While ($Index <= $Count) {
$CurItem = gettoken($ToDel,$Index,"<crlf>");
$ItemToDel = trim(gettoken($CurItem,1,"|"),"<tab>");
$Command = "Net share ""$ItemToDel"" /delete"; sub "_RunCMD"; wait $waittime;
incr $Index;}
#482; #486; //Refresh Tree & list
}}}
"_RunCMD"
// run """<xyscripts>\nircmd\nircmd.exe"" execmd $Command";
run "cmd /c $Command";
-
- Have to export to a text file in temp folder in order to obtain info from cmd window.
-
- Net share offers many more variable; most notablely, /Grant:Everyone which can be other user groups but I haven't figure out how to retrieve all user group.
All comments to improve this script is wellcome.
---------------
P.S: Refresh tree is very slow on my computer because I mapped some network drive which point to company's network. Is there any way to disable refresh drive?