Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Copy directory tree without repeating directory name

Jun
768
6
Is there a way to copy a directory tree to another location without having to type the name of the directory twice on the command line? E.g., suppose Bar is a directory in Foo1 and I want to copy it and its contents to Foo2 (which does not contain a directory named "Bar"). I can do

copy /s \Foo1\Bar\* \Foo2\Bar\

But, this requires me to type "Bar" twice.

TCC 12.00.36 Windows Vista [Version 6.0.6002]
 
Is there a way to copy a directory tree to another location without having to type the name of the directory twice on the command line? E.g., suppose Bar is a directory in Foo1 and I want to copy it and its contents to Foo2 (which does not contain a directory named "Bar"). I can do

copy /s \Foo1\Bar\* \Foo2\Bar\

But, this requires me to type "Bar" twice.

Barring the DWIM parser, how could COPY possibly know?
 
Another example that could benefit from regex expressions in the
destination name!

copy /s ::\Foo1\(Bar\*) ::\Foo2\\1

David - the above syntax is not legal and is not supported in the current
versions of TCC.

The only way I know of to not have to type the name is to already be in
the destination directory.

Or alternatively, if this is something you do regularly, then create an
alias or script to do it for you.

ALIAS COPYDIR=`copy /s %1 %2\%@name[%1]`

copydir \foo\bar \foo2

-Scott

rconn <> wrote on 11/19/2010 04:23:50 PM:


> ---Quote (Originally by David Marcus)---
> Is there a way to copy a directory tree to another location without
> having to type the name of the directory twice on the command line?
> E.g., suppose Bar is a directory in Foo1 and I want to copy it and
> its contents to Foo2 (which does not contain a directory named
> "Bar"). I can do
>
> copy /s \Foo1\Bar\* \Foo2\Bar\
>
> But, this requires me to type "Bar" twice.
> ---End Quote---
>
> Barring the DWIM parser, how could COPY possibly know?
>
>
>
>
 
Well, it would if the destination directory name were long and/or
complicated. At least it'd be more reliable.

I like the alias with %@name solution best so far.

On Fri, Nov 19, 2010 at 14:28, rconn <> wrote:


> ---Quote (Originally by samintz)---
> Another example that could benefit from regex expressions in the
> destination name!
>
> copy /s ::\Foo1\(Bar\*) ::\Foo2\\1
> ---End Quote---
>
> Ummmm -- and that would be easier than typing the destination directory???
>
>
>
>
>



--
Jim Cook
2010 Sundays: 4/4, 6/6, 8/8, 10/10, 12/12 and 5/9, 9/5, 7/11, 11/7.
Next year they're Monday.
 
You could also take advantage of the filename completion editing features.

copy /s \Foo\B[tab]

until the directory is selected, then press F12 to copy that whole name and then just edit the piece you want to change.

-Scott

Well, it would if the destination directory name were long and/or
complicated. At least it'd be more reliable.

I like the alias with %@name solution best so far.

On Fri, Nov 19, 2010 at 14:28, rconn <> wrote:






--
Jim Cook
2010 Sundays: 4/4, 6/6, 8/8, 10/10, 12/12 and 5/9, 9/5, 7/11, 11/7.
Next year they're Monday.
 
Barring the DWIM parser, how could COPY possibly know?

One way would be the same way MOVE knows: "When you specify a a single subdirectory source and a single subdirectory target, the source directory tree will be moved to a subdirectory of the target directory. If the source is a subdirectory and the target doesn't exist, the target subdirectory will be created and the source tree moved to it." (Help typo: "a a single" should be "a single".)

I think if I do

copy foo .

where foo is a directory, then the directory should be copied, not its contents. I suppose doing this would break compatibility with something. I'd be happy with a new command, e.g., COPYDIR.

I used to have a CopyDir btm file that I wrote. I forget why I stopped using it (maybe it had some problems). I'll try Scott's alias.
 
One way would be the same way MOVE knows: "When you specify a a single subdirectory source and a single subdirectory target, the source directory tree will be moved to a subdirectory of the target directory. If the source is a subdirectory and the target doesn't exist, the target subdirectory will be created and the source tree moved to it."

That's a ghastly kludge for some especially lame CMD.EXE MOVE syntax (and one that generates a continual stream of complaints).

Doing the same thing in COPY would break about a bazillion batch files and aliases.

I think if I do

copy foo .

where foo is a directory, then the directory should be copied, not its contents. I suppose doing this would break compatibility with something. I'd be happy with a new command, e.g., COPYDIR.

