2011-09-23 19 views
33

Sto cercando di copiare un file in una nuova posizione, mantenendo la struttura delle directory.Se Copia-Elemento crea la struttura della directory di destinazione?

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

Copy-Item $source $destination -Force -Recurse 

ma ottengo un DirectoryNotFoundException:

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

risposta

66

L'opzione -recurse crea solo una struttura di cartella di destinazione se l'origine è una directory. Quando l'origine è un file, Copy-Item si aspetta che la destinazione sia un file o una directory già esistente. Ecco un paio di modi in cui puoi risolvere il problema.

Opzione 1: directory copia anziché di file

$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 

Opzione 2: 'Touch' il file e poi sovrascrittura è

$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

Grazie mille! –

+3

Grazie Shay. Il metodo touch è davvero a portata di mano, non è necessario ricorrere in modo ricorsivo alle cartelle. – heedfull

+1

Interessante, il "tocco" crea la directory e il file, e il file è un file a una riga .. se apri il file nel blocco note (indipendentemente dall'estensione) ... il file ha il ~ nome completo nome originale ... come riga1 del file (testo?). Quindi la copia è molto felice che tutte le cartelle esistano correttamente. GRAZIE! Ho ampliato la tua risposta per essere una risposta alla mia domanda qui: http: // StackOverflow.it/questions/42818014/powershell-copy-files-with-a-blacklist-exclude-and-a-whitelist-include/42819089 # 42819089 – granadaCoder

2

Ho scavato intorno e ho trovato un sacco di soluzioni a questo problema, essendo tutte alcune modifiche non solo un semplice comando copia-elemento. Concedi alcune di queste domande prima di PS 3.0 in modo che le risposte non siano sbagliate, ma usando PowerShell 3.0 sono finalmente riuscito a farlo usando l'opzione -Container per copia-elemento.

Copy-Item $from $to -Recurse -Container 

questo era il test che ho eseguito, nessun errore e la cartella di destinazione rappresentavano la stessa struttura di cartelle.

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

Questo non è [cosa ** - Container ** fa] (https: // stackoverflow.com/a/129385/3273963). Hai un ragionamento sbagliato per quello che sta succedendo. -Recurse causerà la creazione della cartella di destinazione. La differenza è quando "test_copy2" non esiste, il contenuto di test_copy diventerà (non verrà copiato) test_copy2. Prova a creare un'altra cartella in test_copy, quindi esegui copia-elemento senza creare test_copy2 e finirai con errori. Inoltre, non importa se si usa "\" alla fine, -Container è sempre $ true e ha una funzione diversa come qui assunta. – papo

4

In alternativa, con PS3.0 in poi, si può semplicemente utilizzare il nuovo elemento per creare direttamente la cartella di destinazione, senza dover creare un file "fittizio", ad esempio, ...

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

... sarà felicemente creare il \\ bersaglio \ 1 \ 2 \ 3 \ 4 \ 5 struttura indipendentemente quanta ne esiste già.

1

ho avuto i file in una singola cartella in Windows 7 che ho voluto rinominare e copiare cartelle inesistenti.

ho usato il seguente script PowerShell, che definisce una funzione Copy-New-Item come un wrapper per il Test-Item, New-Item, e Copy-Item cmdlet:

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... 

(Si noti che, in questo caso, i nomi dei file di origine non hanno estensione del file.)

Se il percorso del file di destinazione non esiste, la funzione crea un file vuoto in quel percorso, costringendo la creazione di tutte le directory inesistenti nel percorso del file. (Se Copy-Item può fare tutto da solo, non ho potuto vedere come farlo dal documentation.)

Problemi correlati