From 9dc9824b12943a05263713aa039833e4c7b9e645 Mon Sep 17 00:00:00 2001 From: serialexperiments0815 <112315380+serialexperiments0815@users.noreply.github.com> Date: Sat, 18 Oct 2025 16:59:55 +0200 Subject: [PATCH] Corrected many "./" path entries to $($PSScriptRoot). --- .../count-files-of-specific-type.ps1 | 2 +- 1 - Basics/list-files-in-folder/list-files-in-folder.ps1 | 4 ++-- .../rename-files-in-folder-by-pattern.ps1 | 2 +- .../deleteOlderThan30Days/deleteOlderThan30Days.ps1 | 2 +- .../exportFolderFileListToCSV.ps1 | 4 ++-- .../fileFolderSorting/fileFolderSorting.ps1 | 8 ++++---- .../findAndReplaceTextMultipleFiles.ps1 | 2 +- 2 - Simple Automation/folderBackup/folderBackup.ps1 | 4 ++-- .../generateReportOfInstalledPrograms.ps1 | 6 +++--- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 b/1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 index b9cb330..f9fe984 100644 --- a/1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 +++ b/1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 @@ -4,4 +4,4 @@ # '-File' returns results for files only, no directories. # '-Filter' returns results for specified string pattern. -Get-ChildItem -Path "./" -Recurse -File -Filter *.ps1 \ No newline at end of file +Get-ChildItem -Path "$($PSScriptRoot)" -Recurse -File -Filter *.ps1 \ No newline at end of file diff --git a/1 - Basics/list-files-in-folder/list-files-in-folder.ps1 b/1 - Basics/list-files-in-folder/list-files-in-folder.ps1 index cac3b71..c7aaca8 100644 --- a/1 - Basics/list-files-in-folder/list-files-in-folder.ps1 +++ b/1 - Basics/list-files-in-folder/list-files-in-folder.ps1 @@ -3,7 +3,7 @@ # '-Recurse' instructs iterations through subdirectories. # '-File' returns results for files only, no directories. -Get-ChildItem -Path './' -Recurse -File | +Get-ChildItem -Path "$($PSScriptRoot)" -Recurse -File | # Call 'Select-Object' cmdlet method to return specified properties. # '-Property FullName' returns full String path for each object. @@ -15,4 +15,4 @@ Get-ChildItem -Path './' -Recurse -File | # '-Path './folderFiles.csv'' returns output of file in current directory. # '-NoTypeInformation' supresses #TYPE metadata line. - Export-Csv -Path './folderFiles.csv' -NoTypeInformation \ No newline at end of file + Export-Csv -Path "$($PSScriptRoot)/folderFiles.csv" -NoTypeInformation \ No newline at end of file diff --git a/1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 b/1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 index e2fe93b..4899ec0 100644 --- a/1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 +++ b/1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 @@ -2,7 +2,7 @@ # "./" In current directory. # "-Filter *.PNG" Of the type PNG. -Get-ChildItem "./" -Filter *.PNG | +Get-ChildItem "$($PSScriptRoot)" -Filter *.PNG | # Calling 'ForEach-Object' loop method for iterating through returned files. ForEach-Object { diff --git a/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 b/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 index 21673fe..cee8809 100644 --- a/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 +++ b/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 @@ -8,7 +8,7 @@ # (Get-Item $file).LastWriteTime = $oldDate # (Get-Item $file).LastAccessTime = $oldDate -Get-ChildItem ".\" -Recurse | +Get-ChildItem "$($PSScriptRoot)" -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force -WhatIf diff --git a/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 b/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 index 72ec2bc..aa18519 100644 --- a/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 +++ b/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 @@ -1,5 +1,5 @@ # Export all file and folder path to folderFileList.csv # Select-Object FullName property was chosen to only export the full path. -Get-ChildItem -Path "./" -Recurse | +Get-ChildItem -Path "$($PSScriptRoot)" -Recurse | Select-Object -Property FullName | -Export-Csv -Path "./folderFileList.csv" -NoTypeInformation \ No newline at end of file +Export-Csv -Path "$($PSScriptRoot)/folderFileList.csv" -NoTypeInformation \ No newline at end of file diff --git a/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 b/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 index 4433834..ca89e7b 100644 --- a/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 +++ b/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 @@ -5,21 +5,21 @@ [String[]]$pictureFormat = "jpg", "jpeg", "png", "gif", "bmp", "tiff", "tif", "webp", "heic", "heif", "raw", "cr2", "nef", "arw", "svg", "ico" [String[]]$documentFormat = "pdf", "doc", "docx", "odt", "rtf", "txt", "csv", "xls", "xlsx", "ods", "ppt", "pptx", "odp", "md", "html", "xml" -$filesInFolder = Get-ChildItem -Path "./" -File +$filesInFolder = Get-ChildItem -Path "$($PSScriptRoot)" -File foreach($file in $filesInFolder){ $extension = $file.Extension.TrimStart('.').ToLower() if ($videoFormat -contains $extension){ - Move-Item -Path $file.FullName -Destination "./videos/$($file.Name)" + Move-Item -Path $file.FullName -Destination "$($PSScriptRoot)/videos/$($file.Name)" } elseif ($pictureFormat -contains $extension) { - Move-Item -Path $file.FullName -Destination "./pictures/$($file.Name)" + Move-Item -Path $file.FullName -Destination "$($PSScriptRoot)/pictures/$($file.Name)" } elseif ($documentFormat -contains $extension) { - Move-Item -Path $file.FullName -Destination "./documents/$($file.Name)" + Move-Item -Path $file.FullName -Destination "$($PSScriptRoot)/documents/$($file.Name)" } } \ No newline at end of file diff --git a/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 b/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 index 0bc4fb1..4f4cdef 100644 --- a/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 +++ b/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 @@ -1,6 +1,6 @@ # File searches for txt files and replaces specified string. -$textFiles = Get-ChildItem -Path "./" -Filter *.txt +$textFiles = Get-ChildItem -Path "$($PSScriptRoot)" -Filter *.txt $searchText = 'this text is false' $replaceText = 'this text is true' foreach($textFile in $textFiles){ diff --git a/2 - Simple Automation/folderBackup/folderBackup.ps1 b/2 - Simple Automation/folderBackup/folderBackup.ps1 index 629850e..1060f52 100644 --- a/2 - Simple Automation/folderBackup/folderBackup.ps1 +++ b/2 - Simple Automation/folderBackup/folderBackup.ps1 @@ -3,5 +3,5 @@ # As the -Filter parameter only matches folders at current level, not recursively in all subfolders. $nameOfFolder = "important" -$directionOfFolder = Get-ChildItem -Path "./" -Recurse -Directory | Where-Object { $_.Name -eq $nameOfFolder } -Copy-Item -Path $directionOfFolder.FullName -Destination (Join-Path "./safe/" $directionOfFolder.Name) -Recurse -Force +$directionOfFolder = Get-ChildItem -Path "$($PSScriptRoot)" -Recurse -Directory | Where-Object { $_.Name -eq $nameOfFolder } +Copy-Item -Path $directionOfFolder.FullName -Destination (Join-Path "$($PSScriptRoot)/safe/" $directionOfFolder.Name) -Recurse -Force diff --git a/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 b/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 index 037a45d..95b0eed 100644 --- a/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 +++ b/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 @@ -3,8 +3,8 @@ # Test-Path added to avoid Remove-Item error on first execution. # Remove-Item was added to avoid complications with Export-Csv function. # Delimiter was added to not have all properties stacked in the same column. -if (Test-Path "./InstalledPrograms.csv"){ -Remove-Item -Path "./InstalledPrograms.csv" +if (Test-Path "$($PSScriptRoot)/InstalledPrograms.csv"){ +Remove-Item -Path "$($PSScriptRoot)/InstalledPrograms.csv" } Get-Package | Select-Object -Property Name, Version, Summary | -Export-Csv -Path "./InstalledPrograms.csv" -NoTypeInformation -Delimiter ";" +Export-Csv -Path "$($PSScriptRoot)/InstalledPrograms.csv" -NoTypeInformation -Delimiter ";"