2011-09-23 20 views
33

Bir dosyayı yeni bir konuma kopyalamaya çalışıyorum, dizin yapısını koruyorum.Kopyala-Öğesi hedef dizin yapısını oluşturmalı mı?

$source = "c:\some\path\to\a\file.txt" 
destination = "c:\a\more\different\path\to\the\file.txt" 

Copy-Item $source $destination -Force -Recurse 

Ama olsun bir DirectoryNotFoundException:

Copy-Item : Could not find a part of the path 'c:\a\more\different\path\to\the\file.txt' 

cevap

66

kaynak bir dizin ise -recurse seçenek yalnızca bir hedef klasör yapısını oluşturur. Kaynak bir dosya olduğunda, Kopyala Öğesi hedefin zaten var olan bir dosya veya dizin olmasını bekler. İşte bu konuda çalışabileceğiniz birkaç yol var.

Seçenek 1: Kopya dizinleri yerine dosyaları

$source = "c:\some\path\to\a\dir"; $destination = "c:\a\different\dir" 
# No -force is required here, -recurse alone will do 
Copy-Item $source $destination -Recurse 

Seçenek 2: 'Dokunma' dosyası ilk ve sonra o

$source = "c:\some\path\to\a\file.txt"; $destination = "c:\a\different\file.txt" 
# Create the folder structure and empty destination file, similar to 
# the Unix 'touch' command 
New-Item -ItemType File -Path $destination -Force 
Copy-Item $source $destination -Force 
+0

Çok teşekkür ederim! –

+3

Teşekkürler Shay. Dokunma yöntemi, klasörler arasında yinelemeli bir şekilde gitmeye gerek yok. – heedfull

+1

İlginç, "dokunma", dizin ve dosyayı oluşturur ve dosya bir satır dosyasıdır. Eğer dosyayı not defterinde açarsanız (uzantı ne olursa olsun) ... dosya ~ orijinal adı tam adı var ... (metin?) dosyasının 1. satırı olarak. Daha sonra kopya kalemi tüm klasörlerin doğru şekilde var olduğundan çok mutlu. TEŞEKKÜRLER! Cevabınızı, buradaki sorumun cevabı olarak genişletdim: http: // stackoverflow.com/questions/42818014/powershell-copy-files-with-a-blacklist-exclude-ve-a-whitelist-include/42819089 # 42819089 – granadaCoder

2

üzerine Etrafa kazma bulunmuştur Bu soruna bir çok çözüm, hepsi sadece bir düz kopya-öğe komutuyla bir değişiklik değil. Bu soruların bir kısmını PS 3.0 öncesi verin, yanıtlar yanlış değil ama powershell 3.0 kullanarak nihayet bunu kopya öğesi için -Container anahtarını kullanarak gerçekleştirebiliyordum.

Copy-Item $from $to -Recurse -Container 

bu

i ran testi, hiç hata oldu ve hedef klasör aynı klasör yapısını temsil etti. itibaren PS3.0 ile

New-Item -ItemType dir -Name test_copy 
New-Item -ItemType dir -Name test_copy\folder1 
New-Item -ItemType file -Name test_copy\folder1\test.txt 
#NOTE: with no \ at the end of the destination the file is created in the root of the destination, does not create the folder1 container 
#Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2 -Recurse -Container 

#if the destination does not exists this created the matching folder structure and file with no errors 
Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2\ -Recurse -Container 
+0

Bu [ne ** - Container ** yapar] (https: // stackoverflow.com/a/129385/3273963). Olanlar için yanlış sebeplerin var. -Recurse hedef klasör oluşturma neden olur. Fark "test_copy2" olmadığı zaman, test_copy içeriğinin testi (kopyalanamaz) test_copy2 olur. Test_copy'de başka bir klasör oluşturmaya çalışın, daha sonra test_copy2 oluşturmadan kopya öğesini gerçekleştirin ve hata ile sonuçlanacaksınız. Ve sonunda "\" kullanıp kullanmadığın da önemli değil, -Container her zaman $ true ve burada üstlenilen farklı bir işleve sahip. – papo

4

Alternatif olarak, sadece, örneğin, bir "kukla" dosyası oluşturmak zorunda kalmadan, doğrudan hedef klasör oluşturmak için Yeni-Öğe kullanabilirsiniz ...

New-Item -Type dir \\target\1\2\3\4\5 

... mutlulukla bakılmaksızın zaten var ne kadarının 1 \ 2 \ 3 \ 4 \ 5 yapının \ \\ hedef yaratacaktır.

1

Windows 7'de tek bir klasörde dosyaları yeniden adlandırmak ve varolmayan klasörlere kopyalamak istedim.

aşağıdaki PowerShell Test-Item, New-Item için bir sargı olarak bir Copy-New-Item işlevi tanımlar komut, ve Copy-Item davranırlar kullanılır:
function Copy-New-Item { 
    $SourceFilePath = $args[0] 
    $DestinationFilePath = $args[1] 

    If (-not (Test-Path $DestinationFilePath)) { 
    New-Item -ItemType File -Path $DestinationFilePath -Force 
    } 
    Copy-Item -Path $SourceFilePath -Destination $DestinationFilePath 
} 

Copy-New-Item schema_mml3_mathml3_rnc schema\mml3\mathml3.rnc 
# More of the same... 
Copy-New-Item schema_svg11_svg_animation_rnc schema\svg11\svg-animation.rnc 
# More of the same... 
Copy-New-Item schema_html5_assertions_sch schema\html5\assertions.sch 
# More of the same... 

(bu durumda, bu Not kaynak dosya isimleri hiçbir bilgisi dosya uzantısı.)

Hedef dosya yolu yoksa, bu dosya, dosya yolunda varolmayan dizinlerin oluşturulmasını zorlayarak boş bir dosya oluşturur. (Copy-Item tüm bunları tek başına yapabilirse, documentation'dan nasıl yapılacağını göremedim.)

İlgili konular