Old School Copy Command Question

Quick question as im a little rusty on remembering how to do this,

Im basically trying to copy a data file from a usb memory stick to a folder on a pc, i know the destination location will never change but the source location of the file may change that im copying from.


copy c:\test.bat c:\windows\test.bat

granted this would copy the file from the root to windows folder.

How do i do this from an unknown source or one that could change drive lettrers? such as a usb stick.

seem to remember %file% but i may be wrong? lol been a while

copy ?:\test.bat c:\windows\test.bat

the ? source part would change or is unknown

can i copy from local dir that the main batch file is in somehow?

Cheers all


Who is Participating?

Improve company productivity with a Business Account.Sign Up

Krzysztof PytkoConnect With a Mentor Senior Active Directory EngineerCommented:
if you have space in path, yu have to use quotas ""


"C:\ProgramData\Malwarebytes\Malwarebytes' Anti-Malware\"
For as far as i know it is not possible to do this using a batch file.. You could off course go to the directory in your bacth file uising "CD c:\windows\" and then use the copy file.

If you would like to have input boxes in which you can locate a specific destination you should be looking at a vbs script (bear in mind my vbs knowledge is just margable :)) Else, if you just put in a * or ? or anything like that it will only give you bakc something like "host file unknown"
You could try using Subst to substitute a folder and path into a predetermined drive letter.
So let's say you write your fixed script like this:
     copy  z:\test.bat c:\windows\test.bat
and then before running it, you would just "assign" z: to the desired folder, for example:
   subst  z:  c:\today_path\1
to cancel the assignment, use
  subst z:  /d

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

By the way, you may want to check out xcopy, which is more powerful than copy
For help, simply run
xcopy  /?
And if you're feeling particularly adventurous, and your OS supports it, try Googling "Robust file Copy" a.k.a. robocopy
You cannot use a wildcard for the drive letters.

You can use environment variables, for example %USERPROFILE% always point to the current user's profile, and %SYSTEMROOT% always points to the Windows root directory. That way you don't need to know if it's C:\WINDOWS, or for example D:\WINDOWS.

One way you could get something like that to work with a removable device, such as an USB stick, would be to place an autorun batch file on its root (assuming you haven't disabled autoruns).

When you insert the stick, the autorun file will run, and its working directory will be the root of the USB file system. You can use that to setup a new environment variable, such as %USBROOT%, which you could then use in your copy command.

Krzysztof PytkoSenior Active Directory EngineerCommented:
I don't know exactly if I understood, so let's start

you want to specify firts location as variable and the second is hard coded, right?

in cmd or bat file write

copy %1:\test.bat c:\windows\test.bat

save as movefile.cmd

then from command-line run script with parameter i.ex (d-drive)

movefile.cmd d

it will copy from d:\test.bat to c:\windows\test.bat

as %1 you can also specify path  (but then you have to remove ":")

copy %1\test.bat c:\windows\test.bat

and run

movefile.cmd d:\myfolder

it will copy from d:\myfolder\test.bat to c:\windows\test.bat
Here's how to get the drive letter in VBscript, by the way:

Dim  oDrive
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
For Each oDrive In oFS.Drives
If oDrive.DriveType = 1 And oDrive.DriveLetter <> "A" Then
WScript.Echo oDrive.DriveLetter & ":\"
taologicAuthor Commented:
thanks for the reply, im trying to aim at more of a raw dos method rather than VB but many many thanks for your input ill note it incase i have to go down that route
And here's how to use a plain old batch file to set an environment variable. This is not so accurate, but wil work if you know the usual range of letters where the USB will be (eg F:\, G:\, H:\, I:\).

FIrst, make something in your USB drive that you can easily recognize. For example, a directory named MYDIR.

The autorun.bat would be like this:

@echo off
setLocal EnableDelayedExpansion

for %%a in (f g h i) do (
dir %%a:\MYDIR > nul
if not errorlevel 1 set USBDRIVE=%%a:
taologicAuthor Commented:
Ok, think ive got a slight issue going on.

What im trying to do is have a batch file to overwrite malwarebytes data file onto the infected pc. the command line i had in mind was,

copy rules.ref C:\ProgramData\Malwarebytes\Malwarebytes' Anti-Malware\

But .. this fails "The system cannot find the patch specified"

If i change the c:\ProgramData..... path to say C:\  it works fine. Im assuming from this the spaces in the destination im trying to use are not dos compliant or the length is not compliant.

Is there a way around this? I have seen % being used before as spaces but im not sure


So my issue is the destination path all along.  Ima
GO-87Connect With a Mentor Commented:
You could wrap the destination path in double quotes
Slow typing ...  :-)
taologicAuthor Commented:
yes with quotes either side works perfect. thanks all. Any ideas why the quotes work and the other method doesnt. cheers
taologicAuthor Commented:
oooh out of interest hows the best way to reverse this to copy from the destination to the local folder running the batch file?

Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
Because when you have spaces in the file name the copy command treats the space a delimiter for options.  Quotes indicate it is intended to be one parameter.

taologicAuthor Commented:
aha found it..

copy "C:\ProgramData\Malwarebytes\Malwarebytes' Anti-Malware\rules.ref"

i think :p
taologicAuthor Commented:
thanks lee makes sense now. blast from the past playing with command line batch files.
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
The one time you can specify a file name or path without quotes is when using CD - all other commands, when the path contains spaces, need to be in quotes.  It's just safer to ALWAYS use quotes.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.