1

I have a classroom network where all of the machines have a shared folder following the same pattern. Each share can be accessed as:

\\…\foo\bar\folder

Here, the names foo, bar and folder are fixed, and only the computer name is different.

Eventually I plan to copy files to all of the folders above.

The question is: how do I loop through all of the share names above? In a command shell, I tried:

for %i in (\\*\foo\bar\folder) do echo %i

but that’s not working for me.

Thanks

Manngo
  • 14,066
  • 10
  • 88
  • 110

2 Answers2

1
    for /f "skip=3" %A in ('net view ^| findstr /v /C:"The command completed successfully"') do Echo %A

Lists all computers turned on. You put %A into your copy command instead of computername, eg

for /f "skip=3" %A in ('net view ^| findstr /v /C:"The command completed successfully"') do dir %A\C$

In a batch file use %%A instead of %A

  • Good Grief! Not my first guess. I’ll try it out tomorrow. Um, what exactly is `/f`, `skip=3`, `net vew ^` and `findstr /v`, if I have isolated the components correctly? – Manngo Feb 25 '16 at 10:37
  • Type `for /?`, `net /?`, and `net help View` (or for short help `net view /?`. `^` escapes the pipe character so it is interpreted as part of the command in the brackets (rather than 1/2 way through the whole command). See my list here of command prompt punctuation http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch. –  Feb 25 '16 at 11:03
  • So type the command from inner to outer parts, firstly `net view`, then `net view | findstr /v /C:"The command completed successfully"`. Then read about `for /f`. –  Feb 25 '16 at 11:05
  • OK, got it. I have managed to simplify my own requirement to something like: `for /f %%A in ('net view ^| findstr "\\"') do copy test.txt %%A\foo\bar\folder\`. Thanks for you help. – Manngo Feb 25 '16 at 23:48
  • Even simpler than mine. Excellent. –  Feb 25 '16 at 23:54
0

You could try to use IP address due to classroom network to carry out your function.

example: Your IP range is 192.168.1.2 to 192.168.1.100

for /L %%a in (2,1,100) do (
  if exist \\192.168.1.%%a\foo\bar\folder (
    your command
  )
)
enjoying
  • 177
  • 1
  • 9