Created
August 19, 2020 13:19
-
-
Save jessebutryn/a6d6287fd9d03b71fe70394e927a12b0 to your computer and use it in GitHub Desktop.
powershell script to make directories for each file in a directory and move those files into them.
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
# This script will make directories for all files in a specified directory and move them into it. | |
# You must provide the input directory as the first argument and can optionally specify an output | |
# directory as the second argument. If no output directory is specified the input directory will | |
# be used. | |
$inDir = $args[0] | |
$outDir = $args[1] | |
$isInDir = Test-Path -LiteralPath $inDir -PathType Container | |
if ($outDir -ne $null) { | |
$isOutDir = Test-Path -LiteralPath $outDir -PathType Container | |
} | |
if ($isInDir -eq $false) { | |
throw "Error: $inDir : is not a directory" | |
} | |
if ($isOutDir -eq $false) { | |
throw "Error: $outDir : is not a directory" | |
} | |
$files = Get-ChildItem -LiteralPath $inDir -File | |
foreach ($f in $files) { | |
$fExt = $f.Extension | |
if ($outDir -ne $null) { | |
$fDir = -join($outDir,"\",$f.BaseName) | |
} else { | |
$fDir = $f.FullName.Replace($fExt,"") | |
} | |
New-Item -Path $fDir -ItemType Directory | Out-Null | |
Move-Item -LiteralPath $f.FullName -Destination $fDir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment