使用 Powershell 腳本讀取文本文件、創建者文件夾和復製文件

德塔格

我試圖製作某種自動複製 Powershell 腳本,該腳本可以讀取帶有項目名稱和文件名的文本文件,然後使用項目名稱創建新文件夾並將這些文件複製到其中。因此,例如,我有文件 RELEASE.txt:

project1 example1.rule
project1 example1.doc
project2 example2.sql
project3 example3.sql

我需要在我的本地機器上運行一個腳本,它可以讀取這個文件,創建文件夾 project* 並將這個 example*.* 文件複製到適當的文件夾。

我理解使用 Get-Content 命令的步驟,它只是將所有文件從列表複製到特定文件夾,但我想做一些更容易管理髮布的事情。

Get-Content 'c:\Test\RELEASE.txt' | ForEach-Object { Copy-Item "c:\Test\Projects\$_" c:\Test\Project-1 }
跟隨

雖然在您的問題中不太清楚要復制的文件在哪裡,但我認為這些文件都在您要創建子文件夾的同一個根文件夾中。

為此,您可以:

$sourceFolder = 'C:\repo'
$rootFolder = 'C:\Test\Projects'
Get-Content 'C:\Test\RELEASE.txt' | ForEach-Object { 
    # split the line from the text file in a project and filename
    $project, $file = ($_ -split '\s+').Trim()
    $sourceFile = Join-Path -Path $sourceFolder -ChildPath $file
    if (Test-Path -Path $sourceFile -PathType Leaf) {
        $targetFolder = Join-Path -Path $rootFolder -ChildPath $project
        # create the subfolder if it does not already exist
        $null = New-Item -Path $targetFolder -ItemType Directory -Force
        # copy the file to the new Project subfolder
        Copy-Item -Path $sourceFile -Destination $targetFolder
    }
    else {
        Write-Warning "File '$file' could not be found in folder '$rootFolder'"
    }
}

根據您的評論,如果需要在 Source 文件夾內的可能子目錄中搜索從 RELEASE.txt 獲取的文件名,您可以執行以下操作:

$sourceFolder   = 'C:\repo'            # where the original files to copy are
$projectsFolder = 'C:\Test\Projects'   # where the project subfolders and file copies need to go

Get-Content 'C:\Test\RELEASE.txt' | ForEach-Object { 
    # split the line from the text file in a project and filename
    $project, $file = ($_ -split '\s+').Trim()
    # search for this file inside the Source folder. 
    # This COULD return several files with that name in different subfolders..
    # to not get into trouble with naming collisions in the ProjectX subfolder 
    # when copying, grab only the first one found.
    $sourceFile = Get-ChildItem -Path $sourceFolder -Filter $file -File -Recurse | Select-Object -First 1
    if ($sourceFile) {
        $targetFolder = Join-Path -Path $projectsFolder -ChildPath $project
        # create the subfolder if it does not already exist
        $null = New-Item -Path $targetFolder -ItemType Directory -Force
        # copy the file to the new Project subfolder
        $sourceFile | Copy-Item -Destination $targetFolder
    }
    else {
        Write-Warning "File '$file' could not be found in folder '$projectsFolder'"
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 bash 腳本讀取和分析文本文件

使用 Powershell 腳本從子文件夾複製特定類型的文件

使用Powershell读取文本文件并查找文本

使用Powershell替换文本文件中的路径

使用PowerShell替换文本文件的内容

使用Windows Powershell(FFmpeg)创建串联文本文件

使用Powershell从纯文本文件中提取表

使用PowerShell更改文本文件中的数值

使用PowerShell腳本根據txt文件中的文件/文件夾結構移動一個文件夾中的所有文件

使用 PowerShell,在文本文件中搜索多个匹配项和分组结果

使用Powershell和Regex从文本文件中提取行块

使用Powershell在文本文件中查找和更新键值

使用PowerShell从文本文件中提取用户名和电子邮件

Powershell脚本使用启动过程和管道输出文件到文本文件,不工作,产生空白文本文件

使用Powershell搜索文件并使用文本文件复制它们

使用 Powershell CSV 对象的文本文件到文本文件字符串的转换

基于不同文本文件的行,使用powershell替换文本文件的行

使用 powershell 在 s3 存儲桶中創建一個新文件夾

在批处理文件中使用powershell从文本文件中添加浮点数

使用Powershell将文本文件中的特定行复制到单独的文件中

使用powershell和regex查找和替换文本文件中的多个字符串

使用PowerShell将文本附加到文本文件中的某些值

如何使用Windows CMD(而非Powershell)搜索和替换文本文件中包含“ =”字符的字符串

使用Powershell Get-Content和Set-Content编辑文本文件时遇到麻烦

使用 PowerShell 替换文本文件中一行中间的字符

使用Powershell匹配文本文件中所有出现的模式

将输出重定向到位于Azure存储上的文本文件-使用Powershell

使用Powershell脚本从文本文件中循环提取键/值对

如何使用PowerShell比较两个文本文件以查看它们是否相等?