Computer Hope

Microsoft => Microsoft DOS => Topic started by: Arotin on August 03, 2016, 11:30:33 AM

Title: Batch Script - Copy folder to remote computers
Post by: Arotin on August 03, 2016, 11:30:33 AM
Hello,

I want to write a batch script that will connect to a list of computers (FOR loop) and copy a folder to a specific directory on that remote computer.

So connect to the remote computer and copy folder "TEST" from my computer to the remote computers following path : C:\Program Files (x86)\folder1\TEST

If the file exists then replace it or even rename it to TEST.old.
I know how to put all the credential stuff i just dont know how to start this im new to batch scripting so any help would be great.



Thank you <3
Title: Re: Batch Script - Copy folder to remote computers
Post by: Allan on August 03, 2016, 11:33:42 AM
I deleted your duplicate thread.
Title: Re: Batch Script - Copy folder to remote computers
Post by: Arotin on August 03, 2016, 11:34:29 AM
I deleted your duplicate thread.

Yeah thanks i posted it in the regular windows forum... sorry about that
Title: Re: Batch Script - Copy folder to remote computers
Post by: Geek-9pm on August 03, 2016, 11:46:50 AM
Arotin, you placed this is two places.
Yes, this can be done in a batch file. I won't tell you not to do it.
First, here are things you must reconcile.

Most Desktop PCs today have some security to prevent anybody from just pushing files over the network. You must have permission from all users, or you have to be the master administrator of the workplace.

Otherwise, malware infections would be much worse than they are. All major operating systems now have some way to prevent just anybody from coming aboard the network.

Now, having said that, creating a new folder in DOS on remote computer is a big tough job. Do you really need to do that? Do you know how that has to be done?

Presently, many administrators are using a newer method of writing scripts for DOS. It is PowerShell. It is an alternative to using the CMD feature.

I can not help you with  DOS alone. If you want to use PowerShell I can be of some help.   :)

Otherwise, somebody else will step in with the DOS solution.

Resistance is futile: You will use PowerShell (http://www.infoworld.com/article/2614339/windows-server/resistance-is-futile--you-will-use-powershell.html) << click on link.
Title: Re: Batch Script - Copy folder to remote computers
Post by: Arotin on August 03, 2016, 12:03:24 PM
Arotin, you placed this is two places.
Yes, this can be done in a batch file. I won't tell you not to do it.
First, here are things you must reconcile.

Most Desktop PCs today have some security to prevent anybody from just pushing files over the network. You must have permission from all users, or you have to be the master administrator of the workplace.

Otherwise, malware infections would be much worse than they are. All major operating systems now have some way to prevent just anybody from coming aboard the network.

Now, having said that, creating a new folder in DOS on remote computer is a big tough job. Do you really need to do that? Do you know how that has to be done?

Presently, many administrators are using a newer method of writing scripts for DOS. It is PowerShell. It is an alternative to using the CMD feature.

I can not help you with  DOS alone. If you want to use PowerShell I can be of some help.   :)

Otherwise, somebody else will step in with the DOS solution.

