Hi everyone!
Currently, I am trying to create a script in Powershell that will replace/delete files in a *.zip file without extracting it.
I know it is possible to do manually by using applications like 7Zip and WinRAR or by simply opening a zip archive in Folder Explorer, but I am looking for a way to automate this action.
I am new to powershell and to programming in general, so I cannot come up with any preliminary code. I tried to search for answers on the net, but failed to find anything that would work for me.
So the question is:
Lets say we have a zip archive called Photos (Photos.zip). The archive contains 5 images. How do I replace/delete images in Photos.zip without extracting it in Powershell?
Any code/ideas/links to helpful resources would be highly appreciated.
I used the following solution.
First I moved old files from the archive to a temporary folder by using this code:
#Path to the temporary folder
$pathToTemporaryFolder = "C:\...\Temporary"
#Path to a file that will be moved from the archive to the temporary folder
$pathToFileToBeMoved = "C:\...\Photos.zip\My Photos\My Image.png"
(New-Object -COM Shell.Application).NameSpace("$pathToTemporaryFolder").MoveHere("$pathToFileToBeMoved")
Then, I copied some new files to the archive:
#Path to the folder with a new file
$pathToFolderWithNewFile = "C:\...\New Images\My New Image.jpeg"
#Path to a folder in the archive where the new file will be copied to
$pathToFolderInArchive = "C:\...\Photos.zip\My Photos"
(New-Object -COM Shell.Application).NameSpace("$pathToFolderInArchive").CopyHere("$pathToFolderWithNewFile")
Finally I deleted the temporary folder
Remove-Item -Path "C:\...\Temporary" -Recurse
This is how I deleted files from the archive and copied new files to it without unzipping/extracting.
Related
I have a folder with hundreds of files.
I need to rename the files in this folder one by one; but this takes a lot of time.
I did some research and found that it can export names from a text file to files in any folder I want.
But the videos contained insufficient information for me to implement it.
What I've been able to do so far:
I was able to transfer file names to TXT file with CMD command.
Note: After exporting the filenames I found some errors in the sorting.
You can see the related error in the "Example Files II" section.
Example files I:
Files and their real names.
Example files II:
I exported the file names to txt file with CMD; but the order of the fil names is not the same as in the TXT file with slight differences.
Note: This is not a big problem for now; but I would like to know if there is a way to do the correct sorting.
Export File Names
Example files III:
I batch corrected the filenames with EmEditor.
Now I need to automatically replace these names with the files in the folder.
Here I don't know how to do this.
If anyone can provide practical information on this subject, I would be glad.
Corredted File Names
You can use Powershell and a regular expression to rename files. Go to the folder in Explorer, select Open Windows Powershell on the File menu, and enter the following:
Get-ChildItem *.zip | Rename-Item -NewName { $_.Name -replace '-[0-9]{4}-[0-9]{2}-[0-9]{2}(-[0-9]{2}-[0-9]{2}-[0-9]{2}\-utc)?\.zip$','.zip' }
References:
Use Regex / Powershell to rename files
Replacement operator
I want to back up important files to my USB drive every day. The USB drive is always plugged into the computer, so I don't need to worry about drive letters. I know how to create a batch file to simply copy and paste them into the drive, but I was wondering if there is a way to create a batch file that makes a zip file of all the folders I want (using winzip or winrar), and then has them sent to the drive. That way I can archive them instead of just copying and replacing them.
Thank you.
See:How can you zip or unzip from the command prompt using ONLY Windows' built-in capabilities?
Powershell can do this:
Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip')
you can incorporate it into a batch file by calling powershell.exe like so:
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip'); }"
I would recommend running it from a scheduled task so you don't have to start it manually or worry about it running all the time, and plan on disconnecting/replacing/archiving the drive itself occasionally - locally attached storage is not a backup
Start WinRAR, click in menu Help on first menu item Help topics, expand on help tab Contents the list item Command line mode and
read the help page Command line syntax,
expand the list item Commands and read the help page Alphabetic commands list and
expand the list item Switches and read the help page Alphabetic switches list.
Then you know how I created this single command line below and what all those switches mean.
"%ProgramFiles%\WinRAR\WinRAR.exe" a -ac -ao -afzip -agYYYY-MM-DD_NN -cfg- -ed -ep1 -ibck -inul -m5 -r -y -- "D:\Backup Folder\DataBackup_.zip" "C:\Path To Folder With Files To Backup\"
See also answer on Simply compress 1 folder in batch with WinRAR command line?
WinRAR adds to ZIP archive only files with archive attribute set because of switch -ao and clears the archive attribute after compressing the file into the archive because of -ac. The archive attribute is automatically set by Windows when a file is modified, renamed or moved. So this command line avoids compressing the same unmodified files again and again into ZIP archives.
The ZIP file is named automatically with current date in name and an automatic incremented number in case of this command line is executed multiple times on one day.
I have files at location D:\data\Generate which needs to be Zipped and copied to
D:\data\Upload directory
I used command
set generate=D:\Data\Generate
set upload=D:\Data\Upload
cd %generate%
zip - * >> %upload%\%%i.zip
If I run this command from cmd it works fine but while running it from a
scheduler (ex: Control-M) it actually copies all the files from Control-m config directory into the zip folder.
If I explicitly mention the directory under whose the files needs to be zipped
zip - %generate%*.* >> %upload%\%%i.zip
the final zip folder actually contains the whole directory structure too instead of just the files.
ex: Zip file contains Data folder, Generate folder and the files under Generate folder
Can someone please help with this?
ok I got some clue with this.
This is a problem with Windows itself, for ex:
You open CMD
You are currently in directory C
then you run a command cd D:\data
even after this when you do dir, it will list out all the jobs in C directory only.
if you run D: after the above CD it will actually go into D:\data directory and you can work on that directory
I am sure you are past this since two (2) years ago. Here is a way to do it. I did not understand what you wanted the .zip archive filename to be. There is nothing in the source code presented that would produce %%i.
powershell -NoProfile -Command "Get-ChildItem -File -Path 'D:\Data\Generate' | Compress-Archive -DestinationPath 'D:\data\Upload\i.zip'"
i'm a near complete beginner to batch scripting.
I'm currently learning how to create batch files. My goal is to compress a folder using exclusively InfoZip, add the date to the file name, and have that file copied to an USB memory stick plugged on H:\
The reason why i need to use InfoZip, even though it is a very old program, is because i need somthing that works even on Win95.
InfoZip is not installed, it is just unpacked in folder and ready to use.
It is possible to download InfoZip 3.0 from here:
https://sourceforge.net/projects/infozip/
Anyway, so far, the only thing i could come up with is this...
--------------------------------------
Title : Your folder will be zipped into an archive that will be copied on the USB memory stick plugged on your computer. Please DO NOT remove the memory stick during the operation.
#ECHO OFF
call d:\infozip\wiz.exe
pause
--------------------------------------
It just brings up the InfoZip window on the screen, but then i have absolutely no idea about how to make it zip a folder, add the date, and copy that zipped file to the USB.
All the regular commands meant for 7-zip or Winzip don't seem to work with InfoZip.
I could really use some help, please :)
Thanks!
Using the waybackmachine I was able to get the documentation for info-zip:
https://web.archive.org/web/20170829173722/http://www.info-zip.org/mans/zip.html#EXAMPLES
In contrary to what zip.exe shows, the syntax for zipping files is this:
zip -r zipfilename zipfilecontents
Example:
zip -r myzip.zip c:\myfolder\*.*
The -r parameter includes subfolders as well.
Problem is that the complete folder structure is included in the zip. I have not found a solution for this yet.
To solve the structure folder problem, add a cd command that will target the folder container which has inside your file or files. This before running the code proposed by Martien de Jong upside.
for example:
The path of my file is: cd C:\aa\B\file.txt
So the path you will put in the cd to target the folder container is: cd C:\aa\B
cd C:\aa\B
zip -r myzip.zip B\*.*
*Remember that this code will zip all the files included in B folder.
So I've noticed that Windows creates hidden folders called "blabla.jpg.files" if you have enabled the thumbnail view in your picture folder. I sync my picture folder to my phone and NAS and would like to remove those hidden folders from there with powershell.
Since my picture folder also has subfolders which also have subfolders and so on, I would like to create a little loop.
The script should just scan in all subfolders of one folder for hidden folders with the name "*.files" and remove them.
Can somebody help me with this one.
You don't need to create loop as Get-ChildItem can search recursively for the specified item e.g.:
Get-ChildItem <path> -r -attributes h+d *.files | Remove-Item -r -whatif
The above command requires features that are new in PowerShell v3. If the list of folders to be deleted looks correct, remove the -whatif to actually delete them.
BTW I don't see that Windows creates these folders on my system. It does create a hidden file called Thumbs.db. Perhaps it is some other program that is creating these hidden folders?