Copy Folder (contents included) and Paste in Archive Folder with Date - windows

I'm new to PowerShell and have a task to copy a folder and its contents to an archive folder. The copied folder must be renamed with the date as well.
I've been working on some test folders but am having trouble figuring out how to copy the folder + its contents, let alone rename the copied folder.
I'm able to move a folder with
Copy-Item -Path C:\Test -Destination C:\Archive
But the contents don't come with. I'm also not sure how to apply to best apply
$Date = (Get-Date -UFormat "%m%d%Y")
(Get-Date -UFormat "%m%d%Y")
In order to rename the copied folder with the date.

You can create the backup folder first with the date in the name, then copy all the contents from the source, recursively.
$source = 'C:\Temp'
$backupDestination = 'C:\Backups'
# Will Create: 2019-02-20T15.43.05
$backupDate = (get-date -Format s) -replace ':', '.'
# sourceName will be set to "Temp"
$sourceName = (Get-Item $source).BaseName
# Combine the destination with the source name and the backup date
# to create C:\Backup\Temp-2019-02-20T15.43.05
$backupDirectory = Join-Path $backupDestination "$sourceName-$backupDate"
# Create the backup destination directory
New-Item -ItemType Directory -Path $backupDirectory
# Copy everything recursively
Copy-Item $source\** $backupDirectory\ -Recurse

Related

i need copy command support

Currently I have a directory tree system like the image:
I would like someone to help me create a small piece of code. I want to search all directories, I will see a folder named "C" as above, copy all the data and rename it to a higher level folder, will copy to another folder with the name E . You can only copy the A1 folder and contain the C folder inside. I'm using windows 10. Thank you very much.
In PowerShell, the Copy-Item cmdlet can rename an item while copying. So you want to:
Use Get-ChildItem to find all subfolders of the source named "C".
Pipe these to Copy-Item
Construct the destination path and name using Join-Path.
$Source = 'C:\Source' ### Parent folder of folders "A", "B", etc.
$Dest = 'D:\Dest' ### Destination path for copied folders
# Destination root must exist:
If (!(Test-Path $Dest)) {mkdir $Dest | out-null}
Get-ChildItem -Path $Source -Filter 'C' -Directory -Recurse |
Copy-Item -Destination {Join-Path $Dest $_.Parent.Name} -Recurse
Which can be shortened using positional parameters and aliases to:
gci $Source 'C' -ad -s |
copy -Dest {Join-Path $Dest $_.Parent.Name} -Recurse

Powershell copying specific files from all subfolders to a single folder

I'm trying to copy all of the cover.jpg files in my music library to one folder. My attempts so far have either landed me with one file in the destination, or every desired file but also in their own folder matching the source (i.e. folders named for each album containing just the cover.jpg file).
Get-ChildItem "C:\Music" -recurse -filter *.jpg | Copy-Item -Destination "C:\Destination"
I realised that the copy-item command was simply overwriting the previous copies thus leaving me with only one file. I then tried going down the renaming route by moving the file then renaming it but of course that failed as the folder I was basing the rename off has now changed. I don't want to change the name of the file before I copy it as other programs still need the cover.jpg to function.
My question is...
Does anybody know how to recursively look through each folder in my music library to find the cover.jpg file, rename it to match the parent folder (or even if possible, grandparent and parent) then copy that file to a new folder making sure to not copy or create any new folders in this destination?
As a bonus, could this check if a file already exists so that if I ran it in the future only new files will be copied?
The file structure for the library is pretty simple. \Music\Artist\Album title\cover.jpg
If you have a music library structure like that, the easiest way would be to use the properties Directory and Parent each FileInfo object returned by Get-ChildItem contains:
$sourcePath = 'C:\Music'
$destination = 'C:\Destination'
# if the destination folder does not already exist, create it
if (!(Test-Path -Path $destination -PathType Container)) {
$null = New-Item -Path $destination -ItemType Directory
}
Get-ChildItem -Path $sourcePath -Filter '*.jpg' -File -Recurse | ForEach-Object {
$newName = '{0}_{1}_{2}' -f $_.Directory.Parent.Name, $_.Directory.Name, $_.Name
$_ | Copy-Item -Destination (Join-Path -Path $destination -ChildPath $newName)
}

How to create destination folder with date appended to it via Powershell?

