Question [Résolu] Copie de fichiers dans sous-rep

Plus d'informations
il y a 7 ans 6 mois #27534 par Thierry Le Chevillier
Bonjour à tous,

J'ai récupéré sur le net un script qui est censé trier les fichiers d'un répertoire et les copier dans des sous-répertoires d'une taille prédéfinie.

Mais ça marche pô.

Quelqu'un aurait-il le loisir de m'indiquer ce qu'il faudrait modifier ? (Je n'ai aucune expérience en programmation, hormis celle de mon réveil-matin ;-)

Merci d'avance

Thierry

Annexe N°1 : le script

[code:1]
# Replace with your base folder's path
$baseFolder = M:\MesVideos

# Replace with the desired value for the subfolders to create
$maxSubFolderSize = 500MB

# Get all files contained in your base folder (could use the -recurse switch if needed)
$allFiles = Get-ChildItem $baseFolder

# Setting the subfolders naming convention : a name and a suffix
$baseSubFolder = SubFolder-
[int]$index = 0

# Creating the first subfolder
$subFolder = \"SubFolder-\" + \"$index\"
New-Item -Path $subFolder -Type Directory -Force

# Now processing the files
foreach ($file in $allFiles)
{
# Evaluating the size of the current subfolder
$subFolderSize = ((Get-ChildItem $subFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB«»)

# If the current subfolder size is greater than the limit, create a new subfolder and begin to copy files in it
if([int]$subFolderSize -gt [int]$maxSubFolderSize/1MB«»)
{
$index++
$subFolder = $baseSubFolder + $index
New-Item -Path $subFolder -Type Directory -Force
Write-Verbose -Message \"Created folder $subFolder\"
Move-Item $file.FullName -Destination $subFolder
}
# If the current subfolder is not yet greater that the limit, continue copying files in it
else {
Move-Item $file.FullName -Destination $subFolder
}
}
[/code:1]

Annexe N°2 : Exemple d'erreur

[code:1]

Impossible de convertir la valeur « 10737418240 » en type « System.Int32 ».
Erreur : « La valeur était trop grande ou trop petite pour un Int32. »
Au niveau de C:\MoveFilesLimit.ps1 : 25 Caractère : 54
+ if([int]$subFolderSize -gt [int]$maxSubFolderSize <<<< /1MB«»)
+ CategoryInfo : NotSpecified: (:«») [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

[/code:1]<br><br>Message édité par: Arnaud, à: 13/09/18 10:17

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

Plus d'informations
il y a 7 ans 6 mois #27535 par Thierry Le Chevillier
Ça y est, j'ai trouvé moi-même l'erreur et cela fonctionne.

Il fallait supprimer les espaces $maxSubFolderSize=500MB

Merci de votre attention, vous pouvez reprendre une activité normale

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

Plus d'informations
il y a 7 ans 6 mois #27536 par Arnaud Petitjean

Merci de votre attention, vous pouvez reprendre une activité normale


Mais il n'y a pas de quoi Thierry :P

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

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

Plus d'informations
il y a 7 ans 6 mois #27537 par Thierry Le Chevillier
En fait, ça marche pô comme je voudrais.

Pour l'instant les sous-répertoires sont créés dans c:/USER/MonProfil alors ce qui m'irait bien, c'est que tout reste dans le répertoire de base, c-a-d $baseFolder = M:\MesVideos

Comment fait-on cela ?
Je teste différentes options depuis une heure, rien ne marche.

#oscour<br><br>Message édité par: ThierryLC, à: 12/09/18 16:43

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

Plus d'informations
il y a 7 ans 6 mois #27538 par Philippe
salut ThierryLC

ce qui m'irait bien, c'est que tout reste dans le répertoire de base, c-a-d $baseFolder = M:MesVideos

il faut que tu remplace cette ligne :
[code:1]$subFolder = \&quot;SubFolder-\&quot; + \&quot;$index\&quot;[/code:1]
par cette ligne :
[code:1]$subFolder = $baseFolder + \&quot;SubFolder-\&quot; + \&quot;$index\&quot;[/code:1]

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

Plus d'informations
il y a 7 ans 6 mois #27539 par Thierry Le Chevillier
Je suis maudit, le premier sous-répertoire est bien créé dans $baseFolder, mais les suivants toujours dans C:\Users\Thierry

Voici mon script actuel

[code:1]

# Replace with your base folder's path
$baseFolder = \&quot;E:\Test\&quot;

# Replace with the desired value for the subfolders to create
$maxSubFolderSize = 40MB

# Get all files contained in your base folder (could use the -recurse switch if needed)
$allFiles = Get-ChildItem $baseFolder

# Setting the subfolders naming convention : a name and a suffix
$baseSubFolder = \&quot;SubFolder-\&quot;
[int]$index = 1

# Creating the first subfolder
$subFolder = $baseFolder + \&quot;SubFolder-\&quot; + \&quot;$index\&quot;
New-Item -Path $subFolder -Type Directory -Force

# Now processing the files
foreach ($file in $allFiles)
{
# Evaluating the size of the current subfolder
$subFolderSize = ((Get-ChildItem $subFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB«»)

# If the current subfolder size is greater than the limit, create a new subfolder and begin to copy files in it
if([int]$subFolderSize -gt [int]$maxSubFolderSize/1MB«»)
{
$index++
$subFolder = $baseSubFolder + $index
New-Item -Path $subFolder -Type Directory -Force
Write-Verbose -Message \&quot;Created folder $subFolder\&quot;
Move-Item $file.FullName -Destination $subFolder
}
# If the current subfolder is not yet greater that the limit, continue copying files in it
else {
Move-Item $file.FullName -Destination $subFolder
}
}

[/code:1]

et la sortie dans PowerShell ISE

[code:1]


Répertoire : E:\


Mode LastWriteTime Length Name
----

----
d---- 12/09/2018 18:11 TestSubFolder-1


Répertoire : C:\Users\Thierry


Mode LastWriteTime Length Name
----

----
d---- 12/09/2018 18:16 SubFolder-2
d---- 12/09/2018 18:16 SubFolder-3
d---- 12/09/2018 18:16 SubFolder-4
d---- 12/09/2018 18:16 SubFolder-5
d---- 12/09/2018 18:16 SubFolder-6
d---- 12/09/2018 18:16 SubFolder-7
d---- 12/09/2018 18:16 SubFolder-8
d---- 12/09/2018 18:16 SubFolder-9
d---- 12/09/2018 18:16 SubFolder-10
d---- 12/09/2018 18:16 SubFolder-11
d---- 12/09/2018 18:16 SubFolder-12
d---- 12/09/2018 18:16 SubFolder-13

[/code:1]

Message édité par: ThierryLC, à: 12/09/18 17:33<br><br>Message édité par: ThierryLC, à: 12/09/18 17:34

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

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