How to write a Batch File on Windows

Hello Experts

Can someone show me how to compile a batch file that will run each time Windows  boots up?

I would like to run the following in a batch file

robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Regards
LVL 1
Member_2_7966113Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnBusiness Consultant (Owner)Commented:
Make a file with the command above in it. Call it firstbatch.bat or such like.

Now open Task Scheduler and make a task to run this batch file at start up. You need to use administrative credentials to run the file.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hello ThereSystem AdministratorCommented:
It's easy. If you are in a domain, assign your script to the user as logon script. If not, use Task Scheduler to do it.

Edited: John H., you were 30 sec faster. :D
0
Hello ThereSystem AdministratorCommented:
E.g. create a new task that will run Powershell.exe under admin account and then in Arguments field write the path to your script.
(arguments:  -ExecutionPolicy RemoteSigned -File C:\script.bat)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Nope, Hello There, you do not use PowerShell to run .bat or .cmd files ... It may work (e.g. for the single line used here), but if it is overkill.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You do not need a batch file. Just run the command in cmd, that is use the line
cmd /c robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Open in new window

in Task Scheduler.
0
JohnBusiness Consultant (Owner)Commented:
However you do it, Task Scheduler is a nice way to set it. You may find a batch file (even though only one line) is easy for you to follow.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A batch file is easier to change for most people. And if the command is more complex, I would not try to go without batch file.
However, I just noticed that we do not need to run cmd. RoboCopy is a self-contained executable, able to run on its own, as long as we do not use file redirection or the like. So it should be sufficient to run
robocopy z:\mytestfolder c:\myhardfolder /move /minage:1

Open in new window

as a task.
0
Joe Winograd, Fellow&MVEDeveloperCommented:
There have been numerous suggestions about creating a task in Task Scheduler. If you're not familiar with how to do that, this Experts Exchange 5-minute video Micro Tutorial will get you going on it:
How to use the Windows Task Scheduler - An Introduction

Btw, an alternative to using the Task Scheduler is to put a shortcut to the batch file in the Startup program group — it will then run when Windows starts. Regards, Joe
0
Joe Winograd, Fellow&MVEDeveloperCommented:
The question is how to execute a robocopy command each time Windows starts. Every post in this thread is helpful in answering that question. There is no "Best" answer, so I selected the first one posted as the Accepted Solution and all the others as Assisted Solutions, with the points split evenly among the experts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.

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.