0

以下 powershell 脚本按文件的扩展名在适当的目录中排列文件。我所做的是首先在目标文件夹中找到所有唯一的扩展名,并为所有这些扩展名调用副本,但它在 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"
4

1 回答 1

0

如有疑问,请阅读文档(强调我的):

-Path
以字符串数组的形式指定要复制的项目的路径。
类型:String[]
位置:1
默认值:无
接受管道输入:True (ByPropertyName, ByValue)
接受通配符:False

/home/shubham/ps/*.docx表示不支持类似的路径Copy-ItemCopy-Item它可能适用于 Linux,因为 shell 在看到它之前已经将通配符扩展为绝对路径列表。

更不用说你的代码过于复杂了。像这样的东西就足够了:

$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
于 2018-11-30T14:45:48.270 回答