Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For example I have folder:

F1\F11\some files...
F1\F12\some files...
F1\F12\F121\some files..

And I have another structure

F2\F21\some files...
F2\F22\some files...
F2\F22\folder\some files...

names of folders F11 - F21, F12 and F22 are equal.

In folders F21, F22 there are fewer files than in F11, F12.

I need to copy F2 to F1 recursively, but files, that will be replaced - should be saved in a backup folder:

Fbackup\F11\those files..
Fbackup\F12\those files..

This is needed to be able to make "undo" action of that copy. For example I've copied F2 in F1 recursively, than I copy Fbackup to F1 and I have original F1.

share|improve this question
    
Do you need a solution that does this using .cmd? With a Unix shell, I can do it in 5 lines but I'm not sure that's acceptable. –  Nicole Hamilton Feb 28 '13 at 16:01
    
@NicoleHamilton, I need this only in Windows. But, I'm just curious - I'm a linux user actually - how it'll be in bash? –  Innuendo Feb 28 '13 at 17:29
    
Looks like someone's already downvoted it (who knows why) but I've given you a couple alternatives below in C shell and bash. Hope this helps. –  Nicole Hamilton Mar 1 '13 at 1:54

1 Answer 1

If you're open to using a Unix shell, here are a couple alternatives using bash or my own C shell.

C shell solution

Here's how you might do it with Hamilton C shell, including the free version:

cd F2
foreach i (...\*)
   if ( -f $i ) then
      if ( -e F1\$i ) cp F1\$i Fbackup\$i
      cp $i F1\$i
   end
end

The ... notation is an indefinite directory wildcard that matches zero or more directory levels, whatever it takes to make the rest of the pattern match. ...\* matches every file or directory in the F2 directory tree. The -e operator tests for existence and the -f operator tests for files.

This code fragment assumes there are no hidden files or directories of concern. It also assumes that every directory in F2 (even if not every file) already exists in F1 and Fbackup.

If hidden files and missing directories need to be considered, the solution would be to set nonohidden = 1 and mkdir any missing directories:

cd F2
@ nonohidden = 1
foreach i (...\*)
   if ( -f $i ) then
      if ( -e F1\$i ) then
         if ( ! -e Fbackup\$i:h ) mkdir -r Fbackup\$i:h
         cp F1\$i Fbackup\$i
      end
      if ( ! -e F1\$i:h ) mkdir -r F1\$i:h
      cp $i F1\$i
   end
end

bash solution

If you prefer Cygwin bash, you'll need to use find in lieu of the ... wildcard to treewalk the F2 directory. Here's what that solution might look like. (I'm not a bash expert so it may be possible to improve this.)

First, create a script, call it backupscript.sh:

#!/usr/bin/bash
if [ -f "$@" ]; then
   if [ -e F1/"$@" ]; then
      cp F1/"$@" Fbackup/"$@"
   fi
   head=$(dirname "$@")
   if [ ! -e F1/"$head" ]; then
      mkdir -p F1/"$head"
   fi
   cp "$@" F1/"$@"
end

Then run as follows:

cd F2
find . -type f  | xargs -I "{}" bash -c "backupscript.sh {}"
share|improve this answer
    
Thanks for big response. My bash knowledge is not enough to completely understand the script, but in general I understood how it's realized. –  Innuendo Mar 1 '13 at 17:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.