top of page

When writing a script to copy files from one destination to another, be sure to use PowersShell instead of creating a batch file with the XCOPY command. In PowerShell, the Copy-Item command followed by the source path and then the destination folder:


Copy-Item -Path "C:\foofolder\2022.07.Litigation Support Tip of the Night V2.docx" -Destination "C:\copy set\" -PassThru

Copy-Item -Path "C:\foofolder\acme.js" -Destination "C:\copy set\" -PassThru

Copy-Item -Path "C:\foofolder\AndroGel Meeting.docx" -Destination "C:\copy set\" -PassThru

Copy-Item -Path "C:\foofolder\Applications" -Destination "C:\copy set\" -PassThru


. . . will copy files to a new location faster than a .bat file.




81 views0 comments

The below Visual Basic code, posted here by skyang, can be used to generate a list of PDF files which shows how many pages are in each file.


Simply enter the code in a new module in Visual Basic. Start from the beginning macro at Sub Test(), and after you press play you'll be prompted to select a folder where your PDF files are located.





This code will process any files saved to subfolders.


The code will generate a list like this which will also show the file size and file path of each PDF:




Note that if any file has a folder path longer than 255 characters, the code will fail and this message will come up.





As always, I have tested this code tonight and confirmed that it works - although in a large data set it did give a zero count for the pages in some PDFs. However it took less than 20 minutes to review more than 9000 files containing more than 80,000 pages.



Sub Test()

Dim I As Long

Dim xRg As Range

Dim xStr As String

Dim xFd As FileDialog

Dim xFdItem As Variant

Dim xFileName As String

Dim xFileNum As Long

Dim RegExp As Object

Set xFd = Application.FileDialog(msoFileDialogFolderPicker)

If xFd.Show = -1 Then

xFdItem = xFd.SelectedItems(1) & Application.PathSeparator

Set xRg = Range("A1")

Range("A:B").ClearContents

Range("A1:B1").Font.Bold = True

xRg = "File Name"

xRg.Offset(0, 1) = "Pages"

xRg.Offset(0, 2) = "Path"

xRg.Offset(0, 3) = "Size(b)"

I = 2

Call SunTest(xFdItem, I)

End If

End Sub


Sub SunTest(xFdItem As Variant, I As Long)

Dim xRg As Range

Dim xStr As String

Dim xFd As FileDialog

Dim xFileName As String

Dim xFileNum As Long

Dim RegExp As Object

Dim xF As Object

Dim xSF As Object

Dim xFso As Object

xFileName = Dir(xFdItem & "*.pdf", vbDirectory)

xStr = ""

Do While xFileName <> ""

Cells(I, 1) = xFileName

Set RegExp = CreateObject("VBscript.RegExp")

RegExp.Global = True

RegExp.Pattern = "/Type\s*/Page[^s]"

xFileNum = FreeFile

Open (xFdItem & xFileName) For Binary As #xFileNum

xStr = Space(LOF(xFileNum))

Get #xFileNum, , xStr

Close #xFileNum

Cells(I, 2) = RegExp.Execute(xStr).Count

Cells(I, 3) = xFdItem & xFileName

Cells(I, 4) = FileLen(xFdItem & xFileName)

I = I + 1

xFileName = Dir

Loop

Columns("A:B").AutoFit

Set xFso = CreateObject("Scripting.FileSystemObject")

Set xF = xFso.GetFolder(xFdItem)

For Each xSF In xF.SubFolders

Call SunTest(xSF.Path & "\", I)

Next

End Sub

286 views0 comments

You can use a PowerShell script to count the number of lines in multiple text files saved to a folder.



Enter the file path for the folder after the Get-ChildItem command on the first line. Then specify the extension of the files to be analyzed towards the end of the first line after 'extension - eq'.


Get-ChildItem c:\foofolder\test2 -recurse | where {$_.extension -eq ".txt"} | % {

$_ | Select-Object -Property 'Name', @{

label = 'Lines'; expression = {

($_ | Get-Content).Length

}

}

} | out-file C:\foofolder\test2\lines1.txt

On the last line provide the file path for a new text file to which PowerShell will write the results. Open Windows PowerShell ISE (x86) and then enter the script in a new pane, and then press the play button on the toolbar.



The text file that is generated will list each file name in the source folder and show the number of lines in each in a column to the right.












I ran this script on a set of more than 100,000 text files (which turned out to consist of more than 9 million lines) and it finished the review in less than 30 minutes.


The script can also be used to find the number of lines in other files such as .csv files.


Be sure to enter the file paths in quotes if they include blank spaces.


Thanks to Hari Parkash for posting this script here.


148 views0 comments
bottom of page