I used to have a CopyDir btm file that I wrote. I forget why I stopped using it (maybe it had some problems). I'll try Scott's alias.

I don't see a pressing need for a new command when it can be done with a trivial alias.
 
I used to have a CopyDir btm file that I wrote. I forget why I stopped using it (maybe it had some problems). I'll try Scott's alias.

Scott's alias isn't robust. E.g., you can't leave off the second parameter (to put the copy in the current directory). And, appending a backslash to the first parameter messes it up.
 
David Marcus:
Scott's alias isn't robust. E.g., you can't leave off the second parameter (to put the copy in the current directory). And, appending a backslash to the first parameter messes it up.

Other minor issues with the alias:
- switching from @NAME to @FILENAME cures the issue of a subdirectory name with embedded period (.)
- surrounding the source and the target with @QUOTE allows names with special characters and spaces

I usually use the alternate method, also suggested by Scott, file completion. Use file completion twice for the source; go back in the command line to the beginning of the second copy of the source; insert a space; use filecompletion in front of the 2nd space for the base of the target; delete the extra space and the unnecessary part of the target. Takes less time to do than to explain!
--
HTH, Steve
 
what ever bring me my beer how do people interpret stupidity make it work DWIM unless i make a mistake

>
> ---Quote (Originally by David Marcus)---
> Is there a way to copy a directory tree to another location without having to type the name of the
> directory twice on the command line? E.g., suppose Bar is a directory in Foo1 and I want to copy it
> and its contents to Foo2 (which does not contain a directory named "Bar"). I can do
>
> copy /s \Foo1\Bar\* \Foo2\Bar\
>
> But, this requires me to type "Bar" twice.
> ---End Quote---
>
> Barring the DWIM parser, how could COPY possibly know?
>
>
>
>
 
That's a ghastly kludge for some especially lame CMD.EXE MOVE syntax (and one that generates a continual stream of complaints).

The functionality isn't available from any other command. Moving directories around is still easier in Windows Explorer than in TCC. I'd like to be able to move directories around easily in TCC. Currently, some things can be done by REN, MOVE (using the kludge), and COPY. But, it is hard to remember which to use, what can be done, and what can't.

(What do people complain about?)

I don't see a pressing need for a new command when it can be done with a trivial alias.

Judging by the posts, people aren't using an alias. They are doing lots of editing of the command line. It seems it takes some work to create a robust alias.
 
David Marcus:
The functionality isn't available from any other command. Moving directories around is still easier in Windows Explorer than in TCC. I'd like to be able to move directories around easily in TCC. Currently, some things can be done by REN, MOVE (using the kludge), and COPY. But, it is hard to remember which to use, what can be done, and what can't.
You claim you can use your the keyboard easier in Windows Exploer than in TCC to move directories? I doubt it! As to how you move directories around, think of what is involved - if it is on the same volume, you can use RENAME to provide it a new path; otherwise you must use MOVE - which is a command that performs twe two actions actually required: COPY to the new location, than delete the original. MOVE can also be used when RENAME would be able to do the job.

(What do people complain about?)
I do not remember any other complaints about this particular issue by anyone else.


Quote:
Originally Posted by rconn
I don't see a pressing need for a new command when it can be done with a trivial alias.

Judging by the posts, people aren't using an alias. They are doing lots of editing of the command line. It seems it takes some work to create a robust alias.

The difficulty of creating a robust alias is not the reason I never created an alias (nor a batch program). It's because I very rarely do it. I have 1,515 aliases! Yes, there are probably well over 100 I never used...
--
Steve
 
You claim you can use your the keyboard easier in Windows Exploer than in TCC to move directories?

I didn't say "keyboard".

As to how you move directories around, think of what is involved - if it is on the same volume, you can use RENAME to provide it a new path; otherwise you must use MOVE - which is a command that performs twe two actions actually required: COPY to the new location, than delete the original. MOVE can also be used when RENAME would be able to do the job.

Except the syntax is limited (e.g., wild card support) or finicky (e.g., MOVE can only move one directory at a time) or nonexistent (as this thread has discussed). I end up reading the Help, using /n, trying a couple of things, then often giving up and using Windows Explorer. On occasion, I have written my own btm files, but they've been just as finicky.

I've been using JP Software products since the 4DOS days. And, I keep hoping for better support for renaming/moving/copying directories. It has gotten better over the years, but it seems it is always sort of an afterthought bolted on to the excellent support for working with files.
 
I don't understand the big deal. This alias seems to work OK. I'm sure it could be jazzed up some.

