Share/Unshare folder

Discuss and share scripts and script files...

Share/Unshare folder

Postby binocular222 » 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 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";
Known 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?
Last edited by binocular222 on 06 May 2013 14:15, edited 14 times in total.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=9243#p82488
binocular222
 
Posts: 265
Joined: 04 Nov 2008 06:35
Location: Hanoi, Vietnam

Re: Share/Unshare folder

Postby Enternal » 02 May 2013 20:45

Oh a nice script! Never thought of this so thank you!

Hehe nircmd is really nice though isn't it? Using it together with XYplorer, I can use XYplorer to open my CD/DVD drive! Yeah, don't ask why would I do such a thing... Heck, I actually even have it scripted in a way that I can use "XYplorer" to speak to me text for fun. :biggrin:
Enternal
 
Posts: 201
Joined: 10 Jan 2012 19:26

Re: Share/Unshare folder

Postby binocular222 » 04 May 2013 04:48

Some updates added with much more functionality.
There're still some not-so-nice-workaround. Anyone can help?

@Don: maybe a bug:
When I selelect local drice (i.e: D:\), <curbase> return blank
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=9243#p82488
binocular222
 
Posts: 265
Joined: 04 Nov 2008 06:35
Location: Hanoi, Vietnam

Re: Share/Unshare folder

Postby admin » 04 May 2013 09:29

binocular222 wrote:@Don: maybe a bug:
When I selelect local drice (i.e: D:\), <curbase> return blank

No bug. Help:
Code: Select all
<curbase>    base-name of the current list item
admin
Site Admin
 
Posts: 26319
Joined: 22 May 2004 16:48
Location: Cologne

Re: Share/Unshare folder

Postby highend » 04 May 2013 09:50

Use getpathcomponent() instead.

Regarding your request for help:
Where exactly do you need help?

All I can say: You code is hard to read (use more indentation) and
you shouldn't repeat blocks of code so often. Use hidden subroutines instead.
highend
 
Posts: 1039
Joined: 06 Feb 2011 01:33

Re: Share/Unshare folder

Postby binocular222 » 04 May 2013 17:09

@Don: I think <curbase> of D:\ should be D instead of blank
@Highend: Updated the code as your recommendation. Thank you.
I need help to solve some limitations (see first post).
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=9243#p82488
binocular222
 
Posts: 265
Joined: 04 Nov 2008 06:35
Location: Hanoi, Vietnam


Return to Script Exchange



Who is online

Users browsing this forum: No registered users and 2 guests