Solved

Old School Copy Command Question

Posted on 2010-08-25
19
449 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:taologic
Comment Utility
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
Comment Utility
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
Comment Utility
You could wrap the destination path in double quotes
0
 
LVL 4

Expert Comment

by:GO-87
Comment Utility
Slow typing ...  :-)
0
 

Author Comment

by:taologic
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
aha found it..

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

i think :p
0
 

Author Comment

by:taologic
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 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

10 Experts available now in Live!

Get 1:1 Help Now