Last active
May 17, 2020 13:29
-
-
Save BadgerCode/beb72cc4325a48a0a30a12d587cc96e0 to your computer and use it in GitHub Desktop.
Uses powershell to remove files from one directory, if they exist in the same relative path in another directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Folder to REMOVE files from | |
$targetPath = "C:\Users\mike-\Desktop\csgo\" | |
# Folder to CHECK for files | |
$sourcePath = "C:\Users\mike-\Desktop\css\" | |
# Sub-folder that exists in both folders | |
$assetFolder = "models" | |
# Set to $true to output duplicates | |
# Set to $false to delete duplicates | |
$testRun = $true | |
$fullTargetPath = "$($targetPath)$($assetFolder)\" | |
Write-Host "Files will be deleted from $fullTargetPath" | |
Start-Sleep -Seconds 4 | |
$targetFiles = Get-ChildItem -File -Recurse $fullTargetPath | |
for ($i=0; $i -lt $targetFiles.Count; $i++) { | |
$file = $targetFiles[$i] | |
$targetPathForFile = $file.FullName | |
$relativeFilePath = $targetPathForFile.Replace($targetPath, "") | |
$sourcePathForFile = "$($sourcePath)$($relativeFilePath)" | |
if ((Test-Path $sourcePathForFile)) { | |
Write-Host "Duplicate file- $relativeFilePath" | |
if($testRun -eq $false) { | |
Remove-Item $targetPathForFile | |
} | |
} | |
} | |
if($testRun -eq $true) { | |
Write-Host "Files will be deleted from $fullTargetPath" | |
} | |
else { | |
Write-Host "Files have been deleted from $fullTargetPath" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment