如何通过管道将多个文件复制到Copy-Item并保持目录结构?

时间:2019-01-05 01:56:17

标签: powershell

我在D:\CopyStuff上有一个很大的目录,我想在$from = "D:\stuff" $to = "D:\CopyStuff" $files = Get-ChildItem -Recurse -Path $from -Include *.config, *.txt, *.ini 上创建它的副本,但是我只想获取具有特定扩展名的文件并保持文件夹结构

获取我想要的文件似乎很简单:

Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Container

但是,复制文件并保持结构更具挑战性。我可以使用for循环,但这似乎与Powershell的本质背道而驰。建议在https://stackoverflow.com/a/25780745/782880处这样做:

D:\CopyStuff

但是这会将文件复制到from keras.applications import VGG16 from keras.preprocessing.image import ImageDataGenerator from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.applications.vgg16 import preprocess_input from keras.applications.vgg16 import decode_predictions from keras.layers import Input, Flatten, Dense from keras.models import Model from keras import models from keras import layers from keras import optimizers import ssl import os import cv2 import numpy as np import matplotlib # Force matplotlib to not use any Xwindows backend matplotlib.use('Agg') import matplotlib.pyplot as plt # path to the training, validation, and testing directories train_directory = '/train' validation_directory = '/valid' test_directory = '/test' results_directory = '/results' number_of_training_samples = 1746 number_of_validation_samples = 108 number_of_test_samples = 510 batch_size = 20 ssl._create_default_https_context = ssl._create_unverified_context # get back the convolutional part of a VGG network trained on ImageNet conv_base = VGG16(weights='imagenet',include_top=False,input_shape=(512,512,3)) conv_base.summary() # preprocess the data # rescale images by the factor 1/255 train_data = ImageDataGenerator(rescale=1.0/255) validation_data = ImageDataGenerator(rescale=1.0/255) test_data = ImageDataGenerator(rescale=1.0/255) train_features = np.zeros(shape=(number_of_training_samples,16,16,512)) train_labels = np.zeros(shape=(number_of_training_samples)) train_generator = train_data.flow_from_directory( train_directory, target_size=(512,512), batch_size=batch_size, class_mode='binary', shuffle=True) i = 0 for inputs_batch, labels_batch in train_generator: features_batch = conv_base.predict(inputs_batch) train_features[i*batch_size:(i+1)*batch_size] = features_batch train_labels[i*batch_size:(i+1)*batch_size] = labels_batch i += 1 if i * batch_size >= number_of_training_samples: break train_features = np.reshape(train_features, (number_of_training_samples,16*16*512)) validation_features = np.zeros(shape=(number_of_validation_samples,16,16,512)) validation_labels = np.zeros(shape=(number_of_validation_samples)) validation_generator = validation_data.flow_from_directory( validation_directory, target_size=(512,512), batch_size=batch_size, class_mode='binary', shuffle=False) i = 0 for inputs_batch, labels_batch in validation_generator: features_batch = conv_base.predict(inputs_batch) validation_features[i*batch_size:(i+1)*batch_size] = features_batch validation_labels[i*batch_size:(i+1)*batch_size] = labels_batch i += 1 if i * batch_size >= number_of_validation_samples: break validation_features = np.reshape(validation_features, (number_of_validation_samples,16*16*512)) test_generator = test_data.flow_from_directory( test_directory, target_size=(512,512), batch_size=batch_size, class_mode='binary', shuffle=False) # define the Convolutional Neural Network (CNN) model model = models.Sequential() model.add(layers.Dense(1024,activation='relu',input_dim=16*16*512)) model.add(layers.Dense(1,activation='sigmoid')) # compile the model model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=0.01), metrics=['acc']) # fit the model to the data history = model.fit(train_features, train_labels, epochs=1, batch_size=batch_size, validation_data=(validation_features,validation_labels)) # save the model model.save('benign_and_melanoma_from_scratch.h5') # generate accuracy and loss curves for the training process (history of accuracy and loss) acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] number_of_epochs = range(1,len(acc)+1) plt.plot(number_of_epochs, acc, 'r', label='Training accuracy') plt.plot(number_of_epochs, val_acc, 'g', label='Validation accuracy') plt.title('Training and validation accuracy') plt.legend() plt.savefig('accuracy.png') plt.close() plt.plot(number_of_epochs, loss, 'r', label='Training loss') plt.plot(number_of_epochs, val_loss, 'g', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.savefig('loss.png') # evaluate the model # predict classes for root, dirs, files in os.walk(test_directory): for file in files: img = cv2.imread(root + '/' + file) img = cv2.resize(img,(512,512),interpolation=cv2.INTER_AREA) img = np.expand_dims(img, axis=0) img = img/255.0 feature_value = conv_base.predict(img) feature_value= np.reshape(feature_value,(1,16,16,512)) img_class = model.predict_classes(feature_value) prediction = img_class[0] ,没有文件夹,更不用说我的原始结构了。我究竟做错了什么?我正在使用Powershell 5。

