Question
Create new folder with copy when not existing
- Warrer
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 5
- Remerciements reçus 0
il y a 13 ans 4 mois #13057
par Warrer
Create new folder with copy when not existing a été créé 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
). 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
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
[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.
- Matthew BETTON
- Hors Ligne
- Membre platinium
-
Réduire
Plus d'informations
- Messages : 968
- Remerciements reçus 0
il y a 13 ans 4 mois #13058
par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:Create new folder with copy when not existing
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
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.
- Warrer
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 5
- Remerciements reçus 0
il y a 13 ans 4 mois #13062
par Warrer
Réponse de Warrer sur le sujet Re:Create new folder with copy when not existing
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
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.
- Matthew BETTON
- Hors Ligne
- Membre platinium
-
Réduire
Plus d'informations
- Messages : 968
- Remerciements reçus 0
il y a 13 ans 4 mois #13063
par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:Create new folder with copy when not existing
By adding the '-Force' Parameter to the 'Get-ChildItem' Cmdlet.
[code:1]$users = Get-childitem C:\Users -exclude Public -Force[/code:1]
[code:1]$users = Get-childitem C:\Users -exclude Public -Force[/code:1]
Connexion ou Créer un compte pour participer à la conversation.
- Matthew BETTON
- Hors Ligne
- Membre platinium
-
Réduire
Plus d'informations
- Messages : 968
- Remerciements reçus 0
il y a 13 ans 4 mois #13064
par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:Create new folder with copy when not existing
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
dir is an Alias 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.
- Matthew BETTON
- Hors Ligne
- Membre platinium
-
Réduire
Plus d'informations
- Messages : 968
- Remerciements reçus 0
il y a 13 ans 4 mois #13065
par Matthew BETTON
Réponse de Matthew BETTON sur le sujet Re:Create new folder with copy when not existing
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.051 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Create new folder with copy when not existing