topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday March 19, 2024, 2:45 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
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\data
copy f:\data\pic.jpg C:\Documents and Settings\Administrator\Desktop

work in a bat file?

I don't have a usb drive to test atm. Thanks so much in advance.

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Yes

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
Ok, this is what I have then so far.

copy C:\TestCopy\myicon.ico C:\TestCopy\datafolder\
copy C:\TestCopy\myieshortcut C:\TestCopy\datafolder\

The problem is that the second line is a shortcut, an internet shortcut. I don't know the file extension to it as it's not showing even with file extension view enabled for folder options.

Is there an extension?
Also, how can I shorten the two lines? Is it possible to use one copy command to perform both copies?

Thanks for replying.

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Is there an extension?
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.

the command for could be what you are looking for:
@echo off
for %%f in (myicon.ico, myieshortcut) do copy C:\TestCopy\%%f C:\TestCopy\datafolder


Please post again if you need more hints or alternatives.

Greeting
Wolf


Schön wär's, wenn's schön wär!
« Last Edit: November 15, 2007, 06:26 PM by wolf.b »

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
You are the man my friend. It displayed the file extension. Thank you so much.

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

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.
« Last Edit: November 15, 2007, 07:14 PM by foolios »

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
It displayed the file extension. Thank you so much.
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


Greetings
Wolf

Schön wär's, wenn's schön wär!

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
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.


Greetings
Wolf
Schön wär's, wenn's schön wär!
« Last Edit: November 15, 2007, 07:35 PM by wolf.b »

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
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?

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
@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"

[/quote]

I will try, thank you.

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
YES!

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.

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #10 on: November 15, 2007, 07:46 PM »
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:
@echo off
for %%f in (myicon.ico, myieshortcut.url) do copy "\TestCopy\%%f" "%USERPROFILE%\Desktop\TestCopy\datafolder"

That will copy the two named files from the folder TestCopy in the root of the current drive to the folder TestCopy of your personal desktop.


Buddy list is great!
Have fun!
Schön wär's, wenn's schön wär!
« Last Edit: November 15, 2007, 07:47 PM by wolf.b »

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #11 on: November 15, 2007, 07:50 PM »
Is there anything I can do for you my friend?

Yes, please continue asking questions, that I can answer. That keeps me off the road. :)

Wolf
Schön wär's, wenn's schön wär!

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #12 on: November 15, 2007, 07:54 PM »
Ok, then I will do so. =)

Do you think there is a way that the batch file can take whatever name it's called and input that name to a copied file?

For example:
copy C:\TestCopy\myicon.ico C:\TestCopy\datafolder
rename C:\TestCopy\myshortcut.ico   \myshortcut.ico + batchfilename.bat

Let me explain further. Lets say we have a batch file called joe.bat
Can 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. =)
« Last Edit: November 15, 2007, 07:57 PM by foolios »

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #13 on: November 15, 2007, 08:10 PM »
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.

I go and search for the link now.
Schön wär's, wenn's schön wär!

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #14 on: November 15, 2007, 08:17 PM »
4DOS Revived!

Click on the button that says "Binaries" to download the zip file. For a start, you would only need one file from inside called 4dos.com.
Schön wär's, wenn's schön wär!

cranioscopical

  • Friend of the Site
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,776
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #15 on: November 15, 2007, 08:24 PM »
the powerful options that 4DOS gives you

I heartily second the recommendation to use 4DOS (better yet, 4NT).

foolios

  • Participant
  • Joined in 2007
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #16 on: November 15, 2007, 08:31 PM »
Yes, I am interested as long as it doesn't require the program to be installed in order for the scripts to work on any other computer: XP, Vista.

I will check the link out. THanks for the recommendation. I am interested in learning more.

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #17 on: November 15, 2007, 08:43 PM »
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.

Schön wär's, wenn's schön wär!
« Last Edit: November 16, 2007, 09:52 AM by wolf.b »

tinjaw

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,927
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #18 on: November 16, 2007, 03:25 PM »
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.

wolf.b, back to you wise teacher.

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #19 on: November 16, 2007, 05:29 PM »
Thanks tinjaw,

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:

:: TEST.BAT
:: by Wolf
::
@echo off

:: 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 batch
if not exist 4dos.com echo Sorry, 4DOS.COM not found.

