Copy File to Backup Directory Preserving Folder Structure

Copying files while preserving directory structure

I am perhaps making too broad an assumption here - but really the paths between encrypted and restored files are identical as long as you make the appropriate substitution between \\nas001\DATA\IQC and \\nas001\IQC_restore correct? If so....

You can simply take each file from the "known bad files" file (hname.txt?) and substitute the correct path for the destination using the String.Replace method:

$files = Get-Content C:\script\hname.txt
foreach ($file in $files) {
$src = $file.Replace("\\nas001\DATA\IQC","\\nas001\IQC_restore")
Copy-Item $src -Destination $file -Force
}

Or, to be even more brief:

$enc = '\\nas001\DATA\IQC'      # encrypted files share/folder
$good = '\\nas001\IQC_restore' # clean share/folder
gc c:\scripts\hname.txt | % { copy $_.replace($enc,$good) -dest $_ -force }

The secret decoder ring:

  • gc is an alias for the Get-Content cmdlet
  • % is an alias for the ForEach-Object cmdlet
  • $_ is a special variable inside a foreach scriptblock that represents the "current item" that is being passed in from the pipeline - in this case each line from the input file hname.txt

Copy files using python with complete folder structure

For each line in your file list, do the following:

for filePath in fileList:
destination = .join(['<BackupDrive>:\<BackupFolderName>', filePath[2:]])
os.makedirs(os.path.dirname(filePath), exist_ok=True)
shutil.copy(filePath , destination)

copy files while preserving directory structure in mac

I ended up using rsync -R to solve this.

Bash: Copy named files recursively, preserving folder structure

Have you tried using the --parents option? I don't know if OS X supports that, but that works on Linux.

cp --parents src/prog.js images/icon.jpg /tmp/package

If that doesn't work on OS X, try

rsync -R src/prog.js images/icon.jpg /tmp/package

as aif suggested.

Copy folder structure (without files) from one location to another

You could do something like:

find . -type d > dirs.txt

to create the list of directories, then

xargs mkdir -p < dirs.txt

to create the directories on the destination.

UNIX How to copy entire directory (subdirectories and files) from one location to another and retain permissions

from man cp

-p    Cause cp to preserve the following attributes of each source file
in the copy: modification time, access time, file flags, file mode,
ACL, user ID, and group ID, as allowed by permissions.

so cp -pr



Related Topics



Leave a reply



Submit