【发布时间】:2021-08-21 19:49:19
【问题描述】:
我需要一些帮助,我是 PowerShell 的新手,我正在尝试使用它来简化我的一些工作。我正在编写一个 PowerShell 脚本来从一个位置 (C:\Pictures\People\People) 复制 JPG 文件并将它们移动到一个新位置。
问题是我需要在这个新位置创建一个与 JPG 同名的文件夹,然后再创建一个与 JPG 同名的子文件夹。
所以我需要将图像从 C:\Pictures\People\People 移动到 C:\Pictures\JPG_Name\JPG_Name\'JPG_Image' ,我将调用 JPG_Image 到 C:\Pictures\JPG_Name\JPG_Name\'JPG_Image'
到目前为止,我发现并一直在处理这个问题:
$SourceFolder = "C:\Pictures\People\People"
$TargetFolder = "C:\Pictures\"
# Find all files matching *.JPG in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.jpg |
ForEach-Object {
$ChildPath = Join-Path -Path $_.Name.Replace('.jpg','') -ChildPath $_.Name
[System.IO.FileInfo]$Destination = Join-Path -Path $TargetFolder -ChildPath $ChildPath
# Create the directory if it doesn't already exits
if( -not ( Test-Path -Path $Destination.Directory.FullName ) ){
New-Item -ItemType Directory -Path $Destination.Directory.FullName
}
Copy-Item -Path $_.FullName -Destination $Destination.FullName
}
【问题讨论】:
-
你的问题是什么?代码乍一看还不错,当然你可以用另一种方式解决问题
-
问题是它只创建一个文件夹而不是两个。因为我需要它创建的原始文件夹中的子文件夹。
标签: powershell