Code:
v:\> alias cpdir
if "%1" EQ "/?" .or. %# LT 2 (echo CPDIR dirname new_parent) else (copy /md /q /s /a: %1\* %2\%@filename[%1])

v:\> cpdir
CPDIR dirname new_parent

P.S. Why does COPY /S (as above) create error messages like these when a source directory is empty or contains only directories?

Code:
cpdir g:\Projects\4Utils v:\

TCC: (Sys) There are no more files.
 "G:\Projects\4Utils\release\foo\*"
TCC: (Sys) There are no more files.
 "G:\Projects\4Utils\x64\*"

They are not errors and I don't need to see those messages.
 
Code:
v:\> alias cpdir
if "%1" EQ "/?" .or. %# LT 2 (echo CPDIR dirname new_parent) else (copy /md /q /s /a: %1\* %2\%@filename[%1])

v:\> cpdir
CPDIR dirname new_parentP.S. Why does COPY /S (as above) create error messages like these when a source directory is empty or contains only directories?


Code:
cpdir g:\Projects\4Utils v:\

TCC: (Sys) There are no more files.
"G:\Projects\4Utils\release\foo\*"
TCC: (Sys) There are no more files.
"G:\Projects\4Utils\x64\*"They are not errors and I don't need to see those messages.

They are informational messages which you can suppress with the /Ne option.
--
HTH, Steve
 
On Sat, 20 Nov 2010 21:31:41 -0500, Steve Fábián <> wrote:

|Code:
|v:\> alias cpdir
|if "%1" EQ "/?" .or. %# LT 2 (echo CPDIR dirname new_parent) else (copy /md /q /s /a: %1\* %2\%@filename[%1])
|
|v:\> cpdir
|CPDIR dirname new_parent

P.S. Why does COPY /S (as above) create error messages like these when a source
directory is empty or contains only directories?
|
|
| Code:
|cpdir g:\Projects\4Utils v:\
|
|TCC: (Sys) There are no more files.
| "G:\Projects\4Utils\release\foo\*"
|TCC: (Sys) There are no more files.
| "G:\Projects\4Utils\x64\*"
|
|They are not errors and I don't need to see those messages.
|
| They are informational messages which you can suppress with the /Ne option.

Directories that are empty or contain only directories are common. I never want
to see those messages. I think they're just an artifact of FindFirstFile()
failing (for a good reason).
 
The functionality isn't available from any other command. Moving directories around is still easier in Windows Explorer than in TCC. I'd like to be able to move directories around easily in TCC. Currently, some things can be done by REN, MOVE (using the kludge), and COPY. But, it is hard to remember which to use, what can be done, and what can't.

(What do people complain about?)

People complain (and understandably so!) that the target directory they specify isn't the one that's actually used. (MOVE will put the files in a subdirectory of the target directory, which defies logic, but then that's CMD for you ...)
 
People complain (and understandably so!) that the target directory they specify isn't the one that's actually used. (MOVE will put the files in a subdirectory of the target directory, which defies logic, but then that's CMD for you ...)

The shoehorning into MOVE as a special case isn't logical, but the idea that you should be able to deal with directories the same way that you deal with files is logical. The problem is that currently a directory name like Foo is interpreted as Foo\*. So, there is no way to refer to the directories themselves.

For example,

copy /s * ..\Foo\

will copy all the directories in the current directory into Foo. But, I can't replace the * with the name of one of the directories to get just it copied. This isn't logical.
 
The shoehorning into MOVE as a special case isn't logical, but the idea that you should be able to deal with directories the same way that you deal with files is logical. The problem is that currently a directory name like Foo is interpreted as Foo\*. So, there is no way to refer to the directories themselves.

For example,

copy /s * ..\Foo\

will copy all the directories in the current directory into Foo. But, I can't replace the * with the name of one of the directories to get just it copied. This isn't logical.

Personally, I do find it logical, but logical isn't relevant here.

To change this would require rewriting EVERY file-handling command, and losing all compatibility with CMD (which behaves this way for everything *except* MOVE when both the source and the target are directories). It would also require all TCC users to rewrite all of their batch files and aliases that reference subdirectories.

I don't believe that would be a good solution.
 
Personally, I do find it logical

I'd be interested in understanding the logic. I usually think of a wild card as meaning find each thing that matches the wild card and do the command for each.

To change this would require rewriting EVERY file-handling command, and losing all compatibility with CMD (which behaves this way for everything *except* MOVE when both the source and the target are directories). It would also require all TCC users to rewrite all of their batch files and aliases that reference subdirectories.

I don't believe that would be a good solution.

I don't believe I ever suggested such a change. I've suggested new commands several times over the years. E.g., MOVEDIR, COPYDIR or MOVETREE, COPYTREE.
 
COPY /s \foo\bar .



David Marcus <> wrote on 11/19/2010 08:43:47 PM:


> ---Quote (Originally by samintz)---
> The only way I know of to not have to type the name is to already be
> in the destination directory.
> ---End Quote---
> I don't follow. How does that help?
>
>
>
>
 
I said it would work if you were in the destination directory when you
issued the copy command.

for example:

md /d \foo2\bar
copy /s \foo\bar .

-Scott

David Marcus <> wrote on 11/22/2010 10:49:28 AM:


> ---Quote (Originally by samintz)---
> COPY /s \foo\bar .
> ---End Quote---
> That doesn't work. It is the same as
>
> copy /s \foo\bar\* .
>
>
>
>
 
I said it would work if you were in the destination directory when you
issued the copy command.

for example:

md /d \foo2\bar
copy /s \foo\bar .

Yes, but you've had to type the directory name twice. All you've done is type it in two different commands. Your copy command doesn't copy a specified tree, it copies the contents of a directory.
 
We're picking at nits at this point. I'm aware of all that. My original
comment was that you had to be in the destination folder before issuing
the copy command in order to not have to type the name twice. I didn't
say it was an acceptable solution. I merely said it was the only solution
I was aware of - aside from custom aliases or script files.

What about using XCOPY instead? Does it do what you want?

What are your requirements for copying a tree?

treecopy src dest

where src is a directory and dest is a directory?

What if src is not a directory? What if dest is not a directory?

-Scott

David Marcus <> wrote on 11/22/2010 12:31:18 PM:


> ---Quote (Originally by samintz)---
> I said it would work if you were in the destination directory when you
> issued the copy command.
>
> for example:
>
> md /d \foo2\bar
> copy /s \foo\bar .
> ---End Quote---
> Yes, but you've had to type the directory name twice. All you've
> done is type it in two different commands. Your copy command doesn't
> copy a specified tree, it copies the contents of a directory.
>
>
>
>
 
What about using XCOPY instead? Does it do what you want?

Don't know. Haven't used it in a long time. I usually fiddle with the TCC copy, move, ren commands or use Windows Explorer.

What are your requirements for copying a tree?

treecopy src dest

where src is a directory and dest is a directory?

What if src is not a directory? What if dest is not a directory?

src should be one or more directories, optionally specified with wild cards. dest should be a directory. If multiple src's are listed, then the dest directory should be required; otherwise dest should be optional.

Basically, it should work like copy/move/ren do for files, but operate on directories.
 
I realize that this thread is four months old, but I just now read it after seeing a link to it in Feedback Suggestions under Copydir and Movedir commands.

Rex commented there...
I'm not sure I see what benefit this would have over the existing COPY / MOVE commands (perhaps with a simple alias to set the desired options). Can you expand on the idea a bit?

The perfect way, I found, to see what David is requesting is to fire up a bash shell.

I'm using a directory/file structure as follows...
Code:
test/
+--dir1/
|   +--file3
|   +--file4
+--dir2/
|   +--file5
|   +--file6
+--dir3/
|   +--file7
|   +--file8
+--file1
+--file2
Then we run the test commands (I chopped out a lot of the 'cp --help' output).
The 'cp -r dir1 dir2 dir3' is the command that does the work. All the rest are mainly just to show directory contents.
Code:
~/test $ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
...

~/test $ ls
dir1/  dir2/  dir3/  file1  file2

~/test $ ls dir1
file3  file4

~/test $ ls dir2
file5  file6

~/test $ ls dir3
file7  file8

~/test $ cp -r dir1 dir2 dir3

~/test $ ls dir3
dir1/  dir2/  file7  file8

~/test $
This leaves us with our final directory/file structure.
Code:
test/
+--dir1/
|   +--file3
|   +--file4
+--dir2/
|   +--file5
|   +--file6
+--dir3/
|   +--dir1/
|   |   +--file3
|   |   +--file4
|   +--dir2/
|   |   +--file5
|   |   +--file6
|   +--file7
|   +--file8
+--file1
+--file2
I'm going to have to agree with David on this one. This makes life much easier when copying directories to other locations. It never occurred to me before that when I was copying a directory to another location that I had to force it into the destination directory. I guess just reverted to dragging and dropping with Windows Explorer after my first couple of attempts failed and never looked back.
 

Similar threads

Replies
7
Views
2K
Back
Top