循环并加入 2 个函数以按类别移动文件夹中的文件并按原始标题重命名

Loop and join 2 functions to move files in folders by category & rename by original title

对于匹配类别与文件名我使用这个代码

 gci *.pdf | foreach { (iwr "https://arxiv.org/abs/$($_.BaseName)")`
 -match 'primary-subject">(.*?)</span>'; $matches[1] }

明白我的意思http://i.imgur.com/57KjJr6.png

独立地重命名:我可以使用它,但这没有用,因为我处理一个文件夹一个文件夹,这需要很长时间(文件夹数量很多)

#All PDFs | Rename { query Arxiv for the abstract by filename, use the page title + ".pdf"}

    Get-ChildItem *.pdf | Rename-Item -NewName { 
        $title = (Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName)").parsedhtml.title
        $title = $title -replace '[\/:\*\?"<>\|]', '-'  # replace forbidden characters
        "$title.pdf"                                     # in filenames with -
    }

我应该像这样制作这个文件夹(没有 [文件夹]

[folder] Information Theory (cs.IT)
[folder] Number Theory (math.NT)
....

我尝试加入 2个操作:

按主题移动

[folder] Geometric Topology (cs.IT)
        |
        |__ [file] 1611.00066
        |__ [file] .....

[folder] Number Theory (math.NT)
    |
    |__ [file] 1611.00057

并按标题重命名

[folder] Geometric Topology (cs.IT)
        |
        |__ [file] 1611.00066
        |__ [file] .....

[folder] Number Theory (math.NT)
        |
        |__ [file] 1611.00057

对于循环和连接操作,我制作了一个 .ps1 文件。我插入此代码但不起作用

$res=Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName"
   $rootpath="c:\temp"

   Function Clean-InvalidFileNameChars {
      param(
        [Parameter(Mandatory=$true,
          Position=0,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
        [String]$Name
      )

      $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
      $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
      $res=($Name -replace $re)
      return $res.Substring(0, [math]::Min(260, $res.Length))
    }

    Function Clean-InvalidPathChars {
      param(
        [Parameter(Mandatory=$true,
          Position=0,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
        [String]$Name
      )

      $invalidChars = [IO.Path]::GetInvalidPathChars() -join ''
      $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
      $res=($Name -replace $re)
      return $res.Substring(0, [math]::Min(248, $res.Length))
    }

    gci *.pdf | foreach { (iwr "https://arxiv.org/abs/$($_.BaseName)")`
     -match 'primary-subject">(.*?)</span>'; $matches[1] }


    #get date and cut with format template, group by Subject and clean Title and Subject for transformation to dir and file name
    $grousubject=$res.ParsedHtml.body.outerText | ConvertFrom-String -TemplateContent $template | select  @{N="Subject";E={Clean-InvalidPathChars $_.subject}}, @{N="Title";E={Clean-InvalidFileNameChars $_.title}} | group Subject 

    #create dir and files
    $grousubject | %{$path= "$rootpath$($_.Name)" ; $_.group.title | %{New-Item -ItemType File -Path "$path$_" -Force}   }

   Get-ChildItem *.pdf | Rename-Item -NewName { 
            $title = (Invoke-WebRequest "https://arxiv.org/abs/$($_.BaseName)").parsedhtml.title
            $title = $title -replace '[\/:\*\?"<>\|]', '-'  # replace forbidden characters
            "$title.pdf"                                     # in filenames with -
        }

我的powershell版本是4

编辑: Esmeraldo 解决方案的工作方式如下 http://i.imgur.com/NEio868.png

谢谢

Function Clean-InvalidFileNameChars {
  param(
    [Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [String]$Name
  )

  $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
  $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  $res=($Name -replace $re)
  return $res.Substring(0, [math]::Min(260, $res.Length))
}

Function Clean-InvalidPathChars {
  param(
    [Parameter(Mandatory=$true,
      Position=0,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true)]
    [String]$Name
  )

  $invalidChars = [IO.Path]::GetInvalidPathChars() -join ''
  $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  $res=($Name -replace $re)
  return $res.Substring(0, [math]::Min(248, $res.Length))
}


$rootpath="c:\temp2"

$rootpathresult="c:\tempresult"

$template=@'
[3]  arXiv:1611.00057 [pdf, ps, other] 
Title: {title*:Holomorphy of adjoint $L$ functions for quasisplit A2} 
Authors: Joseph Hundley 
Comments: 18 pages 
Subjects: {subject:Number Theory (math.NT)} 
[4]  arXiv:1611.00066 [pdf, other] 
Title: {title*:Many Haken Heegaard splittings} 
Authors: Alessandro Sisto 
Comments: 12 pages, 3 figures 
Subjects: {subject:Geometric Topology (math.GT)} 
[5]  arXiv:1611.00067 [pdf, ps, other] 
Title: {title*:Subsumed homoclinic connections and infinitely many coexisting attractors in piecewise-linear maps} 
Authors: David J.W. Simpson, Christopher P. Tuffley 
Subjects: {subject:Dynamical Systems (math.DS)} 
[21]  arXiv:1611.00114 [pdf, ps, other] 
Title: {title*:Faces of highest weight modules and the universal Weyl polyhedron} 
Authors: Gurbir Dhillon, Apoorva Khare 
Comments: We recall preliminaries and results from the companion paper arXiv:1606.09640 
Subjects: {subject:Representation Theory (math.RT)}; Combinatorics (math.CO); Metric Geometry (math.MG)
'@

#extract utils data and clean 
$listbook=gci $rootpath -File -filter *.pdf | foreach { New-Object psobject -Property @{file=$_.fullname; books= ((iwr "https://arxiv.org/abs/$($_.BaseName)").ParsedHtml.body.outerText | ConvertFrom-String -TemplateContent $template)}} | select file -ExpandProperty books |  select file,  @{N="Subject";E={Clean-InvalidPathChars $_.subject}}, @{N="Title";E={Clean-InvalidFileNameChars $_.title}}  



#build dirs and copy+rename file
$listbook | %{$newpath="$rootpathresult$($_.subject)"; New-Item -ItemType Directory -Path "$newpath" -Force;  Copy-Item $_.file "$newpath$($_.title).pdf" -Force}