i am trying to move or copy a .xml file into a zip folder.
Move-Item-Path "C:\Users\1469\Desktop\folder1\archive.xml" -Destination "C:\Users\1469\Desktop\*.zip" -Force
But this code is not working. please help me!
You have a typo in that line (Move-Item-Path should be Move-Item -Path). You can't use wildcards in a destination path, though, and you also can't use Move-Item to move files into zip files. Doing so would simply overwrite the zip file with the XML file.
You need the Shell.Application COM object for adding files to a zip file:
$zip = 'C:\Users\1469\Desktop\your.zip'
$xml = 'C:\Users\1469\Desktop\folder1\archive.xml'
(New-Object -COM Shell.Application).NameSpace($zip).CopyHere($xml)
Note that the zip file must already exist. It won't be created automatically.
Related
I have a source folder with 10-80 files jpg images
I need to copy them into a destination folder with subfolders and replace each existing files with its version from the source folder BUT only when such file is already present there
If there is not such files there do not place a copy
Can I do that with batch or shell? And what is better and faster?
PowerShell can make quick work of this. Assuming the photos have the same name in the source and destination, this should work.
# Get the source photos
$SourcePhotos = Get-ChildItem -Path '.\Source'
# Loop through each of our source photos
foreach ($SourcePhoto in $SourcePhotos) {
# Recuresively search the destination for the photo from the source
$DestinationPhoto = Get-ChildItem -Path '.\Destination' -Filter $SourcePhoto.Name -Recurse
# If we find the photo in the destination...
if ($DestinationPhoto) {
# Copy from source to destination forcefully to overwrite the one at the destination
Copy-Item $SourcePhoto.FullName $DestinationPhoto.FullName -Force
}
}
I have the below 5 folders on the desktop.
Each of these folders has a sub-folder word and the word sub-folder contains file document.xml. For example, This is the path of the file
(C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document\Australian Citizenship (Transitionals and Consequentials) Act 2007\word\document.xml).
I would like to move the document.xml out of the sub-folder word to the parent folder "Australian Citizenship (Transitionals and Consequentials) Act 2007" and rename it as "Australian Citizenship (Transitionals and Consequentials) Act 2007".
I want to do the same process for all the document.xml files in the rest of the four folders.
Please advise me if there is a way to do this with power shell or batch script.
Thanks,
Venkat
I believe this will do your job. Give it a try.
$Root = "C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document"
$Folders = Get-ChildItem -Path $Root
Foreach($Fld in $Folders)
{
If(Test-Path "$($Fld.FullName)\word\document.xml")
{
# Move the file document.xml and rename it
Move-Item -Path "$($Fld.FullName)\word\document.xml" -Destination "$($Fld.FullName)\$($Fld.Name).xml"
#Deletes Word folder
Remove-Item "$($Fld.FullName)\word"
}
}
I'm trying to feed the results of a Get-ChildItem call through io.compression.zipfile to create a zip file of "E:\Applications_Server_Test", excluding two folders "BACKUP" and "BACKUP2".
However, Powershell seems to be interpreting this as "$items = a string of directories and file names" instead of a recursive collection of directories and files I want to zip. I can find tutorials on using Get-ChildItem to exclude directories and I can find tutorials on how to zip a full directory or zip multiple directories but I can't find anything on zipping directories with exclusions. Can somebody tell me where I'm going wrong?
$source = "E:\Applications_Server_Test"
$destination = "E:\AST_Dump.zip"
$items = Get-ChildItem $source -Recurse | ?{ $_.fullname -notmatch "\\backup\\?" }
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($items, $destination)
Thanks!
At the moment you are trying to archive the object $items, not the folder. Unfortunately this is not gonna work.
Try to move the backup folders at the same drive (this will just change records at the drive and not move any data), then to archive the whole "E:\Applications_Server_Test" folder and to move back the backup folders.
Other option is to use ZipArchive.CreateEntry method and to add file by file in to the archive. But this is not so elegant ;)
I'm looking to move and rename a .csv file from one folder to another using PowerShell on a daily basis. The .csv file will have a different file name each day but will always be similar to 'Course Completion_123456_1.csv'
I'm new to Powershell and can work out how to move and rename a file with a known name using the following code;
Move-Item c:\folder1\Course Completion_123456_1.csv c:\folder2\CourseCompletion.csv -force
It's the .csv file with the unknown file name that I can't figure out and I'm hoping someone can help.
Assuming that you have only one .csv file in your source folder.
Try this:
$csv = Get-Item "path to the source csv file\*.csv"
$file = $csv.FullName
Move-Item $file "path to the destination folder\newfilename.csv" -Force
how to add many folders from differents places to one file zip in PowerShell script using [io.compression.zipfile]
One way to do this is to use the Extensions
https://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions(v=vs.110).aspx
A simple example for adding a single file would be as follows
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
$NewFilePath = "c:\pat\to\file.txt"
$EntryName = "file.txt"
$zip = [io.compression.zipfile]::Open("test.zip",[io.compression.ziparchivemode]::Create)
[io.compression.ZipFileExtensions]::CreateEntryFromFile($zip, $NewFilePath, $EntryName)
$zip.Dispose()
You can just iterate over your list of files adding them to your new zip file.
Note that you can reference the type without the full path if you wish.