Quantcast
Channel: Tech Support Guy
Viewing all articles
Browse latest Browse all 38405

batch file using NET SHARE - parsing issues

$
0
0
Hello.

I have been hammering away at a particular problem for awhile now, off and on. I am tired of windows 7 ridiculous network sharing scheme. Give me good old XP style, insecure and easy to use. lol.

Anyway, what I am doing is, via a context menu, calling a batch file, with two parameters. %1 is a command, such as "share", and %2 is the path (context menu is always passing an FQP, on a directory).

When the batch script gets the parameter to create a share, it will:
1. check the target exists
2. concatenate a target directory with underscores (ie. some path will become some_path)
3. prepend a user supplied value (ie. temp_ prepended like temp_some_path)
4. use net share to create the share with /grant:everyone,full (exactly what the OS does)
5. use icacls to grant everyone RX rights with OICI inheritance (exactly as the OS does)
(note this is all about simple file sharing for the home, no security required really behind the router)

The batch script will also reverse this process, but I originally built it to delete ALL of these shares, because the idea WAS to use it at LAN parties, so ALL the shares were really temporary anyway. So the batch file would:
1. use a for loop, look for any share name with the prepend tag (ie. temp_ )
2. ignore anything without the prepend tag implicitly
3. use net share with /del parameter
4. use icacls with /remove:g everyone

It works fine enough. However, I became fed up with the "new and improved" windows 7 sharing, tweaked a few things so that no credential prompts come up when sharing, at all, and now want to rid myself of the "share with" context menu and create my own. Not hard.

However, the hard part is being choosy about what to stop sharing.

The NET SHARE command will return a (I think) tab delimited output of share names to FQP. I want the option of simply sharing a directory with the same name as the directory, which might contain spaces (although I rarely use spaces, it happens when you get lazy ;) ). So my For loop will never know how many tokens to use, thus I can't know how many to expect.

Here is a snippet of the batch file
Code:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET _prepend=temp_
SET _alen=5

IF /I [%1]==[unshare] GOTO :xunshare
IF /I [%1]==[share] GOTO :xshare
IF /I [%1]==[add_rights] GOTO :xadd_rights
IF /I [%1]==[rem_rights] GOTO :xrem_rights
GOTO :bye

:xunshare
FOR /F "skip=4" %%I IN ('NET SHARE') DO (
        SET Share=%%I
        SET var=!Share:~0,%_alen%!
        IF !var!==!_append! NET SHARE !Share! /del
)
GOTO :bye

:xshare
IF /I [%2]==[] GOTO :bye
IF NOT EXIST %2 GOTO :bye
FOR /D %%I IN (%2) DO (
        SET Share=%%~nxI
        SET Share=!Share: =_!
        NET SHARE !_prepend!!Share!=%2 /GRANT:Everyone,Full
)
GOTO :bye

So, what I am trying to do is parse the output of NET SHARE, and return the SHARE NAME for each share listed. This I will then match against the %2 parameter which I will have to parse out to find the directory, and if they match can delete the share using net share. I could of course write this in a number of languages, or create a full blown app, but what fun is that lol?

So I need help on how I can parse the NET SHARE results out.

For example, suppose the NET SHARE output was something like this:
Code:

my test dir 2    c:\my test dir\test_dir\my test dir 2
some_dir                  c:\my test dir\some_dir

You can see my output will vary in structure. I understand a lot of the commands, but this one I am struggling with.

Is there any batch geeks who might like to try and help me with this?

Any help appreciated.

geri.

btw, been messing with permutations of this, trying to come up with something that works. Very messy and very verbose lol
Code:

@echo off
SetLocal EnableDelayedExpansion
for /f "skip=4 tokens=*" %%a in ('net share') DO SET M=!M!%%a:::

ECHO %M%


for /f "skip=4 tokens=2,3 delims= " %%a in ('net use ^| FIND /I "%1"') do (
        echo share b.. %%b
  Set SHARE=%%b
  echo cho shar.. !SHARE!
  Set SHARE=!SHARE:%1=%2!
  echo cho shar end.. !SHARE!

)
echo cho =================

For /F "skip=4 delims=<tab>" %%I IN ('net share') do (
        set m=!m!%%I_
        echo !m!
)

for /F "skip=4 tokens=* delims=*" %%I in ('net share') do (
        echo I is ________ %%I
        echo j is %%J
  REM ~ set share=%%i
  echo cho nxi .. %%~nxI
  echo cho nxj--....  %%~nxJ
  REM ~ echo cho XXX.. %%~dp$PATH:I
  REM ~ echo choZZZZ %%~ftzaI
  SET fat=%%I
  SET fat=!fat: =.!
  echo fat is now !fat!
  SET Var=%%~nxI
  echo cho var now.. !Var!
  REM ~ SET share=%%~nxI
  SET Var=!Var: =_!

  REM ~ SET var=!Share:~0,%_alen%!
  echo this is.. !Var!

)


Viewing all articles
Browse latest Browse all 38405

Trending Articles