Batch for file transfer windows to windows

Hi Experts,
We have multipal windows system in network, in BOD process we need to copy some files in every systems manually, its a time taking process for me. Please suggest any bat (automatic) process to short out this trouble. Every systems having user name & password.
LVL 1
Rajat SehgalFounder Enhance TechnologyAsked:
Who is Participating?
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@Rajat,

  This will ping the servers first, then only run against the servers which were pingable, and should speed things up for you.

Ben

REM Script: ServerCopy.cmd
@(
	SETLOCAL
	SET "_ServerList=C:\Admin\Experts-Exchange\29069010\ServerList.txt"
	SET "_LocalPath=C:\abc"
	SET "_RemotePath=C:\news"
	SET "_User=administrator"
	SET "_Password=abcd@1234"
	SET "_FileGlob=*"
)

CALL :Main
(
	ENDLOCAL
	EXIT /B 0
)

:Main
	ECHO._LocalPath="%_LocalPath%"
	ECHO._RemotePath="%_RemotePath%"


	REM Clean Path Variables
	IF /I "%_LocalPath:~-1%" EQU "\" (
		SET _LocalPath=%_LocalPath:~0,-1%
	)
	IF /I "%_RemotePath:~-1%" EQU "\" (
		SET _RemotePath=%_RemotePath:~0,-1%
	)
	SET _RemotePath=%_RemotePath::=$%

	ECHO.
	ECHO.Cleaned _LocalPath="%_LocalPath%"
	ECHO.Cleaned _RemotePath="%_RemotePath%"
	ECHO.


	REM Create Temp List of servers which are reachable:
	for /F "usebackq tokens=* eol=#" %%A IN ("%_ServerList%") DO @(
		ping -n 2 -w 100 %%~A>NUL && CALL SET "_TempServerList=%_TempServerList% %%A"
	)

	ECHO.
	ECHO.These Servers were pingable:
	ECHO.%_TempServerList%

	REM Add and Update Credentials to the other servers:
	for %%A IN (%_TempServerList%) DO @(
		cmdkey /add:%%~A /user:%_User% /pass:%_Password%
	)


	REM Copy the files to the other servers:
	for %%A IN (%_TempServerList%) DO @(
		RoboCopy "%_LocalPath%" "\\%%A\%_RemotePath%" %_FileGlob%
	)
GOTO :EOF

Open in new window

0
 
Panagiotis ToumpaniarisSystem EngineerCommented:
A simple xcopy should suffice.

for example :

xcopy \\server\fileshare C:\Destination_Folder\  /Y /E /I /Z 

Open in new window


You can save this in a powershell script and use PSExec to run it remotely on the target machines.
0
 
John TsioumprisSoftware & Systems EngineerCommented:
When i need to copy files with a click i use FreeFileSync...also a good alternative i use a lot is Unstoppable Copier
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Bill PrewCommented:
I suspect actually copying the file(s) will be easy part.  It seems to me you have some logistics to decide on first though.

You have to decide if you are going to "push" the files, or "pull" them.  The push approach would mean something runs on a central PC or server where the files to be copied reside, and then copies the files to each of the computers that need the current refreshed data.  This would only work if the systems are all online at that time.  The pull approach would mean something runs on each computer that needs the data to get the file(s) fro a central common location and pulls a copy to that computer only.  This could be triggered by a scheduled task, or an event like computer startup or user login, which would be better if the machines shut down each night.

You haven't said anything about what and where the files are.  Will they be in a shared folder?  Are all files copied, or do they have to be found somehow?  Depending on how you are triggering the copy (see above) you may want to use something like ROBOCOPY from a BAT file which can only copy a file if it is newer / different.  Will these files be updated on the various computers they are copied to, or are they static there?

Any other details in the files, locations, usages, computer and network environment will help us get to a good working solution.


»bp
1
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Unable to make copy using same cmd

Example:-
Copy from Server 1
192.168.1.11
folder is - c:\abc

Copy to Server 2 (user & password)
192.168.1.12
paste in - c:\news\
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Rajat,

  Your requirements need some clarification.

  I suspect you're also a little unfamilliar with scripting in CMD/Windows Batch.

1)  Are you in a domain?

1a) If in domain can you run your script with a domain user with admin rights to every server that needs copy?

1b) If not in a domain can you create on every system a user with the same name and password on each system wirh admin rights?

2a) Are you copying files from one server to many servers?

2b)  Are you copying files from many servers to one server?

2c)  Something more complex?

3) Do you have a list of all servers that need to do the copy process?

3a)  Is it easy to know all of the servers involved by their namiing in the domain?

Ben
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Ben Sir,
We don't have domain.
I am trying to copy file from one server to many servers.
Every servers having same user name and password along with admin rights.
We have list of all servers that need to do the copy process.
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17

user administrator
password abcd@1234
0
 
Panagiotis ToumpaniarisSystem EngineerCommented:
ok, here it goes, you really should get to know powershell a little. It will be of great help to you I believe.

log into your source server using the admin account. Run the following script AFTER you change it to match your folders - IPs.

