1

I want to copy specific file from one machine to 5 other machines, so I have server list file contains the ip addresses of each machine like:

10.10.1.3
10.10.1.4
10.10.1.5
10.10.1.6
10.10.1.7

in my batch file:

SET File=C:\Files\servers.txt
SET User=user
SET Password=pass

      IF EXIST b:\ (
        NET USE b: /DELETE /Y
    )

        FOR /F %%A IN (%File%) DO (
                START /WAIT NET USE b: \\%%A\C$\Temp /user:%User% %Password%

                COPY C:\Logs\L1.log b:\L1.log /Y        

          IF EXIST b:\ (
                   NET USE b: /DELETE /Y
                )
        )

the problem is in the first server I get error message The system cannot find the drive specified but for the others servers everything works great. I think it's something with the NET USE of course maybe the map network is deleted before finish copy?

Is there any way in a batch file to loop some servers and for each one of the servers open map network copy files wait till copy is completed and move on to next server?

EDIT:

I have an update for this problem: the source machine and the target machine are both in different domains. I have a user define as admin in both of the machines. The machines knows each other (I can open the target folder in the source machine like \server\C$\temp and I can paste anything I want there)

I tried to copy files without using net use and just copy from C:\file.log \server\c$\temp\file.log for each server (I have 5) and for 3 servers it worked and the other two I had an error: Logon failure: unknown user name or bad password

    FOR /F %%A IN (%File%) DO (
        COPY C:\temp\file.log \\server\c$\temp\file.log /Y
  )

What can be the problem?

Please help? Thank you in advanced.

user3132295
  • 288
  • 4
  • 23

2 Answers2

0

When using START command, you need to either specify new window name, or leave it blank in double quotes, if you want it to run in the same open CMD window. However, you don't need to use START in this particular case. Try adding timeout for the 1st or all server connections in the list:

IF EXIST b:\null NET USE b: /DELETE
:: more code here

NET USE b: \\%%A\C$\Temp /user:%User% %Password%
if "%%A"=="10.10.1.3" timeout 5 >nul
sambul35
  • 1,058
  • 14
  • 22
0

The best way would be if the user/password is the same on every machine (credientals shared throughout the domain)

In that case, no need for NET USE, just copy to UNC path directly

FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\C$\Temp\L1.log

I understood you have a problem on 2 machines out of 5. You cannot access C$ directly if you are not administrator of the machines.

Check:

  • domain (echo %USERDOMAIN% on the remote machine): All machines must be on the same domain for it to work
  • whether you are administrator or not on the remote machine: you cannot mount drives with the C$ trick if you are not administrator.

There is an alternate solution for you if this doesn't work out:

Share C:\TEMP (read/write) as the same name ex: sharedtemp on the 5 machines (or ask an admin to do that)

Then adapt the script like this:

FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\sharedtemp\L1.log

Even if you are not an administrator, you'll be able to access the share that way.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219