generated from ScoopInstaller/BucketTemplate
-
Notifications
You must be signed in to change notification settings - Fork 26
/
SearchForJSONS.ps1
81 lines (66 loc) · 2.96 KB
/
SearchForJSONS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<#
.SYNOPSIS
Searches for JSON files in subdirectories, counts them, and copies them to a separate folder.
.DESCRIPTION
This PowerShell script searches for JSON files in the current directory and its subdirectories. It counts the number of JSON files found and then copies them to a separate folder named "JSONS" located at the same location as the script. If there are any filename conflicts during the copying process, the script renames the conflicting files by appending numbers to the filenames.
.NOTES
- Make sure to place this script in the directory where you want to start the search for JSON files.
- If the "JSONS" folder already exists, the script will copy the JSON files into that folder without overwriting any existing files.
.EXAMPLE
.\json_search.ps1
Runs the script to search for JSON files, count them, and copy them to the "JSONS" folder.
#>
# Define the root directory to search for JSON files
$rootDirectory = Get-Location
# Create the JSONS folder if it doesn't exist
$jsonsFolder = Join-Path -Path $rootDirectory -ChildPath "JSONS"
if (-not (Test-Path -Path $jsonsFolder)) {
New-Item -ItemType Directory -Path $jsonsFolder | Out-Null
}
# Search for JSON files in subdirectories
$jsonFiles = Get-ChildItem -Path $rootDirectory -Recurse -Filter "*.json"
# Count the number of JSON files found
$jsonCount = $jsonFiles.Count
# Print the count of JSON files found
Write-Host "Number of JSON files found: $jsonCount"
# Copy the JSON files to the JSONS folder
$jsonFiles | ForEach-Object -Begin {
$progressPreference = 'SilentlyContinue' # Disable progress stream
$progress = 0
$totalProgress = $jsonFiles.Count
$progressBar = New-Object -TypeName System.Windows.Forms.ProgressBar
$progressForm = New-Object -TypeName System.Windows.Forms.Form
$progressLabel = New-Object -TypeName System.Windows.Forms.Label
$progressLabel.Text = "Copying JSON files..."
$progressForm.Controls.Add($progressLabel)
$progressForm.Controls.Add($progressBar)
$progressForm.Text = "Progress"
$progressForm.Width = 300
$progressForm.Height = 100
$progressForm.StartPosition = 'CenterScreen'
$progressBar.Width = 280
$progressBar.Height = 20
$progressBar.Left = 10
$progressBar.Top = 30
$progressBar.Minimum = 0
$progressBar.Maximum = $totalProgress
$progressForm.Show()
} -Process {
$sourcePath = $_.FullName
$destinationPath = Join-Path -Path $jsonsFolder -ChildPath $_.Name
# Handle filename conflicts
$counter = 1
while (Test-Path -Path $destinationPath) {
$destinationPath = Join-Path -Path $jsonsFolder -ChildPath ("{0}_({1}){2}" -f $_.BaseName, $counter, $_.Extension)
$counter++
}
Copy-Item -Path $sourcePath -Destination $destinationPath -Force
# Update progress bar
$progress++
$progressBar.Value = $progress
$progressForm.Refresh()
} -End {
$progressForm.Close()
}
# Print the path of the JSONS folder
Write-Host "JSON files copied to: $jsonsFolder"