Created
April 13, 2017 11:41
-
-
Save milesgratz/4f8da4d6f7fd1a6319b9fb0095121795 to your computer and use it in GitHub Desktop.
Find recursive text files and move to parent folder
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
# Define parent folder | |
$Parent = "C:\temp\Parent" | |
# Search for text files recursively | |
$Files = Get-ChildItem -Path $Parent -Filter *.txt -Recurse | |
# Loop through files, moving to Parent folder | |
foreach ($File in $Files) | |
{ | |
Try | |
{ | |
# Try moving file | |
Move-Item $File.FullName -Destination $Parent -ErrorAction Stop | |
} | |
Catch | |
{ | |
# Duplicate filename found (throw error) | |
$_ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment