1.

Solve : COPY command help?

Answer»

I need to write a batch file that will copy an existing text file to a file name that I do not know (the user will specify the new file name). This is for a class project in which the user will choose this batch file from a menu in DOS.

I GUESS it would look something like this: COPY C:\*.TXT %1 (right?)

And then I have to write a batch file that will let someone delete that file. :-?Yes ... Something like that, if the user is going to specify the file in the command.

To specify the file to copy in the command you could use
Code: [Select]copy existing.txt %1 or you could get fancy and include error checking like
Code: [Select]if {%1}=={} echo You need to specify a file to copy ... quitting&goto :EOF
if exist %1 echo Warning! Specified file already exists ... quitting&goto :EOF
copy existing.txt %1
Or if you want to PROMPT the user for the file it is similar:
Code: [Select]set /p NewFile=Please enter a file name:
if exist %NewFile% echo Warning! Specified file already exists ... quitting&goto :EOF
copy existing.txt %NewFile%
To delete would be the same, but replace the "copy" line with "del %1" or "del %NewFile%" depending on your case.Thanks for the repsonse, but when I run the batch file using the COPY existing.txt %1 a message comes up saying "The file cannot be copied onto itself". I get the same message when I even use COPY c:\existing.txt %1. All that needs to happen is the user has to give "existing.txt" a new file name. What exactly does a % then a number value do? The %1 is the first parameter of a batch file, %2 is the second parameter, etc.

If your batch file is called mybatch.bat, and your existing file is called existing.txt then you would type in the following:

mybatch newname.txt

And the file existing.txt would be copied to newname.txt (based on my first EXAMPLE).



Discussion

No Comment Found