英文:
Powershell: code error for replacing character in string
问题
我非常新于PowerShell,我正在尝试创建一个脚本,该脚本将循环遍历文件夹的所有子文件夹,然后对于每个子文件夹,循环遍历所有文件,将超过2天的文件复制到新位置。以下是我目前的脚本,但我不断收到错误“方法调用失败,因为[System.IO.DirectoryInfo]不包含名为'replace'的方法。有谁知道我做错了什么
$RootFolder = "H:\route\Backup\Full_Backups"
$SubFolders = Get-ChildItem -Path $RootFolder -Directory
Foreach($SubFolder in $SubFolders)
{
$NewPath = $SubFolder.replace('H:\', 'T:\')
$threshold = (Get-Date).AddDays(-2)
foreach ($SubFolder in Get-ChildItem -LiteralPath $path -Directory) {
# 构建子文件夹路径
$subPath = Join-Path $SubFolder.FullName "*\js\*"
# 定位*\js\*中的文件
$files = Get-ChildItem $subPath -Recurse
# 根据创建日期进行筛选
$files = $files | Where-Object { $_.CreationTime -lt $threshold }
# 删除文件
$files | Copy-Item -Destination $NewPath -WhatIf
}
}
英文:
i am VERY new to powershell and i am trying to create a script which will loop through all the subfolders of a folder, then for each sub folder loop through all the files and any which are older than 2 days copy to a new location. Below is the script i have currently but i keep getting an error "Method invocation failed because [System.IO.DirectoryInfo] does not contain a method named 'replace". Does anyone know what have done wrong
$RootFolder = "H:\route\Backup\Full_Backups"
$SubFolders = Get-ChildItem -Path $RootFolder -Directory
Foreach($SubFolder in $SubFolders)
{
$NewPath = $SubFolder.replace('H:\', 'T:\')
$threshold = (Get-Date).AddDays(-2)
foreach ($SubFolder in Get-ChildItem -LiteralPath $path -Directory) {
# Construct sub-folder path
$subPath = Join-Path $SubFolder.FullName "*\js\*"
# Locate the files at *\js\*
$files = Get-ChildItem $subPath -Recurse
# Filter on Creation date
$files = $files |Where-Object { $_.CreationTime -lt $threshold }
# Remove files
$files |Copy-Item -Destination $NewPath -WhatIf
}
}
答案1
得分: 0
很棒,你进入了PowerShell的世界
这是脚本的优化版本。它递归地获取$RootFolder中所有超过2天的文件。
然后将这些文件复制到新目录,替换驱动器号。
$RootFolder = "H:\route\Backup\Full_Backups"
$files = Get-ChildItem -Path $RootFolder -Recurse -File | Where-Object CreationTime -lt $threshold
foreach ($file in $files) {
$file | Copy-Item -Destination $PSItem.FullName.Replace('H:\', 'T:\')
}
有时非常有用的是cmdlet "Get-Member"。每当你有对象在手,你可以使用" | gm",它是Get-Member的别名。这会显示对象类型以及对象具有的所有属性和方法。
你最初的问题是,你尝试对对象数组进行字符串替换。通过在$SubFolders.FullName上进行替换,你对对象的字符串属性进行了替换。
希望对你有帮助!
玩得开心,享受PowerShell吧!
英文:
Awesome, that you are stepping into the world of PowerShell
Here is an optimized version of the script. It gets all files recursively from the $RootFolder that are older than 2 days.
Then those files are copied to the new directory by replacing the drive letter.
$RootFolder = "H:\route\Backup\Full_Backups"
$files = Get-ChildItem -Path $RootFolder -Recurse -File | Where-Object CreationTime -lt $threshold
foreach ($file in $files) {
$file | Copy-Item -Destination $PSItem.FullName.Replace('H:\', 'T:\')
}
What can be of great help sometimes is the cmdlet "Get-Member". Whenever you have objects at hand, you can always do "| gm", which is the alias for Get-Member. This will show you the object type and all properties and methods an object has.
Your initial problem was, that you tried to do a string replace on an array of objects. By doing the replace on $SubFolders.FullName, you do the replace on the string property of the objects.
Hope that helps!
Have fun with PowerShell!
答案2
得分: 0
Here is the translated code:
Condensed Version:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
(Get-ChildItem $RootFolder -Recurse -File) |
Where-Object { ((Get-Date) - ($_.LastWriteTime)).TotalDays -gt 2 } |
ForEach-Object {
Write-Host "Copying $($_.FullName) to $($_.FullName.Replace('H:\', 'T:\'))"
Copy-Item -Path $_.FullName -Destination $_.FullName.Replace('H:\', 'T:\')
}
Not Condensed:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
$files = (Get-ChildItem $RootFolder -Recurse -File)
foreach ($f in $files) {
if (((Get-Date) - ($f.LastWriteTime)).TotalDays -gt 2) {
$oldpath = $f.FullName
$newPath = $f.FullName.Replace('H:\', 'T:\')
Write-Host $f.FullName
Write-Host $newPath
Copy-Item -Path $oldpath -Destination $newPath
}
}
Please note that I've corrected the date comparison and the use of single quotes for string literals in the code.
英文:
Per our discussion. This is what should get you to where you need to be.
Condensed Version:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
(Get-ChildItem $RootFolder -Recurse -File)
| Where { ((get-date) - ($_.LastWriteTime)) -gt (new-timespan -days 2 -hours 0 -minutes 0) }
| ForEach-Object -Begin {} -Process { Write-Host "Copying $($_.FullName) to $($_.FullName.Replace("H:\", "T:\"))";
Copy-Item -Path ($_.FullName) -Destination ($_.FullName.Replace("H:\", "T:\")) }
Not Condensed:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
$files = (Get-ChildItem $RootFolder -Recurse -File)
foreach ($f in $files){
if (((get-date) - ($f.LastWriteTime)) -gt (new-timespan -days 2 -hours 0 -minutes 0))
{
$oldpath = $f.FullName
$newPath = ($f.FullName.Replace("H:\", "T:\"))
Write-Host ($f.FullName)
Write-Host $newPath
Copy-Item -Path $oldpath -Destination $newPath
} else {
}
}
答案3
得分: 0
感谢大家的帮助。我已成功修复如下代码:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
$subDir = (Get-ChildItem $RootFolder)
foreach($sub in $subDir) {
$newpath = ($sub.FullName.replace('H:\','T:\'))
$Source = $sub.FullName
CD $Source
Get-ChildItem ".\" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-2))} | Where-Object { $_.Name} | Move-Item -Force -Destination "$newpath\"
}
英文:
Thank you for everyone's help. I have managed to fix it with the below:
$RootFolder = "H:\SQLServer\Backup\Full_Backups"
$subDir = (Get-ChildItem $RootFolder)
foreach($sub in $subDir) {
$newpath = ($sub.FullName.replace('H:\','T:\'))
$Source = $sub.FullName
CD $Source
Get-ChildItem ".\" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-2))} | Where-Object { $_.Name} | Move-Item -Force -Destination "$newpath\"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论