ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE. Software > Coding Snacks

Script/batch file that copies two files from a drive(usb) to a location

<< < (2/6) > >>

It displayed the file extension. Thank you so much.

--- End quote ---
My pleasure.

At the command prompt you can type: help for > help.txt and study the help text file that appears in the current directory. But it is awful to read. My interpretation would be:

for %%f does assign a value to an environment variable called %f. The value will be the string "myicon.ico" in the first iteration, and "myieshortcut.url" in the second iteration. Those values are found by the for command inside the set that you give it after the keyword in between the round brackets here: in (myicon.ico, myieshortcut.url). I took the liberty to assume that the extension actually is url. For every value the for command finds inside the "set", it creates a command that looks like the rest of the commandline (after the keyword do) and the expression %%f is substituted with the current value of the environment variable. So it does in this particular case the following two commands like this:

--- ---copy C:\TestCopy\myicon.ico C:\TestCopy\datafolder
copy C:\TestCopy\myieshortcut.url C:\TestCopy\datafolder
The syntax in a easy (not entirely correct) version is: for xxx in (yyy) do zzz
This is just to illustrate the position of the necessary keywords.

For studying if your intended lines are correctly created, you could use the echo command like this:

--- ---@echo off
for %%f in (myicon.ico, myieshortcut) do echo copy C:\TestCopy\%%f C:\TestCopy\datafolder


Only after I wrote my last reply, I noticed your edits.

Try this:

--- ---@echo off
for %%f in (myicon.ico, myieshortcut) do copy "C:\Documents and Settings\All Users\Desktop\TestCopy\%%f" "C:\Documents and Settings\All Users\Desktop\TestCopy\datafolder"

The answer to spaces in the file name or folder name is double quotes.


Most excellent, thanks for the detailed explanation.

I have two more questions;
How do I represent spaces in a directory that leads to the file?
How do I represent the current directory of the batch file I am using along with the files to be copied?

For example, if I had the files to be copied and the batch file in a usb thumb drive but that drive could change depending on what computer I put it in.
I was wondering if I could represent the root of the drive for the copy from.
So for our example:
copy \TestCopy\myicon.ico C:\TestCopy\datafolder
copy \TestCopy\myieshortcut.url C:\TestCopy\datafolder

Does that appear correct to you?


--- ---@echo off
for %%f in (myicon.ico, myieshortcut) do copy "C:\Documents and Settings\All Users\Desktop\TestCopy\%%f" "C:\Documents and Settings\All Users\Desktop\TestCopy\datafolder"


I will try, thank you.


It all worked. That was soo exciting!
Thank you so much my friend. I am going to take my newfound knowledge and try to apply it to some other ideas.

Is there anything I can do for you my friend?
I hope you don't mind my adding you to my buddy list.

Thank you sooo very much.


[0] Message Index

[#] Next page

[*] Previous page

Go to full version