2016-06-13 35 views
-1

我在文件系統(Linux)的短片段對的列表... UniqueDocument.xml UniqueDocument.pdf列表中,找到那些誰不

我需要找到一個沒有條目XML文件,然後我需要獲取它。

一直試着用os.list和正則表達式,但沒有在Ruby中找到一個可用的解決方案和Dir()。但我無法走到最後......我的思想擋住了我。

+4

提供您嘗試過的代碼以及失敗的代碼。提供至少一些樣本輸入和預期輸出。 – Agis

回答

1

在Ruby中,

# Get an array of file names for pdf and xml 
pdf=Dir.glob("test/*.pdf").map {|f| File.basename(f, '.pdf')} 
xml=Dir.glob("test/*.xml").map {|f| File.basename(f, '.xml')} 

# Make the difference between xml and pdf to get file names that have a pdf file but no xml 
p pdf - xml 

它是如何工作的?

  1. Dir.glob("test/*.pdf")

返回與夾test的路徑的所有PDF文件的數組。看起來像["test/foo.pdf", ...]

  • File.basename('test/foo.pdf', '.pdf')
  • 返回不包括擴展名的文件名。在這種情況下,將返回'foo'

  • Dir.glob("test/*.pdf").map {|f| File.basename(f, '.pdf')}
  • 返回的文件名沒有擴展的陣列,只服用PDF文件。

  • pdf - xml
  • 返回該是PDF但不是在所有的XML字符串。

    相關問題