How to Recursively Copy a Directory into Another and Replace Only the Files That Have Not Changed

How can I recursively copy a directory into another and replace only the files that have not changed?

It might, but any time the corresponding files in export and webroot have the same content but different modification times, you'd wind up performing an unnecessary copy operation. You'd probably get slightly smarter behavior from rsync:

rsync -pr ./export /path/to/webroot

Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details.

EDIT: with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option.

Recursively copy a set of files from one directory to another in PowerShell

Seen this before, and I don't know why PowerShell can't seem to get it right (IMHO). What I would do is more cumbersome but it works.

$Source = 'C:\Code\Trunk'
$Files = '*.csproj.user'
$Dest = 'C:\Code\F2'
Get-ChildItem $Source -Filter $Files -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Copy-Item $_.FullName -Destination $Path -Force
}

Batch file to copy directories recursively

Look into xcopy, which will recursively copy files and subdirectories.

There are examples, 2/3 down the page. Of particular use is:

To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:

xcopy a: b: /s /e

Copy all files and directories recursively

You can just be recursive. I just put this together - I might not have analysed your variables perfectly but it will give you an idea.

 private void btn_submit_Click(object sender, EventArgs e)
{
copy_stuff(txt_src.Text, txt_dest.Text);
}

private void copy_stuff(string srcFolder, string destFolder)
{
foreach (string zzz in Directory.GetFiles(srcFolder, "*.zzz", SearchOption.AllDirectories))
{
string modulePath = Directory.GetParent(zzz).FullName;
string moduleName = Directory.GetParent(zzz).Name;
Directory.CreateDirectory(destFolder + "\\" + moduleName);
foreach (string subFolders in Directory.GetDirectories(modulePath, "*", SearchOption.AllDirectories))
{
string dest = subFolders.Replace(modulePath, destFolder + "\\" + moduleName);
Directory.CreateDirectory(dest);
copy_stuff(subfolders, dest);
}
foreach (string allFiles in Directory.GetFiles(modulePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(allFiles, allFiles.Replace(modulePath, destFolder + "\\" + moduleName), true);
}
}
}

Why is Copy-Item overwriting destination files by default?

When in doubt, read the documentation.

-Force

Indicates that this cmdlet copies items that can't otherwise be changed, such as copying over a read-only file or alias.

The default behavior for Copy-Item is to replace existing items. The -Force switch is only to enforce replacement if for instance the destination file has the readonly attribute set.

You can use -Confirm to get prompted before Copy-Item performs the operation, or you can use -WhatIf to see what the cmdlet would do.

Copy files without overwrite

For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"

How to copy a directory structure but only include certain files (using windows batch files)

You don't mention if it has to be batch only, but if you can use ROBOCOPY, try this:

ROBOCOPY C:\Source C:\Destination data.zip info.txt /E

EDIT: Changed the /S parameter to /E to include empty folders.

Performing a recursive find and replace with sed only changes first file

I suppose the question about the 1st command is answered by Beta and let me answer the 2nd one.

Try to put -t (test) option to xargs and see how the command line
is expanded:

find . -name '*.txt' -print0 | xargs -0 -t sed -i '' '1 s/([^()]*)//g'

It will output something like:

sed -i '' 1 s/([^()]*)//g ./test1.txt ./test2.txt ./test3.txt ./test4.txt

The default behavior of xargs is to execute the specified command
(sed in this case) at once with the all arguments read from the standard
input.

In addition sed doesn't reset line numbering across multiple input files and the s command
above will be applied for the 1st file only.

You can change the behavior of xargs with -l1 option:

find . -name '*.txt' -print0 | xargs -0 -l1 -t sed '1 s/([^()]*)//g'

Output:

sed -i '' 1 s/([^()]*)//g ./test1.txt
sed -i '' 1 s/([^()]*)//g ./test2.txt
sed -i '' 1 s/([^()]*)//g ./test3.txt
sed -i '' 1 s/([^()]*)//g ./test4.txt

Then sed will work as expected.

How to Copy Sub Folders and files into New folder using Python

This code snippet should work:

import os
from distutils.dir_util import copy_tree

root_dir = 'path/to/your/rootdir'
try:
os.mkdir('path/to/your/rootdir/dirname')
except:
pass
for folder_name in os.listdir(root_dir):
path = root_dir + folder_name
for folder_name in os.listdir(path):
copy_tree(path + folder_name, 'path/to/your/rootdir/dirname')

just replace the directory names with the names you need



Related Topics



Leave a reply



Submit