24

I have a Powershell script that moves files:

$from = $path + '\' + $_.substring(8)
$to   = $quarantaine + '\' + $_.substring(8)

Move-Item $from $to

The directory structure in the $to path doesn't exist so I would like Powershell to create it with Move-Item. I've tried adding -Force but that didn't work.

What can I do to make sure Powershell creates the needed directories in $to?

3
  • See this question: stackoverflow.com/questions/2695504/… Commented Dec 17, 2012 at 10:15
  • That's what I tried already, with no luck. As mentioned in my question Commented Dec 17, 2012 at 10:17
  • 1
    Could do a copy and delete. Commented Dec 17, 2012 at 10:20

3 Answers 3

22

You could create it yourself:

$from = Join-Path $path $_.substring(8)
$to = Join-Path $quarantaine $_.substring(8)

if(!(Test-Path $to))
{
    New-Item -Path $to -ItemType Directory -Force | Out-Null
}

Move-Item $from $to
Sign up to request clarification or add additional context in comments.

4 Comments

-PathType Container is not a parameter for New-Item. Remove this and it works as expected.
Is there no easier solution nowadays - like an parameter or so
@mkmpf It seems not, but you can make a function such as stackoverflow.com/a/70508397/11434872: Function Confirm-MoveItem([string]$path, [string]$destination) { if (!(Test-Path "$destination\..")) { New-Item -Path "$destination\.." -ItemType Directory -Force | Out-Null } Move-Item -Path $path -Destination $destination -Force }
I would remove the Test-Path conditional and just always call New-Item at this point.
6

You could use the system.io.directory .NET class to check for destination directory and create if it doesn't exist. Here is an example using your variables:-

if (!([system.io.directory]::Exists($quarantine))){
   [system.io.directory]::CreateDirectory($quarantine)
}
Copy-File $from $to

5 Comments

Why would you use this over the simpler and more common Test-Path/New-Item?
@NelsonRothermel At the time of writing I was new to powershell, unaware of them and the answer I gave was how I would do this at the time - this was 4 years ago. These days I would indeed use test-path/new-item.
@NelsonRothermel it's actually something I find pretty nice about powershell though - if you come into it with a .NET background, a lot of what you are used to is portable and reusable in powershell with ease. And if there's not a cmdlet that does what you need, but there's a .NET class that does, problem solved!
Perfect, thanks. I was just wondering if there was something I didn't know. I do like how you can fall back on full .NET as needed or even write your own.
Actually you don't need Exists($quarantine) with CreateDirectory because it does it internally anyways. Even better, if you have access to \\server\share\my\path directory but not \\server\share\my directory Exists(@"\\server\share\my\path\subfolder") will fail but CreateDirectory(@"\\server\share\my\path\subfolder") won't.
1

You could also add a utility function like this:

Function Move-Item-Parent([string]$path, [string]$destination) {
    New-Item $destination -ItemType Directory -Force
    Move-Item $path $destination
}

Then invoke it like this:

Move-Item-Parent $from $to

Comments

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.