Author
Topic: Script/batch file that copies two files from a drive(usb) to a location (Read 42480 times)

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

If I had a thumb drive and I wanted to make two files go to two different locations by just running the script.Would the dos commands like:copy f:\data\data.exe c:\windows\system32\datacopy f:\data\pic.jpg C:\Documents and Settings\Administrator\Desktop

maybe yes, maybe no.One of many ways to find out is checking with a different filemanager than explorer. I suggest the DOS prompt like this:Start>Run>cmd.exe (press enter)type cd C:\TestCopy (press enter)type dir myieshortcut.* (press enter)Look at the output of the dir command.

alternatively you could try and open the file in notepad like this:Start>Run>cmd.exe (press enter)type cd C:\TestCopy (press enter)type notepad myieshortcut (press enter)if notepad opens an empty file, then your original file has a still hidden extension.

What does that line do besides copy? what is for %%f in mean?and the line: @echo off, that removes extra spaces right?

thanks again

EDIT: it looks to me like:for %%f in (myicon.ico, myieshortcut)

sets a variable with those two file names in it. If so, that 's just awesome!

EDIT:for the location:C:\Documents and Settings\All Users\Desktop

I seem to remember that spaces couldn't be used, maybe that's why my file isn't copying to the desktop when I try it.Am I to put % or something in places where a directory name is a space when dealing with the windows file system?

THanks

EDIT: Yes, that's most definitely the problem I think. And I don't think % is the answer for spaces. I get the error incorrect syntax on the part where I am trying to copy to a directory with spaces.

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:

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\datafoldercopy \TestCopy\myieshortcut.url C:\TestCopy\datafolder

Yes, it appears correct.Make sure that the batch file is started from the USB device. Will you keep that batch file there?

another hint: in case you gave "Al lUsers" as an example to protect your privacy (well done!), and plan to write your batch file so that it copies to your personal desktop folder, you could use the build-in environment variable "USERPROFILE" like this:

Let me explain further. Lets say we have a batch file called joe.batCan we have joe appended to the name of the shortcut called myfaveshortcut via the very same batch file?So the name of the shortcut will get personalized to the name of a person on the batch file.So lets say I give the batch file to Cindy, then she would just rename the batch file to cindy.bat and that the name that would be appended to the shortcut's filename;myshortcut_cindy.url

I was hoping something like that could be done. You may not know or maybe it is not possible, but if you do know and it is I will definitely search for a way. =)

Yes, than can be done. I have to admit that I am not very good at the syntax of batch files written for cmd.exe. I am happy to try and find solutions for your needs. But I wonder if you would consider to use an alternative "command line interpreter". I am used to write batch files for 4DOS, which is open source and free. It used to cost money, but not anymore. I would be able to give sound advice in case you are interested. You would not even have to install it, just copy 4dos.com into the same directory as the batch file you are writing, and I can show you how to make use of the powerful options that 4DOS gives you.

So will it be cmd.exe or 4DOS? Please don't feel pushed to something that you don't want, be honest. It would just easier for me to give hints quickly.

No need to install it. We don't want to create dependencies. The batch files for 4DOS will work on many OS: all MS DOS versions since version 3.3 (1990[edit:1988]). All Windows versions since 3.0. As cranioscopical has pointed out correctly, the alternative 4NT is recommended for MS OS that are based on what they call "New Technology", such as Windows NT, Win2000, Win2003, WinXP, and Vista. The drawback is that you only get a time limited version for free. The full version is quite expensive. Everything that you write for 4DOS will be useful for 4NT as well. So I suggested 4DOS for a start and you can always change to 4NT later. At the moment I think for learning batch commands the free 4DOS will be good.

wolf.b is doing a good job as an instructor. I see one question has been unanswered. One about renaming. I will throw out a hint that batch files can take optional parameters. And a second hint - there is a reason why it is %%f and not %f. Keep that in mind when using your optional parameters in your batch file.

Yes , I should have specifically said in reply #13, that the renaming can be done, instead of just

... that can be done.

My reason for "breaking" the flow of discussion at that point was to admit that

eighther I have to go and study the syntax of cmd.exe (I really should do that!) and both of foolios and myself benefit from the discussion at a very slow rate (nothing wrong with that), or

we switch to an alternative command line interpreter.

