2011-11-23 59 views
0

我必须使用大量的文本文件。我能够将这些文件整合到一个文件中。但是我也在我的工作中使用了文件名,我希望在文件本身的文本之前以excel格式存在,最好第一列应该包含文件的名称,然后列可以包含数据。用于整合文件的Windows脚本

任何帮助,将不胜感激。谢谢。

+0

你想使用哪种程序员语言? – reporter

+0

Windows脚本。我有Powershell – user1061514

+0

发布您的实际脚本。这是尝试帮助你的起点。 –

回答

1

这是Powershell脚本。您可能需要稍作修改,以查找特定的文件扩展名现在只要找PS1文件

[System.Threading.Thread]::CurrentThread.CurrentCulture = New-Object System.Globalization.CultureInfo("en-US") 

$excel = new-Object -comobject Excel.Application 
$excel.visible = $false 

$workBook = $excel.Workbooks.Add() 
$sheet = $workBook.Sheets.Item(1) 
$sheet.Name = "Files" 
$sheet.Range("A1", "B1").Font.Bold = $true 
$sheet.Range("A1","A2").ColumnWidth = 40 
$sheet.Range("B1","B2").ColumnWidth = 100 

$sheet.Cells.Item(1,1) = "Filename" 
$sheet.cells.Item(1,2) = "Content" 

$files = get-childitem C:\PST -recurse | where {$_.extension -eq ".ps1"} 
$index = 2 

foreach($file in $files) 
{ 
    $sheet.Cells.Item($index,1) = $file.FullName 
    $sheet.Cells.Item($index,2) = [System.IO.File]::ReadAllText($file.FullName) 
    $index++ 
} 

$workBook.SaveAs("C:\PST\1.xlsx") 
$excel.Quit() 

注:我不会假装它是完美的,你仍然需要打磨,并重构它,但至少它会给你指示

+1

$ workBook.SaveAs(“C:\ PST \ 1.xlsx”)或$ workBook.SaveAs(“C:\ PST \ 1.xls”),基于Office版本安装! –