Get-ChildItem to Move-Item - path not found - powershell

I try to move my old logfiles to a yyyy\MM\dd folder structure by
Get-ChildItem . -Recurse -Include *.log |
Move-Item -Dest {"D:\Archive\{0:yyyy\\MM\\dd}\{1}" -f $_.LastWriteTime, $_.Name} -Force
but i get a path-not-found error.
update
The source path does not seem to be the problem. It looks like using -Force on Move-Item does not create missing destination directories.
sub question: Could the same be done without Get-ChildItem?

As far as I found the proposed way of moving logs practically interesting, I decided to complete the task:
Get-ChildItem . -Recurse -Include *.log |
Move-Item -Force -Destination {
$dir = "C:\Temp\{0:yyyy\\MM\\dd}" -f $_.LastWriteTime
$null = mkdir $dir -Force
"$dir\$($_.Name)"
}

I guess that for a source file “some.log” the destination is supposed to be something like “D:\Archive\2010\04\23\some.log” and the directory “D:\Archive\2010\04\23” actually does not exist. In this case Move-Item fails. Is this the case?

Related

How do you do a selective copy from a directory tree using PowerShell?

I am new to PowerShell but have experience with Bash scripting and Python, but need to use PowerShell for a specific project. I thought my use-case was pretty easy, but am really having a hard time getting it to work.
I have a directory structure like this:
-- Data
-- ProjectA
-- Exception
- exception1.txt
-- Custom
- file1.txt
- file2.txt
-- ProjectB
-- Exception
- exception2.txt
-- Custom
- file3.txt
- file4.txt
What I am trying to do is copy this structure, but copy the 'Exception' directories and their contents to a different path to the rest of the files. So I want to end up with something like this:
-- NewFolder
-- ProjectA
-- Custom
- file1.txt
- file2.txt
-- ProjectB
-- Custom
- file3.txt
- file4.txt
-- ExceptionFolder
-- Exception
- exception1.txt
- exception2.txt
Notice that the contents of the exception folders are all merged into a single folder.
I have tried with doing a Copy-Item and then --exclude '*Exception*' but that only seems to work if the 'Exception' folder is at the top level. So I then tried with Get-ChildItem and pipe that into a where ($_fullname -notin '*Exception*') and pipe that into further steps to copy at each level, but that doesn't seem to work either. I've tried a few other things as well but none give me the results I'm looking for.
Not sure if it makes a difference, but I'm using PowerShell 7.1.1 on a Mac.
Any guidance on this one would be much appreciated!
Convoluted, yes, works? yes.
$source = '.\Data'
$destination = '.\New Folder'
$exceptionFolder = '.\ExceptionFolder'
$exceptionRegex = 'Exception|foobar'
$directories = Get-ChildItem $source -Recurse -Directory
$redirect = $directories | ? {$_.Name -match $exceptionRegex}
switch -Regex ($directories){
{$_.Name -match $exceptionRegex} {
# Create directory based on the regex match.
$ExceptionDestination = [io.directoryinfo]("{0}\{1}" -f $ExceptionFolder, $_.Name)
if (-not(Test-Path -Path $ExceptionDestination)){
$null = md $ExceptionDestination
}
# Copy top level files from the matched directory name into the $ExceptionDestination directory.
Copy-Item -Path ("{0}\*.*" -f $_.FullName) -Destination $ExceptionDestination -Verbose
}
}
# Skips copying of the contents of directories matching $exceptionRegex.
Copy-Item -Path $source -Destination $destination -Exclude ("*{0}*" -f $redirect).Name -Recurse -Verbose
# Actually remove directories.
Get-ChildItem $destination -Recurse -Directory | ? {$_.Name -match $exceptionRegex} | Remove-Item -Recurse -Force -Verbose
You could try Robocopy ! You have an exclude parameter.
https://pureinfotech.com/exclude-files-folders-robocopy-windows-10/
I may be wrong but, this might be what youre asking for?
#add -Recurse to copy subfolders
Get-ChildItem -Path C:\Path1 -Exclude "Exception" | Copy-Item -Destination C:\Path2
Or,
#add -Recurse to copy subfolders
Copy-Item -Path C:\path1\* -Exclude 'Exception' -Destination C:\path2
First copy all items, excluding files named like "Exception*" (there seems to be no way to exclude based on directory name, apart from a Get-ChildItem pipeline):
Copy-Item 'Data\*' -Destination 'NewFolder' -Exclude 'Exception*' -Recurse
Unfortunately this creates empty "Exception" folders in the destination, so we have to delete them:
Remove-Item 'NewFolder\*' -Include 'Exception' -Recurse
Here is an example for a Get-ChildItem pipeline in case you need a more general solution. You could change the Where-Object condition to filter on full path, e. g. $_.Fullname -notlike '*\Exception\*'.

