1

I am new to PowerShell and am stuck at something.

How to move files based on filename using PowerShell?

Move 0001_ddmmyy_username[active1]_L_kkkkk.pdf to C:\Users\abc\London
Move 0001_ddmmyy_jacky[active1]_R_kkkkk.pdf to C:\Users\abc\Russia
Move 0001_ddmmyy_jim[active1]_P_kkkkk.pdf to C:\Users\abc\Poland

I used the following code to move files to a folder called London. It fails if the file name has any square bracket with text [kkkk].

gci 'C:\Users\abc' -Recurse -Include "*_L*" | %{ Move-Item $_.FullName 'C:\Users\abc\London'}

How can I automate this?

1
  • 1
    with some cmdlets, you will get odd results when you use -Path and any part of the full file name contains []. it also happens with some other chars, but those are the only ones i recall. the fix is to use the -LiteralPath parameter instead of the -Path parameter. you are using the -Path parameter since you failed to specify anything - and the cmdlets default to -Path. take a look at the output of Get-Help Move-Item -Parameter *path*. Commented May 13, 2020 at 3:36

2 Answers 2

1
  1. There are multiple ways:

Get-Location

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-location?view=powershell-7

gci (Get-Location).Path

or simply .\

gci .\

additionaly, gci will default to current path if none is specified. So the following will get the current folder:

gci
  1. Use -LiteralPath in the Move-Item cmdlet.
Move-Item -LiteralPath $sourcePath $destinationPath
  1. I'd do something similar to below:
# Note I'm using -File so it only returns files
$items = gci .\ -Recurse -File -Include *_L*, *_R*, *_P*

foreach($item in $items) {
  # Could utilize something cleaner/better but this will do
  if($item.Name -like "*_L*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\London'
  } elseif($item.Name -like "*_R*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\Russia'
  } elseif ($item.Name -like "*_P*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\Poland'
  } else {
    # Unexpected output
    Write-Host "The path did not match what was expected"
    Write-Host $item.Name
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello My Friend, This code gives me the exact same output as my code for _L. When the file name has some text with [xxxxxx] before the _L or _R or _P , then it will not move it. Can you also advice on creating the folder London and moving all _L into it.
Hey, as per the response from @somebadhat, using -LiteralPath will fix the issue with the brackets.
His solution works perfectly fine, just looking at a minor improvement too. 1. Check and create the folders within the .\ and also to skip or overwrite if files exist.
0

Windows 10 64-bit. PowerShell 5.

How to move files based on file basename using PowerShell gci, foreach, if, -like, move-item and -literalpath

Move 0001_ddmmyy_username[active1]_L_kkkkk.pdf to C:\Users\abc\London
Move 0001_ddmmyy_jacky[active1]_R_kkkkk.pdf to C:\Users\abc\Russia
Move 0001_ddmmyy_jim[active1]_P_kkkkk.pdf to C:\Users\abc\Poland

Change $env:userprofile\desktop. Remove -whatif when you are satisfied it works.

$items = gci -recurse -file *_L_*, *_R_*, *_P_*
foreach($item in $items) {
  # Change $env:userprofile\desktop
  if($item -like "*_L_*") {
    Move-Item -LiteralPath $item "$env:userprofile\desktop\London" -whatif
  } elseif($item -like "*_R_*") {
    Move-Item -LiteralPath $item "$env:userprofile\desktop\Russia" -whatif
  } elseif ($item -like '*_P_*') {
    Move-Item -LiteralPath $item "$env:userprofile\desktop\Poland" -whatif 
  }
} 

Comments:

With some cmdlets you will get odd results when you use -Path and any part of the full file name contains []. The fix is to use the -LiteralPath parameter. See Get-Help Move-Item -Parameter *path* – Lee_Dailey

How to move files based on file basename using PowerShell gci, foreach, if, -like, move-item and -literalpath

4 Comments

Hello My friend, It works perfectly, a minor addition is to have 1. Create folder if it does not exist and then move it based on the current directory 2. Check if files with same name exists and if yes do not move or replace it Really appreciate your help
Noted and sorry about that as I was new to the forum.
@somebadhat I have created a new thread. Could you please help?
@cur comment to me from your new post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.