write a powershell script to count the number of PDF files within each subfolder, and write the output to a CSV file. The output should consist of the folder name and the number of PDFS within that folder.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a PowerShell script that will do what you're asking for:
php
# Define the root directory to start the search
$rootDirectory = "C:\MyFiles"
# Get a list of all subdirectories within the root directory
$subDirectories = Get-ChildItem $rootDirectory -Directory
# Create an empty array to store the results
$results = @()
# Loop through each subdirectory
foreach ($subDirectory in $subDirectories)
{
# Count the number of PDF files within the subdirectory
$pdfCount = (Get-ChildItem $subDirectory.FullName -Filter *.pdf).Count
# Create a new object to store the results for this subdirectory
$result = [PSCustomObject]@{
FolderName = $subDirectory.Name
PDFCount = $pdfCount
}
# Add the result to the array of results
$results += $result
}
# Write the results to a CSV file
$results | Export-Csv -Path "C:\MyFiles\PDFReport.csv" -NoTypeInformation
This script first defines the root directory that you want to start the search from ($rootDirectory). It then uses the Get-ChildItem cmdlet to get a list of all subdirectories within the root directory.
Next, it creates an empty array ($results) to store the results of the script. The script then loops through each subdirectory, counts the number of PDF files within that subdirectory, and adds the results to the $results array.
Finally, the script uses the Export-Csv cmdlet to write the results to a CSV file. The -NoTypeInformation parameter is used to exclude the type information from the output.
This script should work as is, but you may need to modify the $rootDirectory variable to point to the correct directory on your system.
User
add a progress indicator
Assistant
Sure! You can add a progress indicator to the script by adding a loop counter and displaying the progress after processing each subdirectory. Here's the updated script:
php
# Define the root directory to start the search
$rootDirectory = "C:\MyFiles"
# Get a list of all subdirectories within the root directory
$subDirectories = Get-ChildItem $rootDirectory -Directory
# Create an empty array to store the results
$results = @()
# Get the total number of subdirectories
$totalSubDirectories = $subDirectories.Count
# Counter to keep track of the current subdirectory
$counter = 0
# Loop through each subdirectory
foreach ($subDirectory in $subDirectories)
{
# Count the number of PDF files within the subdirectory
$pdfCount = (Get-ChildItem $subDirectory.FullName -Filter *.pdf).Count
# Create a new object to store the results for this subdirectory
$result = [PSCustomObject]@{
FolderName = $subDirectory.Name
PDFCount = $pdfCount
}
# Add the result to the array of results
$results += $result
# Increment the counter and display the progress
$counter++
Write-Progress -Activity "Processing Subdirectories" -Status "Processing $counter of $totalSubDirectories" -PercentComplete ($counter / $totalSubDirectories * 100)
}
# Write the results to a CSV file
$results | Export-Csv -Path "C:\MyFiles\PDFReport.csv" -NoTypeInformation
This script uses the Write-Progress cmdlet to display a progress bar while processing the subdirectories. The -Activity parameter specifies the activity being performed (in this case, "Processing Subdirectories"), the -Status parameter specifies the status text to display (including the current subdirectory being processed and the total number of subdirectories), and the -PercentComplete parameter specifies the percentage of the task that has been completed.
The script calculates the percentage complete by dividing the current subdirectory count by the total number of subdirectories and multiplying by 100. This value is then passed as the -PercentComplete parameter to the Write-Progress cmdlet.
User
Add an array to iterate over multiple root directories.
Assistant
Sure! You can add an array of root directories to the script and process each one in turn. Here's the updated script:
php
# Define the root directories to start the search
$rootDirectories = @("C:\MyFiles1", "C:\MyFiles2", "C:\MyFiles3")
# Create an empty array to store the results
$results = @()
# Loop through each root directory
foreach ($rootDirectory in $rootDirectories)
{
# Get a list of all subdirectories within the root directory
$subDirectories = Get-ChildItem $rootDirectory -Directory
# Get the total number of subdirectories
$totalSubDirectories = $subDirectories.Count
# Counter to keep track of the current subdirectory
$counter = 0
# Loop through each subdirectory
foreach ($subDirectory in $subDirectories)
{
# Count the number of PDF files within the subdirectory
$pdfCount = (Get-ChildItem $subDirectory.FullName -Filter *.pdf).Count
# Create a new object to store the results for this subdirectory
$result = [PSCustomObject]@{
FolderName = $subDirectory.FullName
PDFCount = $pdfCount
}
# Add the result to the array of results
$results += $result
# Increment the counter and display the progress
$counter++
Write-Progress -Activity "Processing Subdirectories" -Status "Processing $counter of $totalSubDirectories in $rootDirectory" -PercentComplete ($counter / $totalSubDirectories * 100)
}
}
# Write the results to a CSV file
$results | Export-Csv -Path "C:\MyFiles\PDFReport.csv" -NoTypeInformation
This script uses an array of root directories ($rootDirectories) to process multiple directories. It loops through each root directory, gets a list of subdirectories within that root directory, and processes each subdirectory just as in the previous version of the script. The only difference is that the full path of each subdirectory is now included in the results. This way, you can see which subdirectories belong to which root directory in the final report.
User
Write a version of that script in bash
Assistant
Here's the equivalent script in bash:
bash
#!/bin/bash
# Define the root directories to start the search
root_directories=("/MyFiles1" "/MyFiles2" "/MyFiles3")
# Create an empty array to store the results
results=()
# Loop through each root directory
for root_directory in "${root_directories[@]}"; do
# Get a list of all subdirectories within the root directory
sub_directories=$(find "$root_directory" -type d)
# Get the total number of subdirectories
total_sub_directories=$(echo "$sub_directories" | wc -l)
# Counter to keep track of the current subdirectory
counter=0
# Loop through each subdirectory
while read -r sub_directory; do
# Count the number of PDF files within the subdirectory
pdf_count=$(find "$sub_directory" -type f -name "*.pdf" | wc -l)
# Add the result to the array of results
results+=("$sub_directory,$pdf_count")
# Increment the counter and display the progress
counter=$((counter + 1))
echo "Processing $counter of $total_sub_directories in $root_directory"
done <<< "$sub_directories"
done
# Write the results to a CSV file
echo "${results[@]}" > "/MyFiles/PDFReport.csv"
This script uses the find command to get a list of subdirectories within each root directory, and the wc command to count the number of subdirectories and PDF files. The results are stored in an array (results) and then written to a CSV file using redirection (>). The progress of the script is displayed using echo to print a message for each subdirectory processed.
Note that this script assumes that the root directories are located in the root directory (/) of the file system. You'll need to modify the paths to match the locations of your root directories.
User
write a version of that script in batch
Assistant
Here's the equivalent script in batch:
vbnet
@echo off
REM Define the root directories to start the search
set "root_directories=C:\MyFiles1 C:\MyFiles2 C:\MyFiles3"
REM Create an empty file to store the results
echo FolderName,PDFCount > C:\MyFiles\PDFReport.csv
REM Loop through each root directory
for /D %%r in (%root_directories%) do (
REM Get a list of all subdirectories within the root directory
for /D %%s in ("%%r\*") do (
REM Count the number of PDF files within the subdirectory
for /f %%c in ('dir /b "%%s\*.pdf" ^| find /c /v ""') do (
REM Add the result to the results file
echo %%s,%%c >> C:\MyFiles\PDFReport.csv
)
)
)
This script uses the for /D command to loop through the root directories and subdirectories. The for /f command is used to count the number of PDF files in each subdirectory. The results are stored in a CSV file (C:\MyFiles\PDFReport.csv) using redirection (>>).
Note that this script assumes that the root directories are located in the root directory (C:\) of the file system. You'll need to modify the paths to match the locations of your root directories.
User
add a progress indicator
Assistant
Here's the updated script with a progress indicator:
vbnet
@echo off
REM Define the root directories to start the search
set "root_directories=C:\MyFiles1 C:\MyFiles2 C:\MyFiles3"
REM Create an empty file to store the results
echo FolderName,PDFCount > C:\MyFiles\PDFReport.csv
REM Counter to keep track of the current root directory
set "counter=0"
REM Loop through each root directory
for /D %%r in (%root_directories%) do (
REM Increment the counter
set /a counter+=1
REM Display the progress
echo Processing root directory %counter% of %root_directories:~3%
REM Get a list of all subdirectories within the root directory
for /D %%s in ("%%r\*") do (
REM Count the number of PDF files within the subdirectory
for /f %%c in ('dir /b "%%s\*.pdf" ^| find /c /v ""') do (
REM Add the result to the results file
echo %%s,%%c >> C:\MyFiles\PDFReport.csv
)
)
)
This script uses a counter (counter) to keep track of the current root directory, and displays the progress using the echo command. The progress message is updated after each root directory is processed. The rest of the script is the same as the previous version.