我有过
\\192.168.1.2\Video\PodCasts\MyPodcast S01E01.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S01E02.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S01E02.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S02E01.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S02E02.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S02E03.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S03E01.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S03E02.mp4
\\192.168.1.2\Video\PodCasts\MyPodcast S03E03.mp4我想创造
\\192.168.1.2\Video\PodCasts\Podcasts S01
\\192.168.1.2\Video\PodCasts\Podcasts S02
\\192.168.1.2\Video\PodCasts\Podcasts S03我有一个PS脚本来创建文件夹并在其中移动文件。
到目前为止
$Show = Read-Host "Show"
cd "\\192.168.1.90\video\$Show"
Get-ChildItem -Filter "$Show S*.*" -File | ForEach-Object {
$series = $_.Name -replace '.*(S\d{2}).*', '$1'
# create the target path inside the same directory and create if needed
$destination = Join-Path -Path $_.DirectoryName -ChildPath $series
if (!(Test-Path -Path $destination -PathType Container)) {
$null = New-Item -Path $destination -ItemType Directory
}
# move the file to the new path
$_ | Move-Item -Destination $destination -Force
}这行得通
\192.168.1.2\视频\播客\S01\MyPodcast S01E01.mp4
我想调整一下
\192.168.1.2\视频\播客\MyPodcast S01\MyPodcast S01E01.mp4
我看不出$destination在哪里可以调整它
发布于 2020-12-15 01:09:12
首先,永远不要运行破坏性的(新建,创建,重命名,移动,删除,更新,清除.)没有验证步骤的代码。
这就是你想要的吗?
Clear-Host
@(
'D:\PodCasts\MyPodcast S01E01.mp4',
'D:\PodCasts\MyPodcast S01E02.mp4',
'D:\PodCasts\MyPodcast S01E02.mp4',
'D:\PodCasts\MyPodcast S02E01.mp4',
'D:\PodCasts\MyPodcast S02E02.mp4',
'D:\PodCasts\MyPodcast S02E03.mp4',
'D:\PodCasts\MyPodcast S03E01.mp4',
'D:\PodCasts\MyPodcast S03E02.mp4',
'D:\PodCasts\MyPodcast S03E03.mp4'
) |
ForEach {
$SourceFolder = [regex]::matches($PSItem, '.*\\PodCasts').Value
$DestinationFolder = [regex]::matches($PSItem, 'S0\d').Value
If (Test-Path -Path "$SourceFolder\$DestinationFolder")
{
Write-Verbose -Message "Processing $SourceFolder\$DestinationFolder" -verbose
Move-Item -Path $PSItem -Destination $destinationFolder -WhatIf
}
Else
{
Write-Warning -Message "
Attempt to move a file to nonexistent folder, failed.
Creating the resource: $SourceFolder\$DestinationFolder
"
New-Item $DestinationFolder -ItemType Directory -WhatIf
Move-Item -Path $PSItem -Destination $destinationFolder -WhatIf
}
}
# Results
<#
WARNING:
Attempt to move a file to nonexistent folder, failed.
Creating the resource: D:\PodCasts\S01
What if: Performing the operation "Create Directory" on target "Destination: D:\Scripts\S01".
What if: Performing the operation "Move File" on target "Item: D:\PodCasts\MyPodcast S01E01.mp4 Destination: D:\Scripts\S01".
...
#>如果文件夹已经存在
# Results
<#
VERBOSE: Processing D:\PodCasts\S01
What if: Performing the operation "Move File" on target "Item: D:\PodCasts\MyPodcast S01E01.mp4 Destination: D:\Scripts\S01".
VERBOSE: Processing D:\PodCasts\S01
What if: Performing the operation "Move File" on target "Item: D:\PodCasts\MyPodcast S01E02.mp4 Destination: D:\Scripts\S01".
...
WARNING:
Attempt to move a file to nonexistent folder, failed.
Creating the resource: D:\PodCasts\S02
What if: Performing the operation "Create Directory" on target "Destination: D:\Scripts\S02".
What if: Performing the operation "Move File" on target "Item: D:\PodCasts\MyPodcast S02E01.mp4 Destination: D:\Scripts\S02".
#>https://stackoverflow.com/questions/65297373
复制相似问题