Powershell Loop through folder move file, create directory if doesn't exist

Good Afternoon,
I apologize if this is a basic question, but I have been struggling with it, also still very new to Powershell.
I have a network mapped folder Z:\Test.
Under Z:\Test is multiple subfolders with the same structure. I need to loop through all of the subfolders and move all PDF files if they exist in a specific location.
Z:\Test\1\Work\PDF\*.PDF - then move
Z:\Test\2\Work\PDF\*.PDF - Move So on and so on.
I have tried the following, but like I said I have been struggling with it. Thanks any help
Get-ChildItem -Path Z:\temp\*\Work -File -Include "*.PDF" -Recurse | Copy-Item -Force -Destination Y:\Temp\*\Work
I would try something like this:
$files = Get-ChildItem -Path Z:\Temp\*\Work -File -Include "*.PDF" -Recurse
foreach ($file in $files) {
Copy-Item -Path $file.FullName -Destination Y:\Temp\*\Work -Force
}

Remove files in folder without removing folders

How to remove all files in folder and subfolders without removing folders itself? For example:
Remove-Item -Path C:\Users\dummyuser\foldertoclean\subfolders -recurse
I was trying with -Include and -Exclude but still havent found solution.
Just use the following easy one-liner:
Remove-Item -Path C:\Users\dummyuser\foldertoclean\* -Recurse
You can try below command
Get-ChildItem -Path C:\Users\dummyuser\foldertoclean -Include *.* -File -Recurse | foreach { $_.Delete()}

powershell copy all folder structure and exclude one or more folders

I'm trying to use PowerShell to copy a folder with sub-folders from our users to a small backup. These folders contain a folder called "windows" I don't want to copy.
I have tried "exclude" but can't seem to get it to work.
This is the script so far:
Copy-Item "E:\Curos folder" -Exclude 'Windows' -Destination "E:\Curos folder backup" -Recurse -Verbose
I have read other posts but don't quiet understand how it works
It's my first time working with PowerShell
You are complete right.
Actually the script it's simpler than the one I have wrote before.
Here we go:
$source = "C:\Users\gaston.gonzalez\Documents\02_Scripts"
$destination = "D:\To Delete"
$exclude = "Windows"
$folders = Get-ChildItem -Path $source | Where {($_.PSIsContainer) -and ($exclude -notcontains $_.Name)}
foreach ($f in $folders){
Write-Host "This folders will be copied: $f"
Copy-Item -Path $source\$f -Destination $destination\$f -Recurse -Force
}
I'd use something like this:
Get-ChildItem $root -Directory -Recurse | % {$_.name -ne 'Windows'} | foreach {Copy-Item "$($_.FullName)" -Destination $dest -Recurse}
I haven't tested it but it's the skeleton of something you should be able to make work, although I don't find the point of using recurse on both, Get-ChildItem and Copy-Item my advice is to use it on Get-ChildItem.

Copy file with relative path

I would like to copy all files of a certain type from a certain sub-directory with their relative path from that sub-directory to another directory with the relative path intact. e.g.:
Source sub-dir:
c:\temp\sourcedirectory
Source files:
c:\temp\sourcedirectory\tonymontana\fileOne.txt
c:\temp\sourcedirectory\poker\fileTwo.txt
Target dir:
c:\temp\targetdirectory
Desired result:
c:\temp\targetdirectory\tonymontana\fileOne.txt
c:\temp\targetdirectory\poker\fileTwo.txt
So far I've come up with:
Set-Location $srcRoot
Get-ChildItem -Path $srcRoot -Filter $filePattern -Recurse |
Resolve-Path -Relative |
Copy-Item -Destination {Join-Path $buildroot $_.FullName}
However, this "everything is an object" à la PowerShell is beating me down (at least that's what I suspect). I.e. the files gets copied, but without their relative path.
Anyone who could enlighten me a bit?
Don't bother with PowerShell cmdlets for this, simply use robocopy:
robocopy C:\temp\sourcedirectory C:\temp\targetdirectory *.txt /s
You can try this:
$srcroot = "c:\temp\sourcedirectory"
$builroot= "c:\temp\targetdirectory"
gci -path $srcroot -filter $filepattern -recurse |
% { Copy-Item $_.FullName -destination ($_.FullName -replace [regex]::escape($srcroot),$builroot) }
Try this:
Copy-item $srcRoot -destination $destination -recurse
To prevent copying the folder itself i.e. creating
c:\temp\targetdirectory\sourcedirectory
Change into the source folder, then use a wildcard instead of the folder as the source:
cd C:\temp\sourcedirectory\
Copy-item * -destination c:\temp\targetdirectory -recurse`