-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZip-It.ps1
101 lines (78 loc) · 3.38 KB
/
Zip-It.ps1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# This script uses 7-Zip to compress files and folders in the src/XMLsource directory into a zip file named skeleton.zip.
Write-Host "Staring the compression process..."
$currentDir = Get-Location
Write-Host "Current directory: $currentDir"
# Define the source folder and the output zip file
$sourceFolder = Join-Path -Path $currentDir -ChildPath "src/skeleton.xlsm/XMLsource/"
$outputZipFile = Join-Path -Path $currentDir -ChildPath "src/skeleton.xlsm/XMLoutput/skeleton.zip"
# Path to the 7-Zip executable
$sevenZipPath = "7z" # Assumes 7-Zip is in the system PATH. Adjust if necessary.
# Check if the source folder exists
if (-not (Test-Path $sourceFolder)) {
Write-Host "Source folder not found: $sourceFolder"
ls
exit 1
}
# Ensure the destination directory exists
$outputDir = Split-Path -Path $outputZipFile
Write-Host "Output directory: $outputDir"
if (-not (Test-Path $outputDir)) {
Write-Host "Creating output directory: $outputDir"
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
if (-not (Test-Path $sourceFolder)) {
Write-Host "Source folder not found: $sourceFolder"
exit 1
}
# Ensure the destination directory exists
$outputDir = Split-Path -Path $outputZipFile
if (-not (Test-Path $outputDir)) {
Write-Host "Creating output directory: $outputDir"
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
$absoluteSourceFolder = Resolve-Path -Path $sourceFolder
if (-not (Test-Path $absoluteSourceFolder)) {
Write-Host "Error: Source folder not found: $absoluteSourceFolder"
exit 1
}
$absoluteDestinationFolder = Resolve-Path -Path $outputDir
# Change the working directory to the source folder
Write-Host "Changing directory to $absoluteSourceFolder..."
cd $absoluteSourceFolder
# if ($LASTEXITCODE -ne 0) {
# Write-Host "Error: Failed to change directory to $absoluteSourceFolder"
# exit $LASTEXITCODE
# }
Write-Host "Current directory after change: $(Get-Location)"
# Compress the files and folders using 7-Zip
Write-Host "Compressing files in $sourceFolder to $absoluteDestinationFolder..."
& $sevenZipPath a -tzip "$absoluteDestinationFolder/skeleton.zip" "*" | Out-Null
# Check if the compression was successful using $LASTEXITCODE
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Compression failed with exit code $LASTEXITCODE"
exit $LASTEXITCODE
}
# Restore the original working directory
Set-Location -Path $currentDir
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to restore directory to $currentDir"
exit $LASTEXITCODE
}
Write-Host "Compression completed successfully. Zip file created at: $absoluteDestinationFolder"
# Create a copy of the zip file in the src/skeleton.xlsm/XMLoutput folder at the /src level
$copySource = "src/skeleton.xlsm/XMLoutput/skeleton.zip"
$renameDestination = "./skeleton.xlsm"
# Delete the destination file if it exists
if (Test-Path $renameDestination) {
Write-Host "Deleting existing file: $renameDestination"
Remove-Item -Path $renameDestination -Force
}
# Copy and rename the file in one step
Write-Host "Copying and renaming $copySource to $renameDestination..."
Copy-Item -Path $copySource -Destination $renameDestination -Force
# Verify if the file exists after the copy
if (-not (Test-Path $renameDestination)) {
Write-Host "Error: File not found after copy: $renameDestination"
exit 1
}
Write-Host "File successfully copied and renamed to: $renameDestination"