解析文件名中缺少的字符,就像读取/删除字符一样

时间:2016-11-19 00:42:08

标签: string powershell

计算机上的文件名命名如此

quant-ph9501001
math9901001
cond-mat0001001
hep-lat0308001
gr-qc0703001

但在http链接上,文件名包含/个字符

quant-ph/9501001
math/9901001
cond-mat/0001001
hep-lat/0308001
gr-qc/0703001

我无法将我的文件quant-ph9501001重命名为quant-ph/9501001,因为/是非法字符,因此我无法正确使用我的代码来解析和重命名< em>语法到脚本操作

遵循此模式的文件名语法:

letters + 8 digits
letters + '-' + letters + 8 digits

我可以将quant-ph9501001更改为quant-ph_9501001,但我需要解析文件名中缺少的字符,就像阅读/(斜杠字符)一样。

所以,如果我有像

这样的字符串
gr-qc0701001
gr-qc_0701001

它应该读起来像

quant-ph/9501001

我的脚本对gr-qc/0701001没有工作(没有解析),因为我无法使用非法字符重命名文件名。错误是404。

  

iwr:远程服务器返回错误:(404)Not Found。

如果脚本正常工作,PowerShell应返回此字符串:

General Relativity and Quantum Cosmology (gr-qc)

和文件名应为

Spectral Broadening of Radiation from Relativistic Collapsing Objects

我的脚本是

    $list1 = @"
quant-ph9802001
quant-ph9802004
"@

$list2 = @"
quant-ph/9802001
quant-ph/9802004
"@

Write-Output "Adding forward slashes"
$list1 -split "`r`n" | % {
    $item = $_.Trim()
    $newItem = $item -replace '(.*)(\d{7})', '$1/$2'
    Write-Output $("{0} ==>  {1}" -f $item, $newItem)
}

Write-Output "Removing forward slashes"
$list2 -split "`r`n" | % {
    $item = $_.Trim()
    $newItem = $item -replace '(.*)/(\d{7})', '$1$2'
    Write-Output $("{0} ==>  {1}" -f $item, $newItem)
}

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}

编辑:在Kori Gill回答

之后,错误仍然是404

http://i.imgur.com/ZOZyMad.png

问题是本地文件名和在线文件名之间的区别。我应该暂时在内存中添加本地文件名中的非法字符,否则脚本不起作用。

1 个答案:

答案 0 :(得分:3)

我不能说我完全理解你的问题,但听起来你只需要将这些名称转换为具有或不具有正斜杠的格式。你提到8位数字,但你的例子有7位。你可以根据需要进行调整。

我认为这样的事情会对你有帮助......

$list1 = @"
quant-ph9501001
math9901001
cond-mat0001001
hep-lat0308001
gr-qc0703001
"@

$list2 = @"
quant-ph/9501001
math/9901001
cond-mat/0001001
hep-lat/0308001
gr-qc/0703001
"@

Write-Output "Adding forward slashes"
$list1 -split "`r`n" | % {
    $item = $_.Trim()
    $newItem = $item -replace '(.*)(\d{7})', '$1/$2'
    Write-Output $("{0} ==>  {1}" -f $item, $newItem)
}

Write-Output "Removing forward slashes"
$list2 -split "`r`n" | % {
    $item = $_.Trim()
    $newItem = $item -replace '(.*)/(\d{7})', '$1$2'
    Write-Output $("{0} ==>  {1}" -f $item, $newItem)
}

输出:

Adding forward slashes
quant-ph9501001 ==>  quant-ph/9501001
math9901001 ==>  math/9901001
cond-mat0001001 ==>  cond-mat/0001001
hep-lat0308001 ==>  hep-lat/0308001
gr-qc0703001 ==>  gr-qc/0703001
Removing forward slashes
quant-ph/9501001 ==>  quant-ph9501001
math/9901001 ==>  math9901001
cond-mat/0001001 ==>  cond-mat0001001
hep-lat/0308001 ==>  hep-lat0308001
gr-qc/0703001 ==>  gr-qc0703001