# Copy directory tree without repeating directory name

Discussion in 'Support' started by David Marcus, Nov 19, 2010.

1. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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]

2. ### rconn Administrator Staff Member

Joined:
May 14, 2008
Messages:
9,854
Likes Received:
83
Barring the DWIM parser, how could COPY possibly know?

3. ### samintz Scott Mintz

Joined:
May 20, 2008
Messages:
1,187
Likes Received:
11
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:

4. ### rconn Administrator Staff Member

Joined:
May 14, 2008
Messages:
9,854
Likes Received:
83
Ummmm -- and that would be easier than typing the destination directory???

5. ### Jim Cook

Joined:
May 20, 2008
Messages:
604
Likes Received:
0
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.

6. ### samintz Scott Mintz

Joined:
May 20, 2008
Messages:
1,187
Likes Received:
11
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

7. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

8. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
I don't follow. How does that help?

9. ### rconn Administrator Staff Member

Joined:
May 14, 2008
Messages:
9,854
Likes Received:
83
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 don't see a pressing need for a new command when it can be done with a trivial alias.

10. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

11. ### Steve Fabian

Joined:
May 20, 2008
Messages:
3,520
Likes Received:
4
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

12. ### Kachupp

Joined:
Aug 9, 2009
Messages:
133
Likes Received:
0
what ever bring me my beer how do people interpret stupidity make it work DWIM unless i make a mistake

13. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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?)

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.

14. ### Steve Fabian

Joined:
May 20, 2008
Messages:
3,520
Likes Received:
4
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

15. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
I didn't say "keyboard".

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.

16. ### vefatica

Joined:
May 20, 2008
Messages:
7,934
Likes Received:
30
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.

17. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
That's the point. If it is going to be more convenient than just typing the directory name twice, it should be robust, flexible, and intuitive. E.g., see the various comments in this thread.

18. ### Steve Fabian

Joined:
May 20, 2008
Messages:
3,520
Likes Received:
4
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

19. ### vefatica

Joined:
May 20, 2008
Messages:
7,934
Likes Received:
30
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).

20. ### rconn Administrator Staff Member

Joined:
May 14, 2008
Messages:
9,854
Likes Received:
83
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 ...)

21. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

22. ### rconn Administrator Staff Member

Joined:
May 14, 2008
Messages:
9,854
Likes Received:
83
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.

23. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

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.

24. ### samintz Scott Mintz

Joined:
May 20, 2008
Messages:
1,187
Likes Received:
11
COPY /s \foo\bar .

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

25. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
That doesn't work. It is the same as

copy /s \foo\bar\* .

26. ### samintz Scott Mintz

Joined:
May 20, 2008
Messages:
1,187
Likes Received:
11
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:

27. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

28. ### samintz Scott Mintz

Joined:
May 20, 2008
Messages:
1,187
Likes Received:
11
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:

29. ### David Marcus

Joined:
Jun 4, 2008
Messages:
648
Likes Received:
1
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.

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.

30. ### JohnQSmith

Joined:
Jan 19, 2011
Messages:
559
Likes Received:
7
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...
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.