Question Create new folder with copy when not existing

Plus d'informations
il y a 13 ans 4 mois #13057 par Warrer
Hello all,

I'm trying to create the destination folder when this one does not exist and then copy a file into it. For what I know my code is supposed to be right (but it is not otherwise I won't be asking your help :p). It doesn't do the trick:

[code:1]
#Destination Folder
$Office =\"AppData\Local\Microsoft\Office\"
#File to copy
$OfficeUIFile =\"./olkapptitem.officeUI\"
$users = Get-childitem C:\Users -exclude Public

$MyScriptParentPath = Split-Path -Path $MyInvocation.mycommand.path -Parent
$users | % {
$Destination = join-path $_.fullname $Office
if (test-path $Destination)
{
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
}
else{
New-Item $Office -ItemType Directory -Force
Copy-Item (join-path $MyScriptParentPath $OfficeUIFile) -Destination $Destination -Force
Write-Host \"$Destination did not exist and has been created\"
}

}
[/code:1]

What I've got with this code when I execute it is just a blank file without extension called Office in the path AppData\Local\Microsoft\

Thanks for your help :)

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13058 par Matthew BETTON
Hello,

Welcome in the French PowerShell Forum Community ;)

I think there is a mistake here :

[code:1]
else{

New-Item $Office -ItemType Directory -Force

[/code:1]

Instead, please try this :


[code:1]
else{

New-Item $Destination -ItemType Directory -Force

[/code:1]

So replace the '$Office' variable by '$Destination', as $Office is not a full Path.

Cordialy

Matthew

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13062 par Warrer
Great!! that worked :)

Thanks a lot but I forgot to add something else to my request, how do I include hidden folders like \"Default\" in the C:\Users

Thanks again in advance :)

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13063 par Matthew BETTON
By adding the '-Force' Parameter to the 'Get-ChildItem' Cmdlet.

[code:1]$users = Get-childitem C:\Users -exclude Public -Force[/code:1]

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13064 par Matthew BETTON
In PowerShell Version 3, you can also use the new functionnality of Get-ChildItem :

Use PowerShell 3.0 to Filter Files Based on Advanced Attributes

The alias for the Hidden switch, it is ah. The following command retrieves hidden files.

dir -File -ah


dir is an Alias of Get-ChildItem ...

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 13 ans 4 mois #13065 par Matthew BETTON

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.051 secondes
Propulsé par Kunena