-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveFileContains.ps1
More file actions
34 lines (25 loc) · 1.26 KB
/
Copy pathMoveFileContains.ps1
File metadata and controls
34 lines (25 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$sourceFolder = Read-Host "Enter the path of the source folder";
$searchString = Read-Host "Enter the search string";
$destinationFolder = Join-Path $sourceFolder ($searchString.TrimStart(".") + ".");
if (-not (Test-Path -Path $destinationFolder)) {
New-Item -ItemType Directory -Path $destinationFolder
}
function Move-Files {
param ([string]$currentSourceFolder);
$filesToMove = Get-ChildItem -Path $currentSourceFolder -File -Filter "*$searchString*";
foreach ($file in $filesToMove) {
$relativePath = $file.FullName.Substring($sourceFolder.Length).TrimStart("\")
$destinationFilePath = Join-Path $destinationFolder $relativePath.Replace(":", "")
$destinationDir = Split-Path -Path $destinationFilePath -Parent
if (-not (Test-Path -Path $destinationDir)) {
New-Item -ItemType Directory -Path $destinationDir
}
Move-Item -Path $file.FullName -Destination $destinationFilePath
Write-Host "Moved $($file.Name) to $destinationDir"
}
$subDirs = Get-ChildItem -Path $currentSourceFolder -Directory
foreach ($subDir in $subDirs) {
Move-Files -currentSourceFolder $subDir.FullName
}
}
Move-Files -currentSourceFolder $sourceFolder