Resistance is futile: You will use PowerShell (http://www.infoworld.com/article/2614339/windows-server/resistance-is-futile--you-will-use-powershell.html) << click on link.

Thank you for the reply,

Yes i do have the other computers credentials, now the reason i want to copy this folder is because it has an application in it... its a hermes application so an install is not necessary i just need to basically swap some files but to be safe the whole folder is better... i can try powershell if you feel like teaching a new soul? (:

Thanks!
Title: Re: Batch Script - Copy folder to remote computers
Post by: Squashman on August 03, 2016, 12:54:22 PM
I know how to put all the credential stuff
Well that could be a done a couple of different ways and depending on which one you are using could alter the way the script could be coded.
Title: Re: Batch Script - Copy folder to remote computers
Post by: Geek-9pm on August 03, 2016, 01:09:24 PM
(Dictation.)
Here is a quick reply to your request. Learning power shell might be a rather time-consuming project. So I will outline for you some of the ways you can do it in DOS only provided your work environment is not very complicated. Because other people also read these posts and try to apply to themselves, I must make this as simple as possible without too many things that are specific to your situation.
It is considered good practice to not try to share an entire drive or partition on the network. Actually, there is no law against it, just something that helps a little bit with keeping one's sanity. So instead of trying to share an entire drive, just share one designated folder. The folder can be at the root level, no problem with that. But it should have a unique name that will not cause confusion to anybody using the system. It could be a serious name or a silly name of something a makes no sense at all. Just something that will avoid confusion with any other program or system folders on the system. For this discussion let's say that the name of the special folder is going to be:
Prozac.
That folder can be placed in the root of the C: drive. You'll share it on the network and give the administrator privileges to access this folder. In a very simple work environment you might even allow all users to access this folder. Later, if that becomes an issue, you could do something else to improve the security.
Now on your own administrator workstation you will find that folder on the network and you will assign for each user a drive letter on your own system. That is, the unique drive letter for each instance of where the Prozac folder is found on an user. Now on your local command prompt you will be able to access the Prozac folder of each user by using the drive letter associated with that particular users Prozac file.

Every user must have a folder named Prozac with network sharing on
.

Next, you should make use of one of the better copy programs. Xcopy is good. But a better choice would be Robo copy. If you don't have Robo copy, it is downloadable from the Microsoft site.
Study the syntax for Robo copy and you will see how easy it will be to make directories and copy files into a remote directory. You can do this all inside of a DOS box. In this context I mean the command prompt that is actually created by the CMD executive.
A brief outline of Robo copy is explained in the computer hope archives.
After studying over Robo copy, try writing a test batch file for use on just your local computer. Sorry that I am not able  to write the file for you. I am extremely slow at typing in code, in fact this post is really being done by speech recognition.

RoboCopy
http://www.computerhope.com/robocopy.htm
Title: Re: Batch Script - Copy folder to remote computers
Post by: Arotin on August 03, 2016, 01:22:45 PM
(Dictation.)
Here is a quick reply to your request. Learning power shell might be a rather time-consuming project. So I will outline for you some of the ways you can do it in DOS only provided your work environment is not very complicated. Because other people also read these posts and try to apply to themselves, I must make this as simple as possible without too many things that are specific to your situation.
It is considered good practice to not try to share an entire drive or partition on the network. Actually, this no law against it, just something that helps a little bit with keeping one's sanity. So instead of trying to share an entire drive, just share one designated folder. The folder can be at the root level, no problem with that. But it should have a unique name that will not cause confusion to anybody using the system. It could be a serious name or a silly name of something a makes no sense at all. Just something that will avoid confusion with any other program or system folders on the system. For this discussion let's say that the name of the special folder is going to be:
Prozac.
That folder can be placed in the root of the C: drive. You'll share it on the network and give the administrator privileges to access this folder. In a very simple work environment you might even allow all users to access this folder. Later, if that becomes an issue, you could do something else to improve the security.
Now on your own administrator workstation you will find that folder on the network and you will assign for each user a drive letter on your own system. That is, the unique drive letter for each instance of where the Prozac folder is found on an user. Now on your local command prompt you will be able to access the Prozac folder of each user by using the drive letter associated with that particular users Prozac file.
Next, you should make use of one of the better copy programs. Xcopy is good. But a better choice would be Robo copy. If you don't have Robo copy, it is downloadable from the Microsoft site.
Study the syntax for Robo copy and you will see how easy it will be to make directories and copy files into a remote directory. You can do this all inside of a DOS box. In this context I mean the command prompt that is actually created by the CMD executive.
A brief outline of Robo copy is explained in the computer hope archives.
After studying over Robo copy, try writing a test batch file for use on just your local computer. Sorry that I am not able  to write the file for you. I am extremely slow at typing in code, in fact this post is really being done by speech recognition.

RoboCopy
http://www.computerhope.com/robocopy.htm

Thank you for the reply,

So the idea is to share a drive that has that folder in it (not a whole directoy just a regular folder) then once i share that drive i basically copy and replace the folder to where its suppose to be on the remote computer... interesting - i will try that and test the script - i will post back if it works (:


Thank you for taking the time!
Title: Re: Batch Script - Copy folder to remote computers
Post by: foxidrive on August 03, 2016, 04:06:38 PM
This is untested and should take a list of computers in a text file in the form of IP addresses or computer names
192.168.0.23
labcomputer01

and is designed to change the remote folder to be exactly like the local folder.
The /MIR switch will wipe a remote share or folder if the target folder is missing or wrong - so be careful in your testing.

Aspects to consider:
are permissions on the source folders and target folder
and that Robocopy has switches to control how many times it tries to copy a file if it fails, and also the time between retries.


Code: [Select]
@echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
   robocopy "C:\Program Files (x86)\folder1\TEST" "\\%%a\sharename\test" /mir
)
echo done
pause
Title: Re: Batch Script - Copy folder to remote computers
Post by: foxidrive on August 03, 2016, 04:21:48 PM
After studying over Robo copy, try writing a test batch file for use on just your local computer. Sorry that I am not able  to write the file for you. I am extremely slow at typing in code, in fact this post is really being done by speech recognition.

Geek, can I ask if you have limited sight or perhaps limited mobility?
I wasn't aware that you have restrictions and that discussion is your strong point.

You don't have to give an answer, I'm just interested in how others live their lives with challenges.  A family member had difficulty with conversation and I have a bone/joint disorder myself where joints freeze and do nasty things. 

It gives me a different outlook on people and how they live - all people, even those with bodies that work pretty well. :)

Title: Re: Batch Script - Copy folder to remote computers
Post by: Geek-9pm on August 03, 2016, 06:10:33 PM
Okay here I am again. The major reason I cannot do much coding for the people because at the present time I have this serious vision impairment.  A number of years ago I was in South America on a project that was very exciting for me. But I spent a lot of time outdoors and I guess I must've ignored some of the warnings people gave. Anyhow, I developed cataracts in my eyes. Some say that sunlight might be the cause of cataracts, others say it just happens anyway. Well, anyway, I had to have the cataracts removed and I was living in one of the smaller towns. So I decided to have the work done in the smaller towns and, guess what? The operation failed and I lost the vision in one eye.
Well, that was about 16 years ago and during this time I have never had any real jobs, except selling some of the stuff I brought back from South America. On eBay of course.
But to keep myself occupied I spend a lot of time in front of the computer. In fact, I have to use a computer to read. It causes an awful lot eyestrain to read a book using special magnifiers because it causes a lot of neck strain.. With the computer I can have a 20 inch monitor and get my face is close to it  as I want.
But there's another reason to charge me to write code for the people. My style is different. When I take on a project I do it in a big way and I have to have lots of notes and copies of my source code and all other kinds of stuff used to test out my code. I am not very good at writing just a few lines of code for something, although I do that once in a while.
Also things I do are just for my own stuff. I don't think I've ever written code for anybody else after returning to the United States. For some time I have been retired and living just on Social Security and the computer provides for me the way to still read and learn things in spite of my location problem.
Anyway, here is briefly the method I would use to write a batch file that has to do a repetitive task. I would get the task down to just a list of things that I need to do and I would use the list itself as the template for writing the main part of my batch file. In batch programming there is a call statement which can be used somewhat like the call statement used in other programming languages. Used correctly, it can simplify the overall structure of a programming project.
 Here is a simplified example of how I would do a rather large project and bring it down a list of things. Such as update the contents of a folder on each remote computer.
 CALL NEWJOB BILL
 CALL NEWJOB JACK
 CALL NEWJOB JILL
 CALL REPORT

The NEWJOB is another batch file that does the stuff with RoboCopy.  Somehow it   will be something that invokes  RoboCopy for a given user. Here is a list of users and there shares
BILL is \\DELL-94\Prozac
JACK is \\HP-77\Prozac
JILL  is \\OFFICE-1\Prozac
And again, I would use more CALL statements to hide details.
In general I prefer to avoid a FOR Loop unless I could justify how a for loop can obscure the flow of the program.  Likewise, I do not like nested IF THEN ELSEIF constructs.A simple list like the above is easy to trace or to make check  points and find out what went wrong. But that's just me

Before I went to South America I had worked in different things including working as an electronic technician and also as Junior program. Later. I tried selling public domain software by mail. Later I did a private consulting business. And one time I had a client who established a network for messaging using satellites and dial-up modems. That was back before the to Internet came along and back before DSL modems were in wide use. The satellite would send messages out to hundreds of clients and individual clients could send messages to the central station using dial-up modem. I wrote software that worked on the early IBM type PC. Actually, the PCs were made by Epson and had very little memory. It was a tough thing to do back in those days as back then there were not any good communications programs or libraries available for the PC. Well, actually there was one good library that I had found and I used it. It was a library created by Microsoft. So I've had some experience in writing code for use on a personal computer as well as some earlier work on some commercial equipment in Silicon Valley.
I hope this is of some help to others. Thanks for letting me talk.  :D
Title: Re: Batch Script - Copy folder to remote computers
Post by: foxidrive on August 04, 2016, 10:24:05 PM
I developed cataracts in my eyes. Some say that sunlight might be the cause of cataracts, others say it just happens anyway. Well, anyway, I had to have the cataracts removed and I was living in one of the smaller towns. So I decided to have the work done in the smaller towns and, guess what? The operation failed and I lost the vision in one eye.

I see Geek.  16 years ago was the turn of the century and the Y2K bug hysteria.

I wonder if you use also screen-reader software to help you read the computer screen by listening to the computer generated voice reading the screen. 

That would be a very difficult way to read code from batch scripts posted here.   I'd tear my hair out if I had to listen to batch keywords being spoken and where I would not have any kind of overview of the script, or an ability to look back-and-forth through the code.

Regarding cataracts: My Mother had a cataract removed ten years ago and she lost the sight in that eye.  The operation was successful in her case but an underlying disease caused her blindness in that eye.  I relate that to say that the severe cataract in her good eye had to go untreated due the risk of total blindness if an operation for that eye failed.

Thanks to the mods for the brief off-topicness.

And thanks for your descriptions of parts of your work and life, Geek. 

I can easily see good reasons for your preferences for straightforward code that is easier to debug and follow when your sight isn't all that crash hot.
Title: Re: Batch Script - Copy folder to remote computers
Post by: Geek-9pm on August 04, 2016, 10:59:29 PM
Thanks for letting me talk.
My expertise is in assembly language and getting different things working together In one very old project I had to use three different languages to make a stand alone program that anybody could use in a telecommunications systems.
That was back in the days of 300 baud dial up modems.
Just after Moses crossed  the Red Sea.    :P
Title: Re: Batch Script - Copy folder to remote computers
Post by: Arotin on August 08, 2016, 10:43:16 AM
Guys,


Is there a way around this situation?

So when i use ;

setlocal EnableDelayedExpansion

set pass=Password123!

;
The "!" in my password gets disregarded because of the "enabledelayedExpansion" problem is i need that in there because the passwords have that... lol is there a way i can make it that it includes that in my password with delayed expansion enabled?


Thanks
Title: Re: Batch Script - Copy folder to remote computers
Post by: foxidrive on August 09, 2016, 03:04:29 PM

So when i use ;

setlocal EnableDelayedExpansion

set pass=Password123!


The "!" in my password gets disregarded because of the "enabledelayedExpansion" problem is i need that in there because the passwords have that... lol is there a way i can make it that it includes that in my password with delayed expansion enabled?

There is but other characters will still be excluded. 
The question is how robust do you need your password routine?


Title: Re: Batch Script - Copy folder to remote computers
Post by: foxidrive on August 13, 2016, 05:35:56 AM
The "!" in my password gets disregarded because of the "enabledelayedExpansion" problem is i need that in there because the passwords have that... lol is there a way i can make it that it includes that in my password with delayed expansion enabled?

It is pointless for someone to answer your question when they could have to answer it again when you reply to say it doesn't work.  Your knowledge doesn't cover the other issues that result when entering passwords or you'd know how to answer this question yourself and wouldn't have asked it in the first place.


People ignore a response because of their poor understanding, and yet they believe they know a great deal. 

There is a term for this called the Dunning-Kruger effect and it is widespread in technical forums these days.


On second glance I see that you are just thankless, and made no response to an answer to your question earlier in this thread.  Taking people for granted is another common blight on the internet.

People do that without considering that they are interacting with a person, and they don't have enough sense of social morals to know that it is rude and self-centered behaviour. 

I was not the sole person that he ignored in this thread.   Excuse my grumpy off-topic post for the 10,000th time I've seen this shabby treatment of people in scripting forums.