I founded that by command:

wmic share where type=0 get name,path

I can list shares that I'm interested in. I cannot change their share settings or security permission but I want to back up them.

The point is - someone made it a little strange that inside one share there is another share (could be done on file permission but is not).

File system looks like:

- share1.1
- share1.2

In this case, wmic is going to list all shares separately and I will backup them more than once.

Any idea how to make it work to check only highest stage of file tree?


You can use PowerShell! Save the following script as a .ps1 file, then follow the Enabling Scripts section in the PowerShell tag wiki.

$sharepaths = gwmi -Class 'Win32_Share' | ? {-not $_.Name.EndsWith('$')} | % {$_.Path.ToUpperInvariant()} | sort
$lastroot = $null
ForEach ($path In $sharepaths) {
    If ($lastroot -ne $null -and $path.StartsWith($lastroot)) {Continue}
    $lastroot = $path
    # Run backup command here

It works by getting all Win32_Share WMI instances (like your batch file), filtering them down to the ones that don't end in a $ (which marks a hidden share), pulls out only the all-uppercase path, and sorts the list of paths. That sorting is important when we iterate over the list. If the current path starts with the last "root" share's path, we skip it, since it's already been backed up.

You need to replace the # comment line with your procedure that runs the backup. At that point, the $path variable contains the local path to the share. You can run non-PowerShell utilities like xcopy or robocopy in there too.

Alternatively, you can replace that line with just $path, which returns the current share's path to the script's caller. In that strategy, you would invoke the PowerShell script from batch, then do something with each line (i.e. each path) it returns, using a normal batch loop. For example, if you named the above script shareroots.ps1, you could run it like so:

powershell .\shareroots.ps1

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.