Solved

Batch file to copy a file

Posted on 2011-03-11
15
462 Views
Last Modified: 2012-08-14
Please tell me how I can create a .bat file that copies a file "example.txt" from C:/Folder1/Example.txt to C:/Folder2/Example.txt

Thanks!
0
Comment
Question by:dshrenik
15 Comments
 
LVL 4

Expert Comment

by:loki_loki
ID: 35107676
create a text file and enter

copy "C:/Folder1/Example.txt"  "C:/Folder2/Example.txt"

save it with .bat extension and that will do it.

Other parameters like overwritting etc can be found by opening a command prompt and typing copy /?
0
 
LVL 5

Accepted Solution

by:
NotVeryFat earned 375 total points
ID: 35107677
xcopy /c /q /y "c:\folder1\example.txt" c:\Folder2\
0
 

Author Comment

by:dshrenik
ID: 35107687
It says "the system cannot find the file specified". My folder name has spaces. Does tht matter?
I am actually overwriting the file. Can you include the relevant parameters for the batch file?
Thanks!

@NotVeryFat:
Can you please describe what the parameters mean? Thanks!
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 5

Expert Comment

by:NotVeryFat
ID: 35107689
To get round the spaces issue, put the whole name in quotes e.g. "c:\This folder\That folder\File name.txt"
0
 

Author Comment

by:dshrenik
ID: 35107697
I do have it in quotes.
0
 
LVL 3

Expert Comment

by:ccwill88
ID: 35107698
In DOS Command mode use
echo copy c:\folder1\example.txt  c:\folder2 >copyfile.bat

Open in new window

0
 
LVL 5

Assisted Solution

by:NotVeryFat
NotVeryFat earned 375 total points
ID: 35107704
/c continues copying if errors occur - handy if you're doing a whole load of copying, one problem file won't cause the whole lot to fail
/q quiet - doesn't show the file names as they copy
/y surpresses prompting if running as a batch

you don't necessarily need any of the above, they just make things run better.

You can simply create a .bat file and add the following line:

xcopy "c:\folder1\example.txt" "c:\folder2\"
0
 

Author Comment

by:dshrenik
ID: 35107722
xcopy seems to work fine (just "copy" didn't)
It asks me if I want to overwrite the file. How can I make it overwrite by default?

Thanks!
0
 
LVL 6

Assisted Solution

by:LHT_ST
LHT_ST earned 125 total points
ID: 35107733
add /y on the end

if you open up a command prompt and type xcopy /?

it will list all the switches you can add to the command to enable things like no prompting etc.
0
 

Author Comment

by:dshrenik
ID: 35107740
It now says "Access denied". Could it be because I'm copying into "Program Files (x86)"?
0
 
LVL 5

Expert Comment

by:NotVeryFat
ID: 35107765
Possibly. You'll need to check the permissions on the folder you're copying into, that the account you're using has at least "write" privileges.
0
 

Author Comment

by:dshrenik
ID: 35107769
Well, I'm the administrator on the computer. I think it works if I right click on command prompt and say "Run as admin". Can i do something similar with a batch file?
0
 
LVL 5

Expert Comment

by:NotVeryFat
ID: 35107793
You can either right click Command Prompt and 'Run As' or right click your batch file and 'Run As'
0
 

Author Comment

by:dshrenik
ID: 35107802
Is it possible to do that by default, on a simple double click of the .bat file?
0
 
LVL 5

Assisted Solution

by:NotVeryFat
NotVeryFat earned 375 total points
ID: 35107834
You can use third party software to run batch files as another user (ie cpau.exe) or you can create a scheduled task and tell it to run the process as a different user. Then just run the scheduled task rather than double clicking the batch file...

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hosts.ini- can it be made to block all websites? 14 81
How to make Telix to run under newer PC with MSDOS floppy diskette ? 13 77
file size 9 51
Date variable in batch file 2 28
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question