Bonjour,
J'aurai besoin d'aide svp, je voudrais rajouter une ligne dans mon programme qui permettrait de diviser mon fichier .txt en plusieurs parties de 20 Ko.
Merci de bien vouloir m'aider.
Bonjour,
J'aurai besoin d'aide svp, je voudrais rajouter une ligne dans mon programme qui permettrait de diviser mon fichier .txt en plusieurs parties de 20 Ko.
Merci de bien vouloir m'aider.
Pouvez-vous nous poster votre code et votre tentative pour la modification. et
![]()
J'ai 2 programmes :
- Un qui permet de réunir des lignes de fichiers dans un seul et même fichier, voici le code :
Code PowerShell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 #Selectionne les 10000 permières lignes des fichier qui commencent 202205 et les écrit dans semaine_du_16-05 Get-Content "T:\EDI\GARELLA\ASTI VERS Y2\Archives\AE_202205*.ASTI" | select -First 10000 | Out-File "T:\EDI\GARELLA\ASTI VERS Y2\Archives\semaine_du_16-05.txt" #Supprime les lignes qui contiennent ANHC1I799000206 et supprime les doublons ( Sort-Object -unique ) (Get-Content "T:\EDI\GARELLA\ASTI VERS Y2\Archives\semaine_du_16-05.txt") -notmatch "ANHC1I799000206" | Sort-Object -unique | Out-File "T:\EDI\GARELLA\ASTI VERS Y2\Archives\semaine_du_16-05.txt"
- Un second qui reprend le code que vous m'avez envoyé :
Code PowerShell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 function Split-File { param ( [Parameter(Mandatory)] [String] $Path, [Int32] $PartSizeBytes = 20Ko ) try { # get the path parts to construct the individual part # file names: $fullBaseName = [IO.Path]::GetFileName($Path) $baseName = [IO.Path]::GetFileNameWithoutExtension($Path) $parentFolder = [IO.Path]::GetDirectoryName($Path) $extension = [IO.Path]::GetExtension($Path) # get the original file size and calculate the # number of required parts: $originalFile = New-Object System.IO.FileInfo($Path) $totalChunks = [int]($originalFile.Length / $PartSizeBytes) + 1 $digitCount = [int][Math]::Log10($totalChunks) + 1 # read the original file and split into chunks: $reader = [IO.File]::OpenRead($Path) $count = 0 $buffer = New-Object Byte[] $PartSizeBytes $moreData = $true # read chunks until there is no more data while($moreData) { # read a chunk $bytesRead = $reader.Read($buffer, 0, $buffer.Length) # create the filename for the chunk file $chunkFileName = "$parentFolder\$fullBaseName.{0:D$digitCount}.txt" -f $count Write-Verbose "saving to $chunkFileName..." $output = $buffer # did we read less than the expected bytes? if ($bytesRead -ne $buffer.Length) { # yes, so there is no more data $moreData = $false # shrink the output array to the number of bytes # actually read: $output = New-Object Byte[] $bytesRead [Array]::Copy($buffer, $output, $bytesRead) } # save the read bytes in a new part file [IO.File]::WriteAllBytes($chunkFileName, $output) # increment the part counter ++$count } # done, close reader $reader.Close() } catch { throw "Unable to split file ${Path}: $_" } }
Merci pour votre aide![]()
Juste qu'il faut remplacer Ko en KB
Code Powershell : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 Clear-Host function Split-File { param ( [Parameter(Mandatory)] [String] $Path, [Int32] $PartSizeBytes = 20KB ) try { # get the path parts to construct the individual part # file names: $fullBaseName = [IO.Path]::GetFileName($Path) $baseName = [IO.Path]::GetFileNameWithoutExtension($Path) $parentFolder = [IO.Path]::GetDirectoryName($Path) $extension = [IO.Path]::GetExtension($Path) # get the original file size and calculate the # number of required parts: $originalFile = New-Object System.IO.FileInfo($Path) $totalChunks = [int]($originalFile.Length / $PartSizeBytes) + 1 $digitCount = [int][Math]::Log10($totalChunks) + 1 # read the original file and split into chunks: $reader = [IO.File]::OpenRead($Path) $count = 0 $buffer = New-Object Byte[] $PartSizeBytes $moreData = $true # read chunks until there is no more data while($moreData) { # read a chunk $bytesRead = $reader.Read($buffer, 0, $buffer.Length) # create the filename for the chunk file $chunkFileName = "$parentFolder\$fullBaseName.{0:D$digitCount}_part.txt" -f $count Write-Verbose "saving to $chunkFileName..." $output = $buffer # did we read less than the expected bytes? if ($bytesRead -ne $buffer.Length) { # yes, so there is no more data $moreData = $false # shrink the output array to the number of bytes # actually read: $output = New-Object Byte[] $bytesRead [Array]::Copy($buffer, $output, $bytesRead) } # save the read bytes in a new part file [IO.File]::WriteAllBytes($chunkFileName, $output) # increment the part counter ++$count } # done, close reader $reader.Close() } catch { throw "Unable to split file ${Path}: $_" } } # Appler cette fonction comme dans cet example,et remplacer votre chemin du dossier par le votre Split-File -Path "C:\Chemin du dossier\Test.txt" -PartSizeBytes 20KB -Verbose
Partager