In my humble opinion, it is easier to start learning batch programming for 4DOS than it is to learn it for cmd.exe. Let me admit that my opinion is mainly based on the fact that I never really bothered to tackle the second, because I thought why learn a batch "dialect" that does not give good help. I am very fond of the help system that came with MS-DOS 6.22 and 7.10. Only many years later I had first contact with cmd.exe, which I believe is also much more powerful than command.com. But secondary to lack of good help being available to me, and the fact that 4DOS did everything I wanted it to do, and I already knew the features, I ended up ignoring cmd.exe as a command line interpreter.

For now I just give a sample batch file that I think will be easy enough for beginners to look at, and that slowly introduces some of the power of 4DOS:

:: The following line will branch the flow of execution the the label called:: Continue, if the expression evaluation returns true. All MS DOS command:: line processors will evaluaye the condition as false because they treat:: both sides of the double equal signs as STRINGS. "1.0" and "01" are not:: the same. 4DOS has the ability to recognise the case here that on both:: sides of the double equal signs, we have numerical expressions, that:: evaluate to the same number.

if 1.0 == 01 goto Continue

:: default command line processor will call 4DOS to run the same batch as:: well, with added parameters on the command line. After 4DOS finishes, it:: will give control back to the default command line processor.if exist 4dos.com 4dos.com /c %0 this is test batchif not exist 4dos.com echo Sorry, 4DOS.COM not found.

then it will append the directory C:\TOOLS to the current path. This is achieved by doing a replacement before execution. When the line is read, the CLI will replace the expression %PATH% with the string that is stored inside the environment variable called PATH (usually a list of folders, separated by semicolons). Lets assume for the sake of easy examples that the PATH would be C:\WINDOWS;C:\WINDOWS\COMMAND at the beginning. Replacement leads to the following command being executed:

you would get what you want, because it is not inside a batch file and is correct syntax.The same line inside a batch file would produce a syntax error, secondary to expansion of %x to an empty string, resulting in the execution of:

the keyword "rem" is not universally useful for commenting out lines inside batch files. It really is a "do-nothing" command and it will be executed. This can lead to strange behavior to say the least. Take for example the following line inside a batch file:

Now let us assume that I want that line to be ignored (commented out), but I don't want to delete it, to save me remembering, with the option to include the line later again ... If I added the command rem in front of the line like so:

...rem echo Some text that will appear in the file test.txt > test.txt...

I will get this behavior: the command "rem" with the parameters "echo Some text that will appear in the file test.txt" will be executed and do nothing, that is: it will produce no output. But that empty output will still be redirected to the file test.txt. It is the redirection (">") that will now create an empty file test.txt. The danger here is that redirection does not ask for confirmation when overwriting an existing file. I repeat: rem is a command, that will be executed and, when used for commenting out a line that contains redirection, the redirection will be carried out as well.

Solution:I use the double colon ("::") to comment out lines that I want to temporary be ignored, or for writing comments inside batch files. The behaviour of that can be regarded as such: Any line beginning with a colon is treated as a label. If the second character (first character of the label name) is an illegal character for a label name, then the entire line will be ignored. The following line inside a batch file will not produce an empty file called test.txt, and it will not overwrite an existing file:

What happens here is this:echo on : this line will switch the echo state ON (unnecessary, it is ON anyway), and put out a message about the new state: ECHO is onecho 1 : this will put out the string "1"echo. : this will put out a empty lineecho 2 : this will put out the string "2" : this will put out a prompt (C:\TestCopy\>) or similarecho off : this will switch the default batch echo off, and put out a message about the new state: ECHO is offecho 3 : this will put out the string "3"echo : this will put out a message about the state of the default batch echo: ECHO is offecho 4 : this will put out the string "4"

The output of that batch is very messy. Quite often I want to have empty lines in my code (for structuring) and I don't want to see the prompt echoed to the screen. That is why I have put "echo off" in the first line of the batch file in reply #3. If you remove the "@" sign from that batch you will still see the message "ECHO is off", because when the batch starts, the echo is on, and the first command will create some output. It switches the echo off for all the following lines, not for itself.

The "@" sign in front of any line switches the echo off for that particular line only. So in a batch with only one line you could write:

So it won't matter what drive or directory the batch file is executed from. Please keep in mind that the %CD% represents the current command line directory - you can call the batch/cmd file from anywhere using the full path to it, but %CD% will contain the current drive and directory.

The above phrase hurts my eyes, so an example might be more in order.

CopyMyFiles.cmd is stored in C:\ScriptsAt the command prompt, you change directories to F:\DataFrom there you type in the command C:\Scripts\CopyMyFiles.cmdThe %CD% variable will contain the value F:\Data.