首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助通过Powershell创建基于文件名的文件夹

需要帮助通过Powershell创建基于文件名的文件夹
EN

Stack Overflow用户
提问于 2020-12-14 22:23:54
回答 1查看 90关注 0票数 0

我有过

代码语言:javascript
复制
\\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

我想创造

代码语言:javascript
复制
\\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脚本来创建文件夹并在其中移动文件。

到目前为止

代码语言:javascript
复制
$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在哪里可以调整它

EN

回答 1

Stack Overflow用户

发布于 2020-12-15 01:09:12

首先,永远不要运行破坏性的(新建,创建,重命名,移动,删除,更新,清除.)没有验证步骤的代码。

这就是你想要的吗?

代码语言:javascript
复制
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".
...
#>

如果文件夹已经存在

代码语言:javascript
复制
# 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".
#>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65297373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档