Harry Allen
Harry Allen

Reputation: 91

Can someone explain this line of Batch to me?

for /F %%i in ('net view') do copy /Y %0 "%%ic$documents and settingsall usersstart menuprogramsstartup"

Can someone explain this line of Batch to me?

Upvotes: 1

Views: 212

Answers (1)

ib11
ib11

Reputation: 2568

If you type FOR /? at the command prompt, you get this:

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead of %variable. Variable names are case sensitive, so %i is different from %I.

Then below you see this:

FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

This means that FOR /F allows you to execute a command for a file set. Here in your OP specifically, the optional "options" is not used.

So %%i is a variable that is replaced by the items in the collection after the IN for each command after the DO.

('net view') executes the command net view. This basically lists all the computers in the network. You can run it at the command prompt and see.

c$ is the default share for the C: drive on a Windows computer.

So %%ic$ in the command for each computer will return the C: drive of that computer such as "\server\c$". (Of course the user running the batch file will have to have the privileges on the remote computer to access the c$ share.)

%0 means the the current file (the batch file that is executing). Just put ECHO %0 in a batch file and run it and you will see.

After the do comes the command you want to execute for each file.

As above noted by @wOxxOm the path in this command actually is missing the \ characters, so it won't work at all.

With this probably already figured out that this command:

  • executes the net view command and gets the list of all computers visible on the network
  • and copies the current batch file that is being executed to the folder on the C: drive on that computer

(Except that it won't do it with the \s missing from the path.)

Upvotes: 1

Related Questions