开发者

Batch script to take backup of all *.c and *.h files

开发者 https://www.devze.com 2022-12-15 09:01 出处:网络
I am new to MS batch programing. I want to copy files with matching regex to destination with same directory structure. If I use dir /b /s, I get full path of source then how can I get the relative p

I am new to MS batch programing.

I want to copy files with matching regex to destination with same directory structure. If I use dir /b /s, I get full path of source then how can I get the relative path from source path?

I want DOS based batch script eq开发者_如何转开发uivalent of something like this in bash script,

file_list=`find ./abc/test -name "*.c"`
for file_n in $file_list
do
    cp $file_n $targetdir/$file_n
done


Generally speaking source control is more appropriate than taking backups if source files...


I think the easier way, is to simply use pushd/popd:

pushd abs/test
file_list=`find . -name "*.[ch]"`
for file_n in $file_list
do
    cp $file_n $targetdir/$file_n
done
popd


You can use tar with a pipe to copy the directory hierarchy properly:

find . -name '*.[ch]' -exec tar cf - '{}' '+' | tar xf - -C $targetdir


tar cvf backup-`date +%Y%m%d`.tar `find . -name "*.[ch]" -print`

will create a dated tar file of the required files. That's possibly easier to manage.


with batch,

@echo off
for /F %%A in ('dir /b/s c:\test\*.c c:\test\*.h') do (
  echo copy "%%A" c:\destination
)

remove the echo to actual copy


The robocopy command will probably do what you want, but you didn't say what version of Windows you have (or is it really DOS rather than CMD?).

robocopy -s sourcedir destdir *.c *.h

or xcopy might work for you

xcopy /s *.c \destdir\
xcopy /s *.h \destdir\
0

精彩评论

暂无评论...
验证码 换一张
取 消