Get Share Location using a function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Get Share Location using a function

#1 Post by abc0502 » 16 Feb 2013 01:01

I'm making a batch file that will check for shared resources using the WMIC command in my PC and if there is any, display the share name and share location.

I check first for the share name and when i find one i pass it to the function :ShareLocation that use a for command to retrive the location and set it to a variable name, and the problem is in this function, it doesn't work :(

This is the code:

Code: Select all

@ECHO OFF

REM =====[ Shared Resources ]========================================
::
:: Check for any existence of a shared folders in the system.
:: We do nothing if shared resources was not found.
Echo.
Echo  [+] Scanning Shared Resources.
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE GET Name') DO (
   SET "ShareName=%%A"
   REM the following two if commands is for if sharname doesn't contain IPC$ in it and if wasn't empty
   IF "!ShareName:~0,3!" NEQ "IPC" (
      IF /I "!Sharename!" NEQ "" (
         Echo.
         Echo        Shared Resources :
         Echo          * Share-Name: !ShareName!
         Call :ShareLocation "!ShareName!" "ShareLocation"
         Echo          * Share-Location: !ShareLocation!
         
      )
   )
)
pause
exit /b

:ShareLocation <share_name> <variable_to_set_result_to>
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE where name^="%~1" GET Path') DO SET "%~2=%%A"
GOTO :EOF


I even tried not to set the result to a variable but just echo it instead it work fine if works alone without being in a for command
like this:

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion
Call :ShareLocation "a"

pause
exit
:ShareLocation
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE where name^="%~1" GET Path') DO echo %%A
GOTO :EOF
"a" is a shared folder

but in the previous for command "in the first code" it doesn't, any idea why?

Edit:
Also Tried This:

Code: Select all

@ECHO OFF

REM =====[ Shared Resources ]========================================
::
:: Check for any existance of a shared files/folders in the system.
:: We do nothing if shared resources was not found.
Echo.
Echo  [+] Scanning Shared Resources.
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE GET Name') DO (
   SET "ShareName=%%A"
   REM if the line doesn't contain IPC$ in it and if the line wasn't empty
   IF "!ShareName:~0,3!" NEQ "IPC" (
      IF /I "!Sharename!" NEQ "" (
         Echo.
         Echo        Shared Resources :
         Echo          * Share-Name: !ShareName!
         FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE where name^="!ShareName!" GET Path') DO Echo          * Share-Location: %%A
         
      )
   )
)
pause
exit /b

:ShareLocation
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE where name^="%~1" GET Path') DO <nul Set /p "=     %%A"&Echo.
GOTO :EOF

The out put is:

Code: Select all


 [+] Scanning Shared Resources.

       Shared Resources :
         * Share-Name: a
No Instance(s) Available.

       Shared Resources :
         * Share-Name: New Folder (2)
No Instance(s) Available.
Press any key to continue . . .



The Correct output should be:

Code: Select all


 [+] Scanning Shared Resources.

       Shared Resources :
         * Share-Name: a
         * Share-Loction: C:\Documents and Settings\admin\desktop\a

       Shared Resources :
         * Share-Name: New Folder (2)
         * Share-Loction: C:\Documents and Settings\admin\desktop\New Folder (2)
Press any key to continue . . .

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get Share Location using a function

#2 Post by foxidrive » 16 Feb 2013 06:16

I haven't looked closely yet, but WMIC produces UNICODE... try this
The bit in colour is the magic.

@echo off
SETLOCAL EnableDelayedExpansion
Call :ShareLocation "a"

pause
exit
:ShareLocation
SETLOCAL EnableDelayedExpansion
FOR /F %%A IN ('WMIC SHARE where name^="%~1" GET Path ^|find "\" ') DO set "var=%%A"
echo %var%

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Get Share Location using a function

#3 Post by abc0502 » 16 Feb 2013 07:08

It does work, but the same problem happen when using this function in the first code i posted, this is the output i get when i do that.

Code: Select all


 [+] Scanning Shared Resources.

       Shared Resources :
         * Share-Name: asgfa
