Skip to content

Instantly share code, notes, and snippets.

@davidsk
Last active March 15, 2022 08:57
Show Gist options
  • Save davidsk/2c59632bbf37715e3495d29f66769eab to your computer and use it in GitHub Desktop.
Save davidsk/2c59632bbf37715e3495d29f66769eab to your computer and use it in GitHub Desktop.
Script to move a folder structure using git mv
# find all files
Get-ChildItem -Recurse -File | % {
$sourceDirectory = Split-Path -Path $_.FullName -Parent;
$sourceFileName = Split-Path -Path $_.FullName -Leaf;
# $sourceDirectory;
# $sourceFileName;
# replace old path with new path
$targetDirectory = $sourceDirectory.Replace("Areas\Application\Views", "Modules\Pages");
# optional: replace file extension
$targetFileName = $sourceFileName.Replace(".cshtml", ".js")
$targetFullPath = Join-Path -Path $targetDirectory -ChildPath $targetFileName
# check whether file already exists at destination
if((Test-Path -Path $targetFullPath) -eq $false)
{
# target folder does not exist (git mv won't create it either 😥)
if((Test-Path -Path $targetDirectory) -eq $false)
{
# create target folder
md $targetDirectory;
}
# move file to new location retaining history
git mv (Join-Path -Path $sourceDirectory -ChildPath $sourceFileName) (Join-Path -Path $targetDirectory -ChildPath $targetFileName)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment