Can't Move-Item Modified file - powershell

I was trying to follow the solution given by #StephenP in this post:
Renaming and Moving Files Powershell
I am trying to Move a renamed file to the Output Folder, but it didn't move.
What could go wrong?
Here's my code:
$Files = GCI "$ParentFolder" | ?{$_.Extension -Match "png?"}
$Date = Get-Date -Format "yyyymmddhhmmss"
$Dest = ".\Output"
$Files | ForEach-Object {
# Get the File BaseName and Select the Screen Title only
$FileName = $_.BaseName
$NameCount = $FileName.length
$ScreenTitle = $FileName.substring(0,$NameCount -21)
# Set the New File Name as Variable
$NewFileName = "$($Date)_[$($ScreenTitle)]"
# Start Renaming
$GetName = $_.FullName -replace "$FileName","$NewFileName"
Rename-Item $_ $GetName
# Move the renamed file
Move-Item $GetName -Destination $Dest
}
Thank you for helping :)

First of all, you don't need to rename the file first and then move, because you can do this using Move-Item at the same time.
Use -Filter '*.png' instead of a Where-Object afterwards. The Filter is much more efficient.
Your code does not check if the length of the file BaseName is actually more than 21 characters long, so this $FileName.Substring(0, $NameCount -21) can throw exceptions. However, since you didn't provide any filename examples, I left that in.
Try
$Files = Get-ChildItem -Path $ParentFolder -Filter '*.png' -File
$Date = Get-Date -Format "yyyymmddhhmmss"
$Dest = ".\Output"
$Files | ForEach-Object {
# Get the File BaseName and Select the Screen Title only
$FileName = $_.BaseName
$NameCount = $FileName.Length
# very tricky this.. could throw error if $FileName is less than 21 characters..
$ScreenTitle = $FileName.Substring(0, $NameCount -21)
# Set the New File Name as Variable
$NewFileName = '{0}_[{1}]{2}' -f $Date, $ScreenTitle, $_.Extension
# Move the file with a new name to the destination
$_ | Move-Item -Destination (Join-Path -Path $Dest -ChildPath $NewFileName)
}
As aside, using square brackets in filenames could cause you problems and to do more PowerShell on these files, you need to always remember to use -LiteralPath instead of -Path on cmdlets that support it like Get-ChildItem

Related

Powershell copy files based on last modified date & rename them afterwards

i´ve got little bit stucked with my powershell script.
I would like to run through multiple folders, grab files based on their last modified date and copy them to a new location.
There i have to rename them to a specific convention based on it´s original Filename.
What i wrote only runs through the first part and copy files successfully but not rename them afterwards. Of course when i run the script a second time it renames the files...
File convention is:
120_00001_000_002222_202201_20220124_121833_Formular - Copy.pdf
result should be
2222_120_Memory 01-2022_012022.pdf
this is what i got already
$path = "G:\Temp"
$Target = "K:\Local"
$Max_days = "-60" #Max Days past
$Curr_date = Get-Date
$files = get-childitem $Target *.pdf
Get-ChildItem -Path $path -Recurse -Filter 120_*.pdf |
Where-Object {
$_.LastWriteTime `
-gt (Get-Date $Curr_date.AddDays($Max_days)) `
} | ForEach-Object { $_ | Copy-Item -Destination $Target -Force -PassThru }
foreach($pdf in $files)
{
$split = $pdf.name -replace ".pdf" -split "_"
$newname = "$($split[3].TrimStart("0"))_$($split[0])_$("Memory") $($split[4].Substring($split[4].Length - 2, 2))-$($split[5].Substring(0,4))_$($split[4].Substring($split[4].Length - 2, 2))$($split[5].Substring(0,4))$($pdf.Extension)"
write-verbose "Original: $($pdf.name)" -verbose
write-verbose "NewName: $($newname)" -verbose
Rename-Item $pdf.FullName -NewName $newname -verbose
}
Thanks in adavnced
Edited the Question to more precision.
As commented, you could do this in one loop and rename the file while copying.
Try below:
$path = 'G:\Temp'
$Target = 'K:\Local'
$Max_days = -60 # Max Days in the past
$refDate = (Get-Date).AddDays($Max_days).Date # set to midnight
# get the files of interest
Get-ChildItem -Path $path -Recurse -Filter '120_*_*_*_*_*_*_*.pdf' -File |
Where-Object { $_.LastWriteTime -gt $refDate } |
ForEach-Object {
# rename the file to match the new file naming convention
$split = $_.BaseName -split "_"
# just for clarity, using this example:
# '120_00001_000_002222_202201_20220124_121833_Formular - Copy.pdf'
# $split[0] --> 120 used unchanged
# $split[1] --> 00001 unused
# $split[2] --> 000 unused
# $split[3] --> 002222 used without leading zeros
# $split[4] --> 202201 used, only the last two digits (month)
# $split[5] --> 20220124 used, only the first four digits (year)
# $split[6] --> 121833 unused
# $split[7] --> Formular - Copy unused
# these elements are used more than once, so for convenience store in separate variables
$month = $split[4].Substring($split[4].Length - 2, 2)
$year = $split[5].Substring(0,4)
# construct the new file name
$newName = '{0}_{1}_Memory {2}-{3}_{2}{3}{4}' -f $split[3].TrimStart("0"),
$split[0],
$month,
$year,
$_.Extension
# construct the complete target path and filename
$targetFile = Join-Path -Path $Target -ChildPath $newName
# now copy the file with a new name to the target folder
$_ | Copy-Item -Destination $targetFile -Force
}
I've used the -f Format operator to construct the new filename, because I believe this makes the code easier to read.
I did not take into consideration that naming collisions might occur (file with that new name already in the target folder).
If that can happen, you need to tell us what strategy to use.
Perhaps append an index number to the file in brackets like Windows does?

If testfile1.log exists, create testfile2.log, and so on.. - PowerShell

My PowerShell script creates a log file, but when I run the script for the second time, it tells me that the testfile1.log file already exists.
How do I make the script if it finds testfile1.log, it creates testfile2.log, and if this also exists, it creates testfile3.log, and so on..
New-Item -Path $path -Name "testfile1.log" -ItemType "file"
You could do it this way, first get all the files in the desired path and sort them by the ending digits on their name. If no files are found create the testfile1.log, if there were files found, get the last sorted file (the one with the highest ending digit) extract the ending digits and add +1 to the count and use it to create the new file.
$files = Get-ChildItem $path -Filter testfile*.log | Sort-Object {
$_.BaseName -replace '\D' -as [int]
}
if(-not $files)
{
New-Item -Path $path -Name "testfile1.log" -ItemType File
}
else
{
[int]$number = $files[-1].BaseName -replace '\D'
$number++
New-Item -Path $path -Name "testfile$number.log" -ItemType File
}
An alternative method, based on this answer could be
$path = 'D:\Test'
$log = 'testfile'
$index = ((Get-ChildItem -Path $path -Filter "$log*.log" -File |
Where-Object { $_.BaseName -match "$log\d+$" } |
Select-Object #{Name = 'index'; Expression = {[int]($_.BaseName -replace '\D')}}).index |
Measure-Object -Maximum).Maximum + 1
# create the new file
New-Item -Path (Join-Path -Path $path -ChildPath "$log${index}.log") -ItemType File
A concise solution that also builds on this answer (see there for an explanation of the core technique):
$path = '.' # Output dir.
$nameTemplate = 'testfile{0}.log' # {0} is the sequence-number placeholder
New-Item -ItemType File -Path $path -Name (
$nameTemplate -f (1 + (
# Find all existing log files
Get-ChildItem (Join-Path $path $nameTemplate.Replace('{0}', '*')) |
Measure-Object -Maximum {
# Extract the embedded sequence number.
$_.Name -replace [regex]::Escape($nameTemplate).Replace('\{0}', '(\d+)'), '$1'
}
).Maximum)
) -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.
Note:
The above uses a complex -replace operation to reliably extract the sequence number from existing file names; if you know that that only one number is present in each given file name, $_.BaseName -replace '\D' (removing all non-digit characters) will do in the Measure-Object call above.
If you wanted to use zero-padded, fixed-width sequence numbers, you can adjust (all occurrences of) the {0} placeholder accordingly; e.g, to create sequence numbers 01, 02, ... 99, use {0:00} - see the Composite formatting help topic, which describes the string formatting language also used by PowerShell's -foperator

Renaming multiple files with different names

I am a programmer by no means and am brand new to using powershell, but have been tasked with setting up some batch export processes for daily files we FTP. I need to come up with a script that will take changing file names and change them within the same directory to new names;
Example: files will come in as below (YYYYMMDD will be the changing variable)
YYYYMMDD_Share_Support.txt
YYYYMMDD_Person_Support.txt
We need them to be stripped from the above to:
Share.txt
Person.txt
so on and so forth.
I have found ways to make this work, but only on an as needed basis for one file at a time with specific names, not names that will change daily.
So far I am using:
Get-ChildItem -Filter *.txt
Dir | %{Rename-Item $_ -NewName ("NEWFILENAME.txt" -f $nr++)}
You could use the regex -replace operator inside a pipeline-bound scriptblock:
$files = Get-ChildItem -filter *.txt
$files |Rename-Item -NewName { $_.Name -replace '^\d{8}_(.*)_Support\.txt$', '$1.txt' }
As suggested by TheIncorrigible1, if you know the relative position of the word you need, you can also use -split:
$files |Rename-Item -NewName {'{0}.txt' -f ($_.Name -split '_')[-2]} # grab 2nd last word
How about:
dir *.txt |
rename-item -newname { $null,$base,$null = $_.basename -split '_'; "$base.txt" } -whatif
Probably a longer version of the answer. An alternative mentioned by #TheIncorrigible1
$logDir = "D:\Satish\TestFolders"
cd $logDir
$files = ls
foreach ($file in $files){
$fileSplit=($file.ToString()).split("_")
ren $file "$($fileSplit[1]).txt"
}
And for Share.txt to YYYYMMDD_Share_Support.txt
$logDir = "D:\Satish\TestFolders"
cd $logDir
$files = ls
$date = Get-Date -Format "yyyyMMdd"
foreach ($file in $files){
$fileSplit=($file.ToString()).split(".")
ren $file "$($date)_$($fileSplit[0])_Support.txt"
}

Rename After Copy - Powershell

I am trying to use powershell to copy one type of file (.xlsx) from one folder to another.
Once the copy is completed, I would like the extension on the original file to be changed. (.xlsx to .cmp)
I have the copy part down (below) but I am lost when it comes to the rename. Can you guys please help. I am a PS noob! Thank you.
$src = "C:\Users\x\Documents\Test1"
$dst = "C:\Users\x\Documents\Test2"
Get-ChildItem $src -Filter "*.xlsx" | Move-Item -Destination $dst -Force
As far as I know, you'll have to iterate over your files to be able to perform this rename.
# Set-up variables
$sourcePath = "C:\temp"
$sourceExtension = "txt"
$destinationPath = "C:\temp2"
$destinationExtension = "cmp"
# Grab the list of files
$files = Get-ChildItem -Path $sourcePath -Filter "*.$sourceExtension"
# Loop over the files
foreach ($file in $files) {
# Construct the new file name
$newFileName = (Join-Path -Path $destinationPath -ChildPath $file.BaseName) + ".$destinationExtension"
Write-Output "New File Name = $newFileName"
# Move the file to the new destination with its new name!
Move-Item -Path $file.FullName -Destination $newFileName
}
Note: BaseName = filename without extension
This should do it:
$src = "C:\Users\x\Documents\Test1"
$dst = "C:\Users\x\Documents\Test2"
Get-ChildItem $src -Filter "*.xlsx" | ForEach-Object {
Copy-Item $_ -Destination $dst
Rename-Item $_ -NewName ($_.Name -Replace '.xlsx','.cmp')
}
Uses a ForEach-Object loop to go through each item in the $src folder. Then for each item (represented inside the loop as $_) we use Copy-Item to copy it to the destination Then use Rename-Item with a -Replace to change the file extension.

Move Items relative to current location in Powershell

I need to traverse a folder (ParentLogFolder), find logs and move them from the relative folder (SUBLOG) to a subfolder (CompressedLogFolder)
ParentLogfolder
---SubLog
------CompressedLogFolder
---SubLog
------CompressedLogFolder
Folder structure, not limited to 2 SubLogfolders, can be 100+
$Path = "C:\ParentLogFolder"
$Pattern = "*.log"
$date = get-date
$AgeLimit = $date.AddDays(-31)
Get-ChildItem -Filter $Pattern -path $path -recurse | Where-Object
{$_.LastWriteTime -lt $AgeLimit} | Move-Item -Path
I can't figure out how to get the files of the parent folder into a variable and add the subfolder as destination in the Move-Item part.
Anyone out there, who can help answer this?
Don't mind the $Date part of the code, it works, it's just there because it needs to be in the solution.
Instead of onliner, consider emphasis on code clarity. That is, break the process in substeps by divide and conquer approach. It's easier to check variables for incorrect values when the pipeline isn't used too much. Like so,
$allFiles = Get-ChildItem -Filter $Pattern -path $path -recurse
$oldFiles = $allFiles | ? { $_.LastWriteTime -lt $ageLimit }
foreach($file in $oldFiles) {
$archiveFolder = join-path $file.DirectoryName 'someArchiveFolder'
$destination = join-path $archiveFolder $file.Name
move-item -whatif $file.FullName $destination
}
The -whatif switch will print what the move command would do. Remove it to actually move files.