我的文件夹中有 8 个文档,我打算创建 2 个单独的文档。第一个文档的文件名“包含”'_B';其他 4 个文档不包含'_B'。我创建了代码来合并所有文件,但需要实现此条件。紧急需求请帮忙。
代码文件
strPathSrc = "D:\testing pleae delete\test" ' Source files folder
strMaskSrc = "*.xlsx " ' Source files filter mask
iSheetSrc = 1 ' Sourse sheet index or name
strPathDst = "C:\Users\Admin\Desktop\sample1.xlsx" ' Destination file
iSheetDst = 1 ' Destination sheet index or name
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkBookDst = objExcel.Workbooks.Open(strPathDst)
Set objSheetDst = objWorkBookDst.Sheets(iSheetDst)
Set objShellApp = CreateObject("Shell.Application")
Set objFolder = objShellApp.NameSpace(strPathSrc)
Set objItems = objFolder.Items()
' objItems.Filter 64 + 128, strMaskSrc
objExcel.DisplayAlerts = False
' For Each objItem In objItems
' Wscript.Echo objItem.Path
' Wscript.Echo objItem.type
' Next
For Each objItem In objItems
If objItem.type = "Microsoft Excel 97-2003 Worksheet" Or objItem.type = "Microsoft Office Excel 97-2003 Worksheet" Or objItem.type = "Microsoft Excel Worksheet" Or objItem.type = "Microsoft Excel Macro-Enabled Worksheet" Then
Set objWorkBookSrc = objExcel.Workbooks.Open(objItem.Path)
Set objSheetSrc = objWorkBookSrc.Sheets(iSheetSrc)
GetUsedRange(objSheetSrc).Copy
Set objUsedRangeDst = GetUsedRange(objSheetDst)
iRowsCount = objUsedRangeDst.Rows.Count
objWorkBookDst.Activate
objSheetDst.Cells(iRowsCount + 2, 1).Select
objSheetDst.Paste
objWorkBookDst.Application.CutCopyMode = False
objWorkBookSrc.Close
End If
Next
Function GetUsedRange(objSheet)
With objSheet
' Wscript.Echo ".UsedRange.Row"
' Wscript.Echo .UsedRange.Row
' Wscript.Echo ".UsedRange.Rows.Count"
' Wscript.Echo .UsedRange.Rows.Count
' Wscript.Echo ".UsedRange.Column"
' Wscript.Echo .UsedRange.Column
' Wscript.Echo ".UsedRange.Columns.Count"
' Wscript.Echo .UsedRange.Columns.Count
Set GetUsedRange = .Range(.Cells(2, 1), .Cells(.UsedRange.Row + .UsedRange.Rows.Count - 1, .UsedRange.Column + .UsedRange.Columns.Count - 1))
End With
Wscript.Echo "============================"
End Function
objSheetDst.Rows("1:3").EntireRow.Delete
答案1
$files = get-childitem 'path-to-folder-with-8-files'
$files | % {
if ($_.Name -like "*_B*") {
$_.Name >> files_with_b.txt
} else {
$_.Name >> files_without_b.txt
}
}
vbscript 随着 powershell 的消失而消失了...仍然可以使用它...只是,你为什么要这样做...