需要用户输入某些路径并使用相对路径来压缩我的文件夹

问题描述 投票:0回答:2

我正在创建一个自动化脚本,它应该接受用户输入的版本、新包和新包路径,然后将其压缩到一个 zip 文件夹中。不幸的是,我无法对路径进行硬编码,所以我正在努力解决如何使用在使用之前必须验证的相对路径和用户输入。

$env = Read-Host -Prompt "Version Number"

if ($env.length -ne 7) {
    $env = Read-Host "Re-enter version number"
}

$packageName = Read-Host -Prompt "Package Name"

$newPackage = Write-Output $packageName"."$env

$newDirectory = Read-Host -Prompt "Path to new directory"
$deployPath = Read-Host -Prompt "Path to Deploy.ps1 file"
$zipFile = Read-Host -Prompt "Path for new zip file"

$newItem = $newDirectory"/"$newPackage
Write-Host $newItem

new-item $newItem -type directory 
Copy-Item $deployPath -Destination $zipFile

Microsoft.PowerShell.Archive\Compress-Archive -Path /Users/SG/projects/$newPackage -DestinationPath /Users/SG/$newPackage.zip

我需要从用户输入 $zipFile,并且 -Path/-DestinationPath 应该是相对路径,因为它们不能被硬编码。

macos powershell user-input relative-path compress-archive
2个回答
0
投票

您可以使用 Windows 窗体来提示目录路径。这样做的功能可能看起来像

Function Get-filePath() {

    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenFolderDialog.rootfolder = 'MyComputer'
    $OpenFolderDialog.ShowDialog() | Out-Null
    $OpenFolderDialog.SelectedPath
}

然后像这样使用它

Write-Host "Please enter your path for X/Y/Z"
$newDirectory = Get-filePath

对于您需要的每条路径,或者您希望集成的方式。你也可以玩一下初始目录。

更新示例:

Function Get-filePath() {

    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenFolderDialog.rootfolder = 'MyComputer'
    $OpenFolderDialog.ShowDialog() | Out-Null
    $OpenFolderDialog.SelectedPath
}

Write-Host "Path to new directory:"
$newDirectory = Get-filePath
$newDirectory
Write-Host "Path to deploy the ps1 file:"
$deployPath = Get-filePath
$deployPath
Write-Host "Path for new zip file:"
$zipFile = Get-filePath
$zipFile

0
投票

您可以简单地在脚本中添加强制参数
我会简单地使用

[String]
类型作为
[System.Io.FileInfo]
类型 将默认为
C:\WINDOWS\system32
目录的任何相对路径。请参阅:PowerShell 问题引入一个 PowerShell 感知路径信息类以实现方便的 .NET 互操作性
#14745
.
您可以在评论库帮助中进一步描述

<#
.SYNOPSIS
    Compress Files
.DESCRIPTION
    I am creating an automation script that should take a user's input for
    version, package new, and new package path, then compress it into a zip
    folder. Unfortunately, I can't hardcode the paths, so I'm struggling with 
    how to use relative paths and user inputs that I have to validate before 
    using.
.PARAMETER ZipFile
    (Relative) path for new zip file
#>
Param(
    [Parameter(Mandatory=$true)]
    [string]
    $zipFile
)
Join-Path (Get-Location) $ZipFile

如果你把它放在一个

ZipFile.ps1
脚本中,会发生以下情况:

PS C:\CurrentDirectory> .\ZipFile.ps1 -?

退货

NAME
    C:\CurrentDirectory\ZipFile.ps1

SYNOPSIS
    Compress Files

SYNTAX
    C:\CurrentDirectory\ZipFile.ps1 [-zipFile] <String> [<CommonParameters>]

DESCRIPTION
    I am creating an automation script that should take a user's input for
    version, package new, and new package path, then compress it into a zip
    folder. Unfortunately, I can't hardcode the paths, so I'm struggling with
    how to use relative paths and user inputs that I have to validate before
    using.

RELATED LINKS

REMARKS
    To see the examples, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Examples"
    For more information, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Detailed"
    For technical information, type: "Get-Help C:\CurrentDirectory\ZipFile.ps1 -Full"

如果您不带参数调用脚本 (

.\ZipFile.ps1
),脚本将自动询问强制参数:

PS C:\CurrentDirectory> .\ZipFile.ps1

退货

cmdlet ZipFile.ps1 at command pipeline position 1
Supply values for the following parameters:
zipFile: Test.Zip
C:\CurrentDirectory\Test.Zip

您还可以考虑使用所需的参数直接调用您的脚本:

PS C:\CurrentDirectory\ZipFile.ps1 My.Zip

退货

C:\CurrentDirectory\My.Zip
© www.soinside.com 2019 - 2024. All rights reserved.