My folder C:\Downloads\Files\ has 3 .zip files.
test1.zip
test2.zip
test3.zip
I need to copy these files into same location and append another extension .dat to the file.
The total files in my folder C:\Downloads\Files\ should have 6 files now (3 .zip and 3.dat)
test1.zip
test2.zip
test3.zip
test1.zip.dat
test2.zip.dat
test3.zip.dat
Can anyone help me get this done?
Use the Get-ChildItem cmdlet to retrieve all zip files and copy them using the Copy-Item cmdlet:
Get-ChildItem -Path 'C:\Downloads\Files\' -Filter '*.zip' |
Copy-Item -Destination { "$($_.Name).dat" }
The following copies only files from within the $path directory with extension .dat.
Get-ChildItem -Path $path -File | ForEach-Object {Copy-Item -Path $_.FullName -Destination "$($_.FullName).dat"}
Related
When trying to unzip files traditionally in Windows, it unzips to the name of the zip file as a folder with the files embedded into the folder itself. I am unable to recreate this using the Extract-Archive Cmdlet. Here is the code section for this that I have:
Get-ChildItem 'path of zip' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
While running this, the unzip process works fine. I am just wondering if there is a way to keep the traditional unzip file/folder structure when using the Expand-Archive Cmdlet
Thank you
You can set the destination folder in parameter DestinationPath. If the folder with the name of the zipfile does not yet exist, it will be created:
$sourcePath = 'path of zip file(s)'
$destination = 'path to extract'
Get-ChildItem -Path $sourcePath -Filter '*.zip' -File | ForEach-Object {
$target = Join-Path -Path $destination -ChildPath $_.BaseName
$_ | Expand-Archive -DestinationPath $target -Force
}
The folder named "z:\original" has hundreds of sub-folders containing multiple copies of the same .jpg files. I wanted to copy all .jpg files into a folder named "z:\dump" WITHOUT the folder structure and hopefully overwrite most of the copies. I used
copy-Item -path "Z:\original" -filter "*.jpg" -Destination "Z:\dump" -recurse -verbose
but this recreated the structure with the .jpg files. How can I dump all files into a single folder, using PowerShell?
Use combination of dir/Get-ChildItem and Copy-Item with pipeline.
$_.FullName - Full path to jpg file
$_.Name is file name only
Get-ChildItem Z:\original\*.jpg -Recurse | %{Copy-Item $_.FullName "Z:\dump\$($_.Name)"}
You might need to add -Force to overwrite same files
This is what I am trying to do with powershell for zipping files.
Sort out files older than xxx days
Add those files in a zip file.
Delete the source files.
I have Powershell_Community_Extensions installed so I use Write-zip to do the job.
$Source = "\\network\share"
$files = Get-ChildItem -Path $Source | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-62)}
$files | Write-Zip -OutputPath $Source\Archive.zip -EntryPathRoot $Source -Append -Quiet
Remove-Item $files -Force
Issues:
I have to use -EntryPathRoot with Write-zip, otherwise it wont pick up network share
Remove-item also wont pickup files from network share, it says "Remove-Item : Cannot find path 'C:\Windows\system32\feb03.txt' because it does not exist.", why it deleting file from C:\Windows\system32\ instead of \\network\share
Write-zip -append did add files into the zip file, but it did not just add files in the root of that zip file, it actually created entire folder structure in the root of the zip and added newer filtered files in the end of that folder structure. I just want to add the newer filtered files into root of that zip file.
Any idea please?
Utilizing the v5 *Archive cmdlets:
$Source = '\\network\share'
$Files = Get-ChildItem -Path $Source |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-62) }
Compress-Archive -Path $Files.FullName -DestinationPath $Source\Archive.zip -CompressionLevel Optimal -Update
$Files | Remove-Item -Force
I am trying to copy a whole bunch of files using Powershell, from one directory to another on my computer.
I used Get-ChildItem C:\Users\Tom\Google Drive\My Files\*\Assessment 1\* to identify that this was the path that I wanted to copy too, and I know about Copy-Item, but I want to maintain parts of the path name when copied.
Example:
If I copy from C:\Users\Tom\Google Drive\My Files\Cool Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\Cool Stuff\Assessment 1
Whereas if I copy from C:\Users\Tom\Google Drive\My Files\New Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\New Stuff\Assessment 1
You could use the Get-ChildItem cmdlet to recursively find all Assessment 1 folders within your base directory and then remove the base path using -replace to finally copy the items using the Copy-Item cmdlet:
$baseDir = 'C:\Users\Tom\Google Drive\My Files\'
$destination = 'C:\Users\Tom\Archive\'
Get-ChildItem $baseDir -directory -Filter 'Assessment 1' -Recurse | ForEach-Object {
$newPath = Join-Path $destination ($_.FullName -replace [regex]::Escape($baseDir))
Copy-Item $_.FullName $newPath -Force -Recurse
}
I want to copy file
base.txt
To Other Folders And I want his name change To The Folder Name.
cd c:\Program\Levels
copy-item *.txt c:\Books -force -recurse
Get-ChildItem C:\Books -Filter *.txt -Recurse | Rename-Item -NewName { $_.Directory.Name}
So he's copying *.txt for the folder 'Books' and renaming the file to 'books.txt' but, he did not copy the file to subfolders within 'Books'
I want it to copy the .txt for subfolders in 'Books' and rename the .txt to the folder name.
Obs: the .txt file is unique
So the file base.txt which is presumably some sort of template will be copied to each subfolder in C:\Books? I think I can understand your logic approach but I offer this as a solution instead.
$templateFile = "C:\temp\base.txt"
Get-ChildItem "C:\Books" | Where-Object {$_.PSIsContainer} |ForEach-Object{
$newName = "{0}\{1}.txt" -f $_.FullName, $_.Name
Copy-Item -Path $templateFile -Destination $newName
}
Gather each subdirectory of "C:\Books" and in each of those directories copy the file base.txt to its new location where its name is the name of the parent folder.