Solved

Old School Copy Command Question

Posted on 2010-08-25
19
450 Views
Last Modified: 2013-11-09
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.

eg:

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

Adam


0
Comment
Question by:taologic
  • 6
  • 5
  • 3
  • +3
19 Comments
 
LVL 23

Expert Comment

by:rhandels
ID: 33519189
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"
0
 
LVL 4

Expert Comment

by:GO-87
ID: 33519200
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

0
 
LVL 4

Expert Comment

by:GO-87
ID: 33519205
By the way, you may want to check out xcopy, which is more powerful than copy
For help, simply run
xcopy  /?
0
 
LVL 4

Expert Comment

by:GO-87
ID: 33519231
And if you're feeling particularly adventurous, and your OS supports it, try Googling "Robust file Copy" a.k.a. robocopy
0
 

Expert Comment

by:elirtai
ID: 33519232
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.

0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 33519294
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
0
 

Expert Comment

by:elirtai
ID: 33519295
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 & ":\"
Next
0
 

Author Comment

by:taologic
ID: 33519312
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
0
 

Expert Comment

by:elirtai
ID: 33519323
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:
)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:taologic
ID: 33519365
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

Adam

So my issue is the destination path all along.  Ima
0
 
LVL 39

Accepted Solution

by:
Krzysztof Pytko earned 32 total points
ID: 33519396
if you have space in path, yu have to use quotas ""

i.e

"C:\ProgramData\Malwarebytes\Malwarebytes' Anti-Malware\"
0
 
LVL 4

Assisted Solution

by:GO-87
GO-87 earned 31 total points
ID: 33519403
You could wrap the destination path in double quotes
0
 
LVL 4

Expert Comment

by:GO-87
ID: 33519405
Slow typing ...  :-)
0
 

Author Comment

by:taologic
ID: 33519415
yes with quotes either side works perfect. thanks all. Any ideas why the quotes work and the other method doesnt. cheers
0
 

Author Comment

by:taologic
ID: 33519462
oooh out of interest hows the best way to reverse this to copy from the destination to the local folder running the batch file?

0
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 62 total points
ID: 33519463
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.

0
 

Author Comment

by:taologic
ID: 33519473
aha found it..

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

i think :p
0
 

Author Comment

by:taologic
ID: 33519478
thanks lee makes sense now. blast from the past playing with command line batch files.
0
 
LVL 95

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 62 total points
ID: 33519497
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now