I have backup files sitting in a directory. The objective I am trying to achieve is that each time I run this piece of Powershell against that directory, I want it to move the files into a folder that gets created and append today's date to it. I have tried this:
Get-ChildItem -Path 'C:\API\APIBackups' | ForEach-Object {
Move-Item -Path $_.FullName -Destination "C:\Users\Admin\Desktop\New folder\$($_.BaseName,(Get-Date).ToString("MMddyyyy"),$_.Extension)"
}
All this does is moves the backup files into "New folder" and append the date to the files themselves. I'm wanting it to create a new folder with today's date appended within "New folder" and have the backup files sitting in there. Any help would be great.
For one the commas do not belong but each section of "code" needs their own subexpression. You are also sticking the date in between the base filename and the file extension, so it looks like you want to insert the date in the file name. Instead, you can adjust it to this.
Get-ChildItem -Path 'C:\API\APIBackups' | ForEach-Object {
Move-Item -Path $_.FullName -Destination "C:\Users\Admin\Desktop\New folder$((Get-Date).ToString("MMddyyyy"))\"
}
Unless you're renaming the file, you don't need to specify it in the path.
Important Note that if that folder does not exist, you will need to create it first. Otherwise you'll end up with an extensionless file with that name instead. You could test for the path first, create if it doesn't exist, then move.
Get-ChildItem -Path 'C:\API\APIBackups' | ForEach-Object {
$newfolder = "C:\Users\Admin\Desktop\New folder$((Get-Date).ToString("MMddyyyy"))\"
if(-not(Test-Path $newfolder)){
$null = New-Item -Path $newfolder -ItemType Directory
}
Move-Item -Path $_.FullName -Destination $newfolder
}
The $null is to hide the output that New-Item creates by default.
A suggestion for improvement would be to use Join-Path for building the new folder path

How to extract multiple zips into one folder without creating separate folders with zip name? Powershell

Here's my script in powershell
$today = (Get-Date).ToString('dd_MM_yyyy')
$LocalPath = "C:\Builds\$today"
New-Item -ItemType directory -Path $LocalPath
$RemotePath = "C:\Builds\zip\$today"
$Max_hours = "-5"
#Max_mins = "-5"
$Curr_date = get-date
#Checking date and then copying file from LocalPath to RemotePath
Foreach($file in (Get-ChildItem $RemotePath))
{
if($file.LastWriteTime -gt ($Curr_date).addhours($Max_hours))
{
Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {& "C:\Program Files\7-Zip\7z.exe" "x" "-aoa" $_.fullname "-oC:\Builds\$today"}
}
ELSE
{"not extracting $file"
}
}
I've got a few *.zip files which I want to extract into a specific folder. The problem is, that 7zip creates subfolders with *.zip name and extract files into this folders.
Eg. I've got a.zip, b.zip and c.zip files and I want them to be extracted exactly in Builds folder. Right now after my command they are extracted to:
Builds/a/(here a.zip files)
Builds/b/(here b.zip files)
Builds/c/(here c.zip files)
I want them all to be in Builds/(here a,b,c files) with full directory paths.
Are there any 7zip options in shell to do that?
'-e' option exctracts all files without folders paths and thats now what I'm looking for.
If you're using PowerShell v5 you can use Expand-Archive instead of 7zip:
Get-ChildItem "C:\Builds\zip\$today\*pc*.*" | % {Expand-Archive $_ -DestinationPath "C:\Builds\$today"}
EDIT:
I do not get folders created when using the command.
Zip file containing a file:
Running Expand-Archive C:\aaaa\nuget350.zip -DestinationPath C:\bbbb
What is extracted:

Powershell Script : Unzip files and execute bat file within the zip files

I need to write a script to unzip zip files into unique folders and execute a bat file within them.
I am able to get through the unzipping process with the below code. I having a problem with executing the bat file. I need to execute the bat file in a way that it is able to perform its operation on the files of the folder it is presently in.
The bat file contain this code
copy *.prn /b \\PC\Printer
My Current Powershell Script
$shell=new-object -com shell.application
$CurrentLocation=get-location
$CurrentPath=$CurrentLocation.path
$Location=$shell.namespace($CurrentPath)
# Find all the Zip files and Count them
$ZipFiles = get-childitem *.zip
$ZipFiles.count | out-default
# Set the Index for unique folders
$Index = 1
# For every zip file in the folder
foreach ($ZipFile in $ZipFiles)
{
# Get the full path to the zip file
$ZipFile.fullname | out-default
# Set the location and create a new folder to unzip the files into - Edit the line below to change the location to save files to
$NewLocation = "E:\BCode\$Index"
$A = "E:\BCode\$Index"
New-Item $NewLocation -type Directory
# Move the zip file to the new folder so that you know which was the original file (can be changed to Copy-Item if needed)
Move-Item $ZipFile.fullname $NewLocation
# List up all of the zip files in the new folder
$NewZipFile = get-childitem $NewLocation *.zip
# Get the COMObjects required for the unzip process
$NewLocation = $shell.namespace($NewLocation)
$ZipFolder = $shell.namespace($NewZipFile.fullname)
# Copy the files to the new Folder
$NewLocation.copyhere($ZipFolder.items())
#NOT WORKING
#ITS NOT SENDING FILES TO THE PRINTER
#PRINTER NAME & PC NAME ARE PREFECT
$PC = "$A\*.prn" + ' /b //Samsung-2012/Zebra'
cmd \c COPY $PC
# Increase the Index to ensure that unique folders are made
$Index = $Index + 1
}
$destination = 'E:\BCode'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
Why not embed the one line from the batch file in the powershell script, then you can use the variables in your powershell script.
Simply put cmd /c before the copy:
cmd /c copy "$NewLocation\*.prn" /b \\PC\Printer

Resources