Script to copy selected files from tree

January 12, 2011 at 00:25:34
Specs: Windows XP
My requirement is to copy selected files from src tree to destination maintaining the directory structure. My actual use case is as follows:

I start work on source code tree and do compilation and source code changes. At some logical point I want to take backup of the changed source code. However the compilation adds lot of additional files and simple copy of tree adds to greater disk space and more time. So I want to copy only the files present before compilation. I thought of following approach.

In order to list the files in the original source code tree (i.e. before compilation) dos command "dir /s" can be used. This command will list all the files in the directory as well as subdirectories. This list can be used to copy the files from the compiled source code tree.

I am able to list the files however I am finding problem to copy them. The first thing is to maintain directory structure the copy command can't create non existing directories. Also there is no method to specify all the listed paths from some txt file.

Please guide me on simple dos command based approach or batch file approach to realize the requirement.

Thanks
- Pavan


See More: Script to copy selected files from tree

Report •

#1
January 12, 2011 at 01:43:16
::===========================================
@echo off & setLocal EnableDELAYedeXpansion

xcopy /t /e c:\files f:\files\

dir/b/s/a-d c:\files > filelist

for /f "tokens=* delims= " %%a in (filelist) do (
echo copy "%%a" "f:%%~Pa"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
January 12, 2011 at 05:32:23
Wow!! This is really doing the job.

Only a thing is that the tree is copied on different drive. I am trying to copy the tree at the same level in different directory. e.g.

d:/dir1/dir2/src_tree
copied to
d:/dir1/dir2/src_tree_bkp_1

I will appreciate if you provide me hint regarding it.
Thanks


Report •

#3
January 13, 2011 at 04:26:08
Hello,

I modified the script as below in order to meet directory structure mentioned in my last comment.

::===========================================
::Adjust folder names and filelist name for specific tree copy
@echo off & setLocal EnableDELAYedeXpansion
xcopy /t /e /i /h src_tree src_tree_bkp

dir /s /b /a-d src_tree > filelist_src_tree

::chdir gives D:/dir1/dir2/dir3/dir4 Then token is 1 to 6 (i.e. MaxDirLvl+2 = 4+2 = 6)
::adjust token names accordingly in a given directory
for /f "usebackq tokens=1-5* delims=\ " %%A in ("filelist_src_tree") do (
copy "%%~fE\%%F" "%%~fA\src_tree_bkp\%%F"
)
::===========================================

But I think it is done in amateur fashion because I need to change token names depending on the folder depth (no of levels away from base drive).

Also I want to copy tree structure using directory list generated previous to compilation. Because xcopy in above script is going to copy directories created during compilation too, which is not needed.

Could anybody provide some inputs?

Regards
- Pavan


Report •

Related Solutions

#4
January 13, 2011 at 04:31:52
Hint: use the backslash as the path or folder separator.

d:\dir1\dir2\src_tree
copied to
d:\dir1\dir2\src_tree_bkp_1


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question