簡體   English   中英

在Linux(Ubuntu pwsh)上運行的PowerShell腳本,但在Windows上不運行

[英]PowerShell script working on Linux (Ubuntu pwsh) but not on Windows

以下Powershell腳本按擴展名在相應目錄中排列文件。 我要做的是首先在目標文件夾中找到所有唯一的擴展名,並為所有這些擴展名叫copy,但是在Windows上不起作用。我為Windows使用了正確的路徑。

當前正在創建文件夾,但未將文件從源文件夾復制到目標文件夾。

$source = "/home/shubham/ps/" #location of starting directory
$destination = "/home/shubham/ps/check"; #location where files will be   `copied to`
$extensionArray = @(Get-ChildItem ($source) |
                  Select-Object Extension |
                  Sort-Object Extension |
                  Get-Unique -AsString)
echo ($extensionArray[3])
[string[]]$l_array = ($extensionArray | Out-String -Stream) -notmatch '^$' |
                     select -Skip 2

for ($i=0; $i -lt $extensionArray.Length; $i++) {
    $x = $l_array[$i]

    $ext = "*" + "$x"

    #foreach ($x in $extension_array){
    echo ("*" + ($x))
    $newsrc = ($source) + "/" + ($ext)
    $files = @("", "*" + ($x)) #edit .xlsx to your desired extension
    $outputPath = ($destination) + "_" + ($x)

    New-Item -ItemType Directory -Force -Path ($outputpath);
    Copy-Item -Path $newsrc -Filter ($ext) -Destination $outputPath -Container 
}
echo "end"

如有疑問,請閱讀文檔 (重點是我的):

-路徑
將要復制的項目的路徑指定為字符串數組。
類型:字符串[]
位置:1
默認值:無
接受管道輸入:True(ByPropertyName,ByValue)
接受通配符:False

Copy-Item不支持/home/shubham/ps/*.docx類的含義路徑。 它可能在Linux上有效,因為Shell在Copy-Item看到通配符之前已經將通配符擴展到了絕對路徑列表。

更不用說您的代碼過於復雜。 這樣的事情就足夠了:

$src = '/home/shubham/ps/'
$dst = '/home/shubham/ps/check'

$files = Get-ChildItem $src

$files |
    Select-Object -Expand Extension -Unique |
    New-Item -Path $dst -Name {'_' + $_.TrimStart('.')} -Type Directory |
    Out-Null

$files | Copy-Item -Destination {Join-Path $dst ('_' + $_.Extension.TrimStart('.')} -Container

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM