Command Line code without any addon to unzip a .zip file - command-line

I have no idea about command line scripts.
I checked the few already existing threads on the similar topic however I could not find a thread which mentions the exact code to unzip the file.
I have found the a code where Fk8pwu47g.zip is my file which I need to unzip
for /R "C:\Users\sneshah\Desktop\2. ISM" %I in ("Fk8pwu47g.zip") do ("%ProgramFiles(x86)%\WinZip\winzip32.exe" Fk8pwu47g -y -o"%%~dpnI" "%%~fI")
but it throws errors as below:
Action: Add (and replace) files Include subfolders: no Save full
path: no no files were found for -y that match your selection
criteria. no files were found for -o"%C:\Users\sneshah\Desktop\2. that
match your selection criteria. no files were found for Fk8pwu47g""
that match your selection criteria. Irrecoverable Error: Bad or
incomplete option for the operation.

We achieved the requirement using a powershell script to unzip file.
Here is the code
$src = "C:\Desktop\InputFile\"
$dest = "C:\Desktop\OutputFile\"
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zps = Get-ChildItem $src -Filter *.zip
foreach ($zp IN $zps)
{
$foldernames = Get-ChildItem $dest
$newfolders = $dest + $zp
$all = $src + $zp
[System.IO.Compression.ZipFile]::ExtractToDirectory($all, $newfolders)
}

Related

PowerShell Move Files From One Server to Another

I know this has been asked a million times, but I can't seem to find anything that works for me. I don't know if there is a permissions issue or what, but I am trying to move files from one server to another using a PowerShell script in the task scheduler and it worked for about a week before it stopped working. There are no errors in the task scheduler, and I'm not well versed in PowerShell at all, I'm just trying to get something quick and simple for our CMS manager to move her files from the website to a folder on another server.
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST= "\\server-folder-structure\uploads\" ## enter your destination folder
foreach ($ORG in gci $DEST -include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -recurse)
{
Move-Item -path $ORG -destination $DEST ## Move the files to the destination folder
}
I tried this too, in hopes it would work, but still no files are being moved.
Get-ChildItem E:\folder-structure\uploads\* -Include *.doc,*.docx,*.pdf,*.png,*.gif,*.jpg,*.jpeg,*.html,*.htm -Recurse |ForEach-Object { Move-Item $_.FullName \\server-folder-structure\uploads\ }
Am I doing something wrong? Are there permissions to folders that I need to set that I don't know about? Is PowerShell just not the best way to do this? Thanks in advance.
I believe you're making this harder than it should be (with respect to PowerShell being new to you). You don't need a loop on any of your examples if you want to pipe directly to Move-Item:
$ORG = "E:\folders\uploads\" ## enter current source folder
$DEST = "\\server-folder-structure\uploads\" ## enter your destination folder
$filterFor = "*.doc","*.docx","*.pdf","*.png","*.gif","*.jpg","*.jpeg","*.html","*.htm"
Get-ChildItem $ORG -Include $filterFor -File -Recurse |
Move-Item -Destination $DEST -WhatIf
As for what you tried, and as Mathias pointed out, you would be searching your $DEST location in which the files wouldn't exist as they would only be in $ORG; given that that's the actual source folder.
This would also overwrite you $ORG variable with the current item in your iteration in: foreach ($ORG in gci ..){ ... }.
Meaning, your Move-Item would be invalid.

Create / Extract zip file and overwrite existing files/content

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('foo', 'foo.zip')
[IO.Compression.ZipFile]::ExtractToDirectory('foo.zip', 'bar')
I found the code to create and extract .zip files via PowerShell from this answer, but because of my low reputation I cannot ask a question as a comment on that answer.
Creating - How to overwrite an existing .zip file without user interaction?
Extracting - How to overwrite existing files and folders without user interaction? (Preferably like robocopys mir function).
PowerShell has built-in .zip utilities without needing to use .NET class methods in version 5 and above. The Compress-Archive -Path argument also takes a string[] type so you can zip multiple folders/files into the destination zip.
Zipping:
Compress-Archive -Path C:\Foo -DestinationPath C:\Foo.zip -CompressionLevel Optimal -Force
There is also an -Update switch.
Unzipping:
Expand-Archive -Path C:\Foo.zip -DestinationPath C:\Foo -Force
PowerShell versions prior to 5 can execute this script
Thanks to #Ola-M for update.
Thanks to #maximilian-burszley for update.
function Unzip($zipfile, $outdir)
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
$archive = [System.IO.Compression.ZipFile]::OpenRead($zipfile)
try
{
foreach ($entry in $archive.Entries)
{
$entryTargetFilePath = [System.IO.Path]::Combine($outdir, $entry.FullName)
$entryDir = [System.IO.Path]::GetDirectoryName($entryTargetFilePath)
#Ensure the directory of the archive entry exists
if(!(Test-Path $entryDir )){
New-Item -ItemType Directory -Path $entryDir | Out-Null
}
#If the entry is not a directory entry, then extract entry
if(!$entryTargetFilePath.EndsWith("\")){
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entryTargetFilePath, $true);
}
}
}
finally
{
$archive.Dispose()
}
}
Unzip -zipfile "$zip" -outdir "$dir"

Replace all files in Folder with many sub folders with same name files with powershell

My problem is the following:
I repaired a bunch of dwg that were corrupted and i need to replace the corrupted ones with the repaired ones using power shell. They have the same name, the problem is that i acquired the files through the windows search tool, and i have them all in a single folder.
The original corrupted files exist in many sub folders. How do i copy all files from say folder
"d:\repaired" to "d:\original"
replacing each original files with its repaired one?
I tried doing it with xcopy using the /u, but i also need a way to iterate through the sub-folders.
Ok guys, i did it through command line. I found the answer here:
Replacing a file into multiple folders/subdirectories
I simply used something like this:
Replace C:\SomeFile.Txt C:\SomeRootFolder_ContainsMultipleSubFolders /s
EDIT
$files = Get-ChildItem -path C:\files -Filter *.txt -Recurse
foreach
($file in $files)
{copy-item -path *.txt -destination $files -recurse -force}
Will find all files with a certain extension, then replace them with another file from a directory specified in the -path.

How to move a single txt file to the zip in powershell 3

I am trying to copy/move one text file to the zip. I don't want to unzip it, copy the file and zip it back. Is there any way we can directly copy or move text file to the zip in powershell. When i am doing it in powershell, it's doing after that when i try to look inside the zip it's saying invalid path.
Powershell commands:
$A = "20160914.4"
New-Item "C:\test\VersionLabel.txt" -ItemType file
$A | Set-Content "C:\test\VersionLabel.txt"
Copy-Item "C:\test\VersionLabel.txt" "C:\test\Abc.zip" -Force
Error: The compressed folder is invalid
>= PowerShell 5.0
Per #SonnyPuijk's answer above, use Compress-Archive.
clear-host
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
Compress-Archive -Path $fileToZip -Update -DestinationPath $zipFN
< PowerShell 5.0
To add a single file to an existing zip:
clear-host
Add-Type -assembly 'System.IO.Compression'
Add-Type -assembly 'System.IO.Compression.FileSystem'
[string]$zipFN = 'c:\temp\myZipFile.zip'
[string]$fileToZip = 'c:\temp\myTestFile.dat'
[System.IO.Compression.ZipArchive]$ZipFile = [System.IO.Compression.ZipFile]::Open($zipFN, ([System.IO.Compression.ZipArchiveMode]::Update))
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($ZipFile, $fileToZip, (Split-Path $fileToZip -Leaf))
$ZipFile.Dispose()
To create a single file zip file from scratch:
Same as above, only replace: [System.IO.Compression.ZipArchiveMode]::Update
With: [System.IO.Compression.ZipArchiveMode]::Create
Related Documentation:
Compress-Archive: https://technet.microsoft.com/en-us/library/dn841358.aspx
CreateEntryFromFile: https://msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx
You can use Compress-Archive for this. Copy-Item doesn't support zip files.
If you don't have PowerShell v5 you can use either 7Zip command line or .Net

Powershell: processing files in two folders based on what's in them

I have two directories: c:\Rar and c:\unRared
Rar - contains hundreds of RAR'ed files. Only one file inside the RAR. File inside the archive is with *.TRN extension.
UnRared has unarchived files (hundreds of files with *.TRN extension)
I've been trying to create a Powershell script to extract only files that have not been extracted already.
can't you just supply parameters to whatever compression program you have telling it not to overwrite existing files?
Knocked this out on my own...well, not really, with the help of developer
$dir1='C:\temp\aaa'
$dir2='C:\temp\bbb'
$CmdPath = "C:\Program Files (x86)\WinRAR\RAR.exe"
$Files2Extract = get-childitem -path 'C:\temp\aaa' -recurse -name -filter *.rar
#$d2 = get-childitem -path $dir2 -recurse
foreach($file in $Files2Extract){
$justname = $dir2+'\\'+(split-path $file -leaf).split(".")[0]+'.trn'
if(!(Test-Path -path $justname)) {
&$CmdPath e $file $dir2
}
}