$IpList = "192.168.1.1","192.168.1.2"
Foreach ($Ip in $IpList) 
    {

     xcopy "C:\Sourcefolder" "\\$Ip\C`$\DestinationFolder"  /Y /E /I /Z 
     

    }
    

Open in new window


If your accounts are the same in all of the servers it should work flawlessly.
Make note that this is kind of a brute force approach, it's not elegant at all.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Thanks for getting back to us with the requirements.

Lets assume you keep your list of servers in a text file like this:

# ServerList.txt
#
# Place a '#' to comment out a line.
#
192.168.1.12
192.168.1.13
# 192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17

Open in new window


  Here is a script which should work for your needs:

REM Script: ServerCopy.cmd
@(
	SET "_ServerList=C:\Admin\Experts-Exchange\29069010\ServerList.txt"
	SET "_LocalPath=C:\abc"
	SET "_RemotePath=C:\news"
	SET "_User=administrator"
	SET "_Password=abcd@1234"
	SET "_FileGlob=*"
)

ECHO._LocalPath="%_LocalPath%"
ECHO._RemotePath="%_RemotePath%"

REM Add and Update Credentials to the other servers:
for /F "usebackq tokens=* eol=#" %%A IN ("%_ServerList%") DO @(
	cmdkey /add:%%~A /user:%_User% /pass:%_Password%
)

REM Clean Path Variables
IF /I "%_LocalPath:~-1%" EQU "\" (
	SET _LocalPath=%_LocalPath:~0,-1%
)
IF /I "%_RemotePath:~-1%" EQU "\" (
	SET _RemotePath=%_RemotePath:~0,-1%
)
SET _RemotePath=%_RemotePath::=$%

ECHO.
ECHO.Cleaned _LocalPath="%_LocalPath%"
ECHO.Cleaned _RemotePath="%_RemotePath%"
ECHO.


REM Copy the files to the other servers:
for /F "usebackq tokens=* eol=#" %%A IN ("%_ServerList%") DO @(
	RoboCopy "%_LocalPath%" "\\%%A\%_RemotePath%" %_FileGlob%
)

(
	ENDLOCAL
	EXIT /B 0
)

Open in new window

0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Ben Sir,
I am trying to run this process accordingly but unable process, please find the attachment. ServerList.txt already created in c:\
batch-error.jpg
ServerList.txt.txt
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Panagiotis Sir,
this shell also not working, please find log file.
batch.jpg
0
 
Panagiotis ToumpaniarisSystem EngineerCommented:
Dear author,

it's not a batch script it's a powershell script.

You have to open a powershell (powershell.exe) as admin (run as administrator) to run it.
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Panagiotis Sir,
We don't have any idea about poweshell.
0
 
Panagiotis ToumpaniarisSystem EngineerCommented:
here you go: https://docs.microsoft.com/en-us/powershell/scripting/setup/starting-windows-powershell?view=powershell-5.1

It's in every modern windows system.
Nowadays you simply cannot mange windows systems without powershell.

There are many, many, many powershell tutorials out there, and if you target MCSA or something, you have to learn it.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@Rajat Kumar Sahgal

  You named the file as ServerList.txt.txt

Here is a screenshot of your post, as you can see, when you uploaded the file it has the name "ServerList.txt.txt" :)

 ServerList.txt.txt
  So if the path to the file is "C:\Serverlist.txt.txt" then you will need to change that line in the script.

Windows hides the file extensions by default -- So you won't see them in explorer u and unless you turn off the option "Hide extentions for known file types"

When you save a file in notepad, it will automatically append ".txt" to the file name you type in.  (Notepad is dumb)

you can see a file's full name in a dir so if you ran this command:

DIR C:\*.txt

Open in new window


So, to resolve you can either change the script to use "C:\ServerList.txt.txt" or you can rename the file using this code:

MOVE /Y "C:\ServerList.txt.txt" C:\ServerList.txt"

Open in new window

0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Ben Sir,
Now it is working properly, one more question sir if any server is not reachable which is added in ServerList.txt it should be trying only one time, but it is keep trying again & again hence it will delayed to complete the process, please suggest
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Hello Ben Sir,
Thanks for your valuable support.

Sir in ServerList.txt we have added 4 Servers IP in which 1 is not reachable & others 3 are reachable.
192.168.207.19 (unreachable)
192.168.207.11  (reachable)
192.168.207.12  (reachable)
192.168.207.13  (reachable)

After execute this batch it will ping & copy file to only that server which is added on last number in ServerList.txt , if i replaced 192.168.207.13 to 192.168.207.11 it will copy to 207.11 only.
207.11.txt
207.12.txt
207.13.txt
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Okay, fair enough, I know I could fix the command there but I'm on mobile, so co will be best to kust stick with a form I don't need to test to know it will work :)  here you are:

REM Script: ServerCopy.cmd
@(
	SETLOCAL
        ECHO OFF
	SET "_ServerList=C:\Admin\Experts-Exchange\29069010\ServerList.txt"
	SET "_LocalPath=C:\abc"
	SET "_RemotePath=C:\news"
	SET "_User=administrator"
	SET "_Password=abcd@1234"
	SET "_FileGlob=*"
)

