mirror of
https://github.com/serialexperiments0815/PowershellExcercises.git
synced 2026-11-07 12:55:30 +00:00
Expanded and structurized scripts.
This commit is contained in:
parent
361e384195
commit
fbcf753965
33 changed files with 158 additions and 0 deletions
|
|
@ -0,0 +1,14 @@
|
|||
# Deletes all objects older than 30 days inside the folder.
|
||||
# -WhatIf Parameter was chosen for Remove-Item to remove the necessity for file -recreation
|
||||
# Should creation of an file older than 30 days for testing purposes be necessary, run this script:
|
||||
|
||||
# $file = "./testing.txt"
|
||||
# New-Item -Path $file -ItemType File -Force
|
||||
# $oldDate = (Get-Date).AddDays(-31)
|
||||
# (Get-Item $file).LastWriteTime = $oldDate
|
||||
# (Get-Item $file).LastAccessTime = $oldDate
|
||||
|
||||
Get-ChildItem ".\" -Recurse |
|
||||
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
|
||||
Remove-Item -Force -WhatIf
|
||||
|
||||
0
2 - Simple Automation/deleteOlderThan30Days/testing.txt
Normal file
0
2 - Simple Automation/deleteOlderThan30Days/testing.txt
Normal file
|
|
@ -0,0 +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 |
|
||||
Select-Object -Property FullName |
|
||||
Export-Csv -Path "./folderFileList.csv" -NoTypeInformation
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
0
2 - Simple Automation/fileFolderSorting/empty copy.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty copy.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty.png
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# Assign all relevant file-extensions to arrays to easily filter through them during a for-loop
|
||||
# This approach was choosen for its simplicity and visibility
|
||||
|
||||
[String[]]$videoFormat = "mp4", "mkv", "mov", "avi", "wmv", "flv", "webm", "mpeg", "mpg", "m4v", "3gp", "ogv", "ts", "vob"
|
||||
[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
|
||||
|
||||
foreach($file in $filesInFolder){
|
||||
$extension = $file.Extension.TrimStart('.').ToLower()
|
||||
|
||||
if ($videoFormat -contains $extension){
|
||||
Move-Item -Path $file.FullName -Destination "./videos/$($file.Name)"
|
||||
}
|
||||
|
||||
elseif ($pictureFormat -contains $extension) {
|
||||
Move-Item -Path $file.FullName -Destination "./pictures/$($file.Name)"
|
||||
}
|
||||
|
||||
elseif ($documentFormat -contains $extension) {
|
||||
Move-Item -Path $file.FullName -Destination "./documents/$($file.Name)"
|
||||
}
|
||||
|
||||
}
|
||||
0
2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/test copy.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test copy.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test.mp4
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# File searches for txt files and replaces specified string.
|
||||
|
||||
$textFiles = Get-ChildItem -Path "./" -Filter *.txt
|
||||
$searchText = 'this text is false'
|
||||
$replaceText = 'this text is true'
|
||||
foreach($textFile in $textFiles){
|
||||
(Get-Content $textFile) -replace $searchText, $replaceText | Set-Content $textFile
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
this text is false
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
this text is false
|
||||
|
||||
7
2 - Simple Automation/folderBackup/folderBackup.ps1
Normal file
7
2 - Simple Automation/folderBackup/folderBackup.ps1
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Copy 'important' folder to 'safe' location
|
||||
# Get-ChildItem -Filter parameter was deemed insufficient and replaced by -Directory | Where-Object
|
||||
# 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
|
||||
|
|
@ -0,0 +1 @@
|
|||
important
|
||||
Loading…
Add table
Add a link
Reference in a new issue