I’m currently working on a personal project and i’m realitvely new to writing powershell scripts.
The use case for the script is to backup screenshots that get automatically captured during certain events within a game, while also organising them. At the minute they just get saved into generic folders and my OCD brain has been manually organising them, and thought a fun project would be to script this.
The primary function of the script is to check for the existence of a folder and then files within the folder with specific words in the file names, then create a new folder if required and copy those certain files to the new folder. (hopefully makes sense)
I have the script working but i’m thinking there may be a simpler way of having the script so that its not doubled up.
The script as it sits at the minute is below:
# Screenshots default folder:
$game="$env:Userprofilegamescreenshots"
# Folder to copy the screenshots to:
$targetfolder="E:_GameScreenshots"
# Name of game account being organised:
$accountname="username"
# Folder name
$folder="event name"
If (Get-ChildItem -Path $game$accountname$folder -Filter "*File Name*")
{ Write-Host "Files Exist" -ForegroundColor Green
If (-not(Test-Path "$targetfolder$accountname$folderFolder Name"))
{ New-Item -ItemType Directory "$targetfolder$accountname$folderFolder Name" |Out-Null
Write-Host "Folder Exists or created, copying screenshots..." -ForegroundColor Cyan
robocopy "$game$accountname$folder" "$targetfolder$accountname$folderFolder Name" /COPYALL /IF "*File Name*.*" |Out-Null}
Else {
Write-Host "Folder Exists or created, copying screenshots..." -ForegroundColor Cyan
robocopy "$game$accountname$folder" "$targetfolder$accountname$folderFolder Name" /COPYALL /IF "*File Name*.*" |Out-Null}
}
Any thoughts and advice will be greatly appreciated.
Toby Hayton is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.