3 个答案:

答案 0 :(得分:2)

尝试:

$Source="C:\temp\test1"
$Dest="C:\temp\test2"
$EnableExt=".config", ".txt" , ".ini"

Get-ChildItem $Source -Recurse | % {

    $NewPath=$_.FullName.Replace($Source, $Dest)

    if ($_.psiscontainer)
    {
        New-Item -Path $NewPath -ItemType Directory -Force
    }
    elseif ($_.Extension -in $EnableExt)
    {
        Copy-Item $_.FullName $NewPath -Force
    }
}

答案 1 :(得分:1)

首先,Copy-Item可以自己完成,例如:

$fromFolder = "C:\Temp\Source"
$toFolder = "C:\Temp\Dest"
Copy-Item -Path $fromFolder -Destination $toFolder -Recurse -Filter *.txt

但是,您可能不喜欢结果:它将使文件夹“ Source”位于“ Dest”文件夹中,然后复制该结构。我认为,您需要将“源”文件夹中的相同文件/文件夹复制到“目标”文件夹中。好吧,这有点复杂,但是这里是:

$fromFolder = "C:\Temp\Source"
$toFolder = "C:\Temp\Dest"

Get-ChildItem -Path $fromFolder -Directory -Recurse | Select-Object FullName, @{N="NewPath";E={$_.FullName.Replace($fromFolder, $toFolder)}} | ForEach-Object { New-Item -Path $_.NewPath -ItemType "Directory" }
Get-ChildItem -Path $fromFolder -Include "*.txt" -Recurse | Select-Object FullName, @{N="NewPath";E={$_.FullName.Replace($fromFolder, $toFolder)}} | ForEach-Object { Copy-Item -Path $_.FullName -Destination $_.NewPath }

它首先复制文件夹结构,然后复制文件。

注意!我强烈建议仅使用绝对路径。否则,Replace方法可能会产生意外结果。

答案 2 :(得分:0)

注意:以下解决方案仅为包含与-Include过滤器匹配的文件的源文件夹创建类似的目标文件夹,而不为 all 所有源文件夹创建类似的目标文件夹。


通过将Get-ChildItem -Namedelay-bind script blocks结合使用,您可以摆脱单管道解决方案:

$from = 'D:\stuff'
$to = 'D:\CopyStuff'

Get-ChildItem -Name -Recurse -LiteralPath $from -Include *.config, *.txt, *.ini |
  Copy-Item `
    -LiteralPath { Join-Path $from $_ } `
    -Destination { New-Item -Type Directory -Force (Split-Path (Join-Path $to $_)) }
  • -Name发出相对于输入目录的路径相对作为字符串。

  • 延迟绑定脚本块{ Join-Path $from $_ }从每个相对输入路径构建 full 输入文件名。

  • 延迟绑定脚本块{ New-Item -Type Directory -Force (Split-Path (Join-Path $to $_)) }根据目标根路径和相对输入路径构建目标目录的完整路径,并根据需要使用预先存在(如果存在的话,则为{-Force)。