No Instance(s) Available.
ECHO is off.

       Shared Resources :
         * Share-Name: a
No Instance(s) Available.
ECHO is off.

       Shared Resources :
         * Share-Name: sss
No Instance(s) Available.
ECHO is off.

       Shared Resources :
         * Share-Name: New Folder (2)
No Instance(s) Available.
ECHO is off.
Press any key to continue . . .
i added two more test shared folders and for every one i get "No Instance(s) Available."
It seems that the problem is in the 1st for command that check for the share names and pass the share names to the function.

I used a for command to read the shared folders names from a file and used old function in first post and it worked fine.

i guess i will use a temporary file to store folders names then use another for command to get the location.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get Share Location using a function

#4 Post by foxidrive » 16 Feb 2013 09:47

This works around it. There are two issues - WMIC creates double-byte (Unicode) text and when converted it includes trailing spaces.
This workaround of using a CSV format output causes WMIC to stop adding trailing spaces and the extra for /f in do command converts to ascii text without the double upped CR.

Code: Select all

@ECHO OFF

REM =====[ Shared Resources ]========================================
::
:: Check for any existence of a shared folders in the system.
:: We do nothing if shared resources was not found.
Echo.
Echo  [+] Scanning Shared Resources.
SETLOCAL EnableDelayedExpansion
FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC SHARE GET Name /format:csv') DO (
for /f "delims=" %%Z in ("%%B") do SET "ShareName=%%Z"
   REM the following two if commands is for if sharename doesn't contain IPC$ in it and if wasn't empty
   IF "!ShareName:~0,3!" NEQ "IPC" (
      IF /I "!Sharename!" NEQ "" (
         Echo.
         Echo        Shared Resources :
         Echo          * Share-Name: !ShareName!
         Call :ShareLocation "!ShareName!" "ShareLocation"
         Echo          * Share-Location: "!ShareLocation!"
pause         
      )
   )
)
pause
exit /b

:ShareLocation <share_name> <variable_to_set_result_to>
FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC SHARE where name^="%~1" GET Path /format:csv') DO for /f "delims=" %%Z in ("%%B") do SET "%~2=%%Z"
GOTO :EOF

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Get Share Location using a function

#5 Post by abc0502 » 16 Feb 2013 23:36

@Foxidrive, That is amazing :D
I spent hours trying to figure it out but nothing, really that is amazing.

WMIC creates double-byte (Unicode) text and when converted it includes trailing spaces.

I have seen that before in one of my tests it add spaces at the end, i was trying to save the folders names in a variable with a space as a separator then read that variable again using a for command.

The previous functions from yours and mine work fine when reading from a file or from a text (variable store folders name).

Code: Select all

@ECHO OFF

SETLOCAL EnableDelayedExpansion
Set "S="
FOR /F "skip=1 tokens=* delims=" %%A IN ('WMIC SHARE GET Name') DO (
   SET "S=!S! "%%A""
)
For %%A in (%S%) do (
   echo %%A
   )
pause
exit /b
if you test it you will see the spaces at the end of each shared folder.

Thanks again :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get Share Location using a function

#6 Post by foxidrive » 17 Feb 2013 00:25

Yes, I see all the padded strings.

For those that want to see the WMIC output view the file.txt from this in a hex editor and you can see the double CR and how the top part is converted to the lower part.
The extra for in do is Dave's idea from a previous thread.

Code: Select all

@ECHO OFF
del file.txt 2>nul
FOR /F "delims=" %%A IN ('WMIC SHARE GET Name /format:csv') DO (
>> file.txt echo.%%A
)

>> file.txt echo.=======================

FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC SHARE GET Name /format:csv') DO (
for /f "delims=" %%Z in ("%%B") do >> file.txt echo.%%Z
)

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Get Share Location using a function

#7 Post by abc0502 » 17 Feb 2013 02:02

Thanks,
Then this could be a rule, always use the csv format when using the WMIC command outputs.

Post Reply