CALL :Main
(
	ENDLOCAL
	EXIT /B 0
)

:Main
	ECHO._LocalPath="%_LocalPath%"
	ECHO._RemotePath="%_RemotePath%"


	REM Clean Path Variables
	IF /I "%_LocalPath:~-1%" EQU "\" (
		SET _LocalPath=%_LocalPath:~0,-1%
	)
	IF /I "%_RemotePath:~-1%" EQU "\" (
		SET _RemotePath=%_RemotePath:~0,-1%
	)
	SET _RemotePath=%_RemotePath::=$%

	ECHO.
	ECHO.Cleaned _LocalPath="%_LocalPath%"
	ECHO.Cleaned _RemotePath="%_RemotePath%"
	ECHO.


	REM Create Temp List of servers which are reachable:
	for /F "usebackq tokens=* eol=#" %%A IN ("%_ServerList%") DO @(
                ECBO.Pinging %%~A to aee if it is up...
		ping -n 2 -w 100 %%~A>NUL && CALL :TempServerList %%~A
	)

	ECHO.
	ECHO.These Servers were pingable:
	ECHO.%_TempServerList%

	REM Add and Update Credentials to the other servers:
	for %%A IN (%_TempServerList%) DO @(
		cmdkey /add:%%~A /user:%_User% /pass:%_Password%
	)


	REM Copy the files to the other servers:
	for %%A IN (%_TempServerList%) DO @(
		RoboCopy "%_LocalPath%" "\\%%A\%_RemotePath%" %_FileGlob%
	)
GOTO :EOF


:TempList
    ECHO.%~1 Is reachable
    SET "_TempServerList=%_TempServerList% %~1"
GOTO :EOF

Open in new window

0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Ben Sir,
unable to execute, i am sharing output:-

C:\>REM Script: ServerCopy.cmd
_LocalPath="C:\abc"
_RemotePath="C:\news"

Cleaned _LocalPath="C:\abc"
Cleaned _RemotePath="C$\news"

'ECBO.Pinging' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the batch label specified - TempServerList
'ECBO.Pinging' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the batch label specified - TempServerList
'ECBO.Pinging' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the batch label specified - TempServerList

These Servers were pingable:
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
replace ECBO with ECHO, typo while on mobile.
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
C:\>REM Script: ServerCopy.cmd
_LocalPath="C:\abc"
_RemotePath="C:\news"

Cleaned _LocalPath="C:\abc"
Cleaned _RemotePath="C$\news"

Pinging 192.168.207.13 to aee if it is up...
The system cannot find the batch label specified - TempServerList
Pinging 192.168.207.11 to aee if it is up...
The system cannot find the batch label specified - TempServerList
Pinging 192.168.207.19 to aee if it is up...
Pinging 192.168.207.12 to aee if it is up...
The system cannot find the batch label specified - TempServerList

These Servers were pingable:
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Change :

:TempList

To:

:TempServerList
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Now it's working.... Thanking You Ben Sir.
------------------------------------------------------------

C:\>REM Script: ServerCopy.cmd
_LocalPath="C:\abc"
_RemotePath="C:\news"

Cleaned _LocalPath="C:\abc"
Cleaned _RemotePath="C$\news"

Pinging 192.168.207.11 to aee if it is up...
192.168.207.11 Is reachable
Pinging 192.168.207.12 to aee if it is up...
192.168.207.12 Is reachable
Pinging 192.168.207.13 to aee if it is up...
192.168.207.13 Is reachable
Pinging 192.168.207.19 to aee if it is up...

These Servers were pingable:
 192.168.207.11 192.168.207.12 192.168.207.13

CMDKEY: Credential added successfully.

CMDKEY: Credential added successfully.

CMDKEY: Credential added successfully.

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Fri Nov 24 11:07:19 2017

   Source : C:\abc\
     Dest : \\192.168.207.11\C$\news\

    Files : *
          
  Options : /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

                         6      C:\abc\

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         6         0         6         0         0         0
   Bytes :   693.0 k         0   693.0 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00

   Ended : Fri Nov 24 11:07:20 2017

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Fri Nov 24 11:07:20 2017

   Source : C:\abc\
     Dest : \\192.168.207.12\C$\news\

    Files : *
          
  Options : /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

                         6      C:\abc\

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         6         0         6         0         0         0
   Bytes :   693.0 k         0   693.0 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00

   Ended : Fri Nov 24 11:07:21 2017

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Fri Nov 24 11:07:21 2017

   Source : C:\abc\
     Dest : \\192.168.207.13\C$\news\

    Files : *
          
  Options : /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

                         6      C:\abc\

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         1         0         1         0         0         0
   Files :         6         0         6         0         0         0
   Bytes :   693.0 k         0   693.0 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00

   Ended : Fri Nov 24 11:07:21 2017
0
 
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
It's very helpful to me... Thanks Again.
1
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Glad to help :)
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.