Copy / Move Maintaining Tree structure

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Copy / Move Maintaining Tree structure

#1 Post by drgt » 09 Nov 2021 02:12

How to copy a folder to target maintaining specified path tree (with the tree folders to be copied empty even if they contain files)

Example:
A dir command creates a text.log with an entry: "C:\user\desktop\FolderA\1\2"


It is asked to Copy / Move
C:\user\desktop\FolderA\1\2
to
C:\user\desktop\FolderB
NOT AS
C:\user\desktop\FolderB\2
BUT AS
C:\user\desktop\FolderB\1\2

C:\user\desktop\FolderA\1 might contain other subfolders (except "2") and / or files.
The C:\user\desktop\FolderB\1 should only contain the subfolder "2", not other folders or files.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Copy / Move Maintaining Tree structure

#2 Post by Squashman » 09 Nov 2021 08:08

Maybe use XCOPY with the /T and /E options.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Copy / Move Maintaining Tree structure

#3 Post by drgt » 09 Nov 2021 22:31

xcopy "C:\user\desktop\FolderA\1\2" "C:\user\desktop\FolderB" /t /e copies nothing.

xcopy "C:\user\desktop\FolderA\1\" "C:\user\desktop\FolderB" /t /e copies folder 2 to target, not 1\2.

xcopy "C:\user\desktop\FolderA\" "C:\user\desktop\FolderB" /t /e copies unwanted subfolders of folderA too.

Remember we have to work with the "text.log" entries.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Copy / Move Maintaining Tree structure

#4 Post by drgt » 10 Nov 2021 00:15

The problem with this, I think, is that it must be specified where to start copying to.
I mean:

Does the system create <target path> append <source path> in which case the result will be
C:\user\desktop\FolderB\user\desktop\FolderA\1\2
That is not the desired result.

So, in addition to the "text.log" entry, in the sought batch it must be specified HOW MANY LEVELS to truncate from the entries path and append the rest.
In our case, truncate the "\user\desktop\FolderA" and create "1\2" under target.
In our case: C:\user\desktop\FolderB\1\2"

Post Reply