goto End

:Continue
echo Command line: %0&
echo Command line parameters: %&
echo.
echo Batch file name:   %0
echo First parameter:   %1
echo Second parameter:  %2
echo Third parameter:   %3
echo Fourth parameter:  %4

:End


In case anybody wants to provide enlightenment, as to what the equivalent batch would be for cmd.exe, please post. In the meantime I do a bit of research myself.


Greetings
Wolf

Schön wär's, wenn's schön wär!

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #20 on: November 16, 2007, 06:04 PM »
A note on using two percent signs inside batch files, when using the for command. There are other similar cases as well:

The command line interpretor reads the batch file line by line. When it comes across a line that reads for example:
set PATH=%PATH%;C:\TOOLS
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:
set PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\TOOLS
Relacement (also called expansion of variables) is done first, execution is done second.

Now the for command is special, as you don't want the variable %f to be expanded, you want to assign something to it. If you were to write on the command line (DOS prompt) this:
for %x in (1 2 3) do echo %x
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:
for in (1 2 3) do echo
and that is missing the necessary name for the variable.

The solution is the special expansion of double percent signs: "%%" expands to (the shorter) "%", and the line:
for %%x in (1 2 3) do echo %%x
will expand to the correct line:
for %x in (1 2 3) do echo %x

Note that the name of the variable (here: x) is just added on after the expansion ot "%%", just like all the other letters.

Short: at the prompt use one, in batch files use two percent signs. (Applies only to special commands like for)


Greetings
Wolf
Schön wär's, wenn's schön wär!

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #21 on: November 16, 2007, 06:56 PM »
A note on writing comments inside batch files: (sorry, I am getting carried away)

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:

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

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:

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


Greetings
Wolf
Schön wär's, wenn's schön wär!
« Last Edit: November 16, 2007, 07:00 PM by wolf.b »

wolf.b

  • Member
  • Joined in 2007
  • **
  • Posts: 70
    • View Profile
    • Donate to Member
Re: Script/batch file that copies two files from a drive(usb) to a location
« Reply #22 on: November 19, 2007, 11:53 PM »
Only today I noticed that there is another question unanswered:

and the line: @echo off, that removes extra spaces right?
-reply#4

It toggles the "default batch echo" state OFF, which is otherwise ON. Now to understand what the default batch echo does, take the following code:

echo on
echo 1
echo.
echo 2

echo off
echo 3
echo
echo 4

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 on
echo 1      : this will put out the string "1"
echo.        : this will put out a empty line
echo 2      : this will put out the string "2"
               : this will put out a prompt (C:\TestCopy\>) or similar
echo off    : this will switch the default batch echo off, and put out a message about the new state: ECHO is off
echo 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 off
echo 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:

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

and get the same effect (avoid messy output).


Greetings
Wolf
Schön wär's, wenn's schön wär!

TheFormatter

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
foolios

Sorry, I'm a little late getting into this thread, but there is some help I can provide:

As for the question

How do I represent the current directory of the batch file I am using along with the files to be copied?

the lightly documented %CD% variable that has been in the cmd environment for the last few revs will do what you need.

If you start the batch or cmd file in the directory f:\data, then %CD% will equal f:\data, so instead of looking like this

copy f:\data\data.exe c:\windows\system32\data
copy f:\data\pic.jpg C:\Documents and Settings\Administrator\Desktop

your copy lines would look something like this:

copy %CD%\data.exe c:\windows\system32\data
copy %CD%\pic.jpg C:\Documents and Settings\Administrator\Desktop


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:\Scripts
At the command prompt, you change directories to F:\Data
From there you type in the command
       C:\Scripts\CopyMyFiles.cmd
The %CD% variable will contain the value F:\Data.

I hope that hasn't muddied the waters.


Jim

If you think the problem is bad now, wait until I solve it!

TheFormatter

  • Participant
  • Joined in 2006
  • *
  • default avatar
  • Posts: 2
    • View Profile
    • Donate to Member
wolf.b:

In regards to handling the command line arguments from the "regular" environment as opposed to the 4DOS environment, you can get the whole command line with this:

ECHO Command Line:  %0 %*

And the parameter list with this:

ECHO Command Line Parameters:  %*

Jim

Batches? We don't need no stinkin' batches!