Advertisement
dropbox1349

Parsing missing character in filenames as if is reading /

Nov 19th, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $list1 = @"
  2. quant-ph9802001
  3. quant-ph9802004
  4. "@
  5.  
  6. $list2 = @"
  7. quant-ph/9802001
  8. quant-ph/9802004
  9. "@
  10.  
  11. Write-Output "Adding forward slashes"
  12. $list1 -split "`r`n" | % {
  13.     $item = $_.Trim()
  14.     $newItem = $item -replace '(.*)(\d{7})', '$1/$2'
  15.     Write-Output $("{0} ==>  {1}" -f $item, $newItem)
  16. }
  17.  
  18. Write-Output "Removing forward slashes"
  19. $list2 -split "`r`n" | % {
  20.     $item = $_.Trim()
  21.     $newItem = $item -replace '(.*)/(\d{7})', '$1$2'
  22.     Write-Output $("{0} ==>  {1}" -f $item, $newItem)
  23. }
  24.  
  25. Function Clean-InvalidFileNameChars {
  26.   param(
  27.     [Parameter(Mandatory=$true,
  28.       Position=0,
  29.       ValueFromPipeline=$true,
  30.       ValueFromPipelineByPropertyName=$true)]
  31.     [String]$Name
  32.   )
  33.  
  34.   $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
  35.   $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  36.   $res=($Name -replace $re)
  37.   return $res.Substring(0, [math]::Min(260, $res.Length))
  38. }
  39.  
  40. Function Clean-InvalidPathChars {
  41.   param(
  42.     [Parameter(Mandatory=$true,
  43.       Position=0,
  44.       ValueFromPipeline=$true,
  45.       ValueFromPipelineByPropertyName=$true)]
  46.     [String]$Name
  47.   )
  48.  
  49.   $invalidChars = [IO.Path]::GetInvalidPathChars() -join ''
  50.   $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
  51.   $res=($Name -replace $re)
  52.   return $res.Substring(0, [math]::Min(248, $res.Length))
  53. }
  54.  
  55.  
  56. $rootpath="c:\temp2"
  57.  
  58. $rootpathresult="c:\tempresult"
  59.  
  60. $template=@'
  61. [3]  arXiv:1611.00057 [pdf, ps, other]
  62. Title: {title*:Holomorphy of adjoint $L$ functions for quasisplit A2}
  63. Authors: Joseph Hundley
  64. Comments: 18 pages
  65. Subjects: {subject:Number Theory (math.NT)}
  66. [4]  arXiv:1611.00066 [pdf, other]
  67. Title: {title*:Many Haken Heegaard splittings}
  68. Authors: Alessandro Sisto
  69. Comments: 12 pages, 3 figures
  70. Subjects: {subject:Geometric Topology (math.GT)}
  71. [5]  arXiv:1611.00067 [pdf, ps, other]
  72. Title: {title*:Subsumed homoclinic connections and infinitely many coexisting attractors in piecewise-linear maps}
  73. Authors: David J.W. Simpson, Christopher P. Tuffley
  74. Subjects: {subject:Dynamical Systems (math.DS)}
  75. [21]  arXiv:1611.00114 [pdf, ps, other]
  76. Title: {title*:Faces of highest weight modules and the universal Weyl polyhedron}
  77. Authors: Gurbir Dhillon, Apoorva Khare
  78. Comments: We recall preliminaries and results from the companion paper arXiv:1606.09640
  79. Subjects: {subject:Representation Theory (math.RT)}; Combinatorics (math.CO); Metric Geometry (math.MG)
  80. '@
  81.  
  82. #extract utils data and clean
  83. $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}}  
  84.  
  85.  
  86.  
  87. #build dirs and copy+rename file
  88. $listbook | %{$newpath="$rootpathresult\$($_.subject)"; New-Item -ItemType Directory -Path "$newpath" -Force;  Copy-Item $_.file "$newpath\$($_.title).pdf" -Force}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement