交换两个文件的名称
Switching the names of two files
我正在编写一个脚本来更改计算机的登录背景。我已经得到它来做我需要的一切,但我正在努力使脚本更有效率,因为就目前而言,在选择一个新脚本后,我创建的一个名为 OrderNames
的函数被调用以将所有内容重命名为随机,然后将它们重命名为 background1、2、3,依此类推。这是我正在使用的片段:
Function OrderNames #Renames everything to a format better than random numbers
{
$FileNames = GCI $BGPath -exclude $BGOld
$FileNames | ForEach-Object -process { Rename-Item $_ -NewName "$Get-Random).jpg" }
$OrderNames = GCI $BGPath -exclude $BGOld
$OrderNames | ForEach-Object -begin { $count = 1 } -process
{ Rename-Item $_ -NewName "background$count.jpg"; $count++ }
}
$Path = "C:\Windows\System32\oobe\info\backgrounds"
$BGOld = GCI $BGPath "backgrounddefault.jpg" #Store current background name
$BGFiles = GCI $BGPath -exclude $BGOld #Get all other images
$BGNew = $BGFiles[(get-random -max ($BGFiles.count)] #Select new image
Rename-Item $BGOld.FullName "$(Get-Random)-$($_.Name).jpg"
Rename-Item $BGNew.FullName "backgrounddefault.jpg"
OrderNames
这很好用,但我希望能够简单地切换 $BGOld
和 $BGNew
的名称。回到大学时使用 C,我可以创建一个临时变量,将 BGNew 存储到其中,使 BGNew 等于 BGOld,然后使 BGOld 等于临时变量。但是当我创建一个值为 BGOld 的临时变量时,它不起作用。事实上,变量似乎并没有随着重命名函数而改变,并且将一个设置为等于另一个导致
Cannot rename because item at does not exist.
很好,所以我尝试使用 Select basename
将文件名设置为一个变量,但我收到关于
的错误
cannot index into an object of type system.io.fileinfo.
另外,我试过 $BGOld.FullName = $BGNew.FullName
,试过 Rename-Item
和其他几个我现在记不起来了。
我试图复制项目名称,但这也没有用。我希望这不是我忽略的简单事情。
TL;DR
是否可以将文件名复制到临时变量中,这样当我重命名文件时,我可以将临时变量的名称复制回 "old" 以避免重命名所有内容?或者更好的是,我可以只切换文件名吗?
是的,您可以在 PowerShell 中执行类似的操作。 "algorithm" 与你描述的 C:
基本相同
- 将旧的重命名为临时名称
- 将新名称重命名为旧名称
- 将旧名称重命名为新名称
所以我们需要跟踪的唯一信息是旧名称和新名称 + 临时文件名。
# Grab the current background and pick the new one
$BGPath = "C:\Windows\System32\oobe\info\backgrounds"
$CurrentBG = Get-Item (Join-Path $BGPath -ChildPath 'backgrounddefault.jpg')
$NewBG = Get-ChildItem $BGPath -Filter *.jpg -Exclude $CurrentBG.Name |Get-Random
# Store the current name of the new background in a variable
$NewBGName = $NewBG.Name
# Now comes the swap operation
# 1. Rename old file to something completely random, but keep a reference to it with -PassThru
$OldBG = $CurrentBG |Rename-Item -NewName $([System.IO.Path]::GetRandomFileName()) -PassThru
# 2. Rename new file to proper name
$NewBG |Rename-Item -NewName 'backgrounddefault.jpg'
# 3. And finally rename the old background back to the name previously used by the new background
$OldBG |Rename-Item -NewName $NewBGName
看看吧。使用变量引用文件,然后交换变量。
${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt}
我正在编写一个脚本来更改计算机的登录背景。我已经得到它来做我需要的一切,但我正在努力使脚本更有效率,因为就目前而言,在选择一个新脚本后,我创建的一个名为 OrderNames
的函数被调用以将所有内容重命名为随机,然后将它们重命名为 background1、2、3,依此类推。这是我正在使用的片段:
Function OrderNames #Renames everything to a format better than random numbers
{
$FileNames = GCI $BGPath -exclude $BGOld
$FileNames | ForEach-Object -process { Rename-Item $_ -NewName "$Get-Random).jpg" }
$OrderNames = GCI $BGPath -exclude $BGOld
$OrderNames | ForEach-Object -begin { $count = 1 } -process
{ Rename-Item $_ -NewName "background$count.jpg"; $count++ }
}
$Path = "C:\Windows\System32\oobe\info\backgrounds"
$BGOld = GCI $BGPath "backgrounddefault.jpg" #Store current background name
$BGFiles = GCI $BGPath -exclude $BGOld #Get all other images
$BGNew = $BGFiles[(get-random -max ($BGFiles.count)] #Select new image
Rename-Item $BGOld.FullName "$(Get-Random)-$($_.Name).jpg"
Rename-Item $BGNew.FullName "backgrounddefault.jpg"
OrderNames
这很好用,但我希望能够简单地切换 $BGOld
和 $BGNew
的名称。回到大学时使用 C,我可以创建一个临时变量,将 BGNew 存储到其中,使 BGNew 等于 BGOld,然后使 BGOld 等于临时变量。但是当我创建一个值为 BGOld 的临时变量时,它不起作用。事实上,变量似乎并没有随着重命名函数而改变,并且将一个设置为等于另一个导致
Cannot rename because item at does not exist.
很好,所以我尝试使用 Select basename
将文件名设置为一个变量,但我收到关于
cannot index into an object of type system.io.fileinfo.
另外,我试过 $BGOld.FullName = $BGNew.FullName
,试过 Rename-Item
和其他几个我现在记不起来了。
我试图复制项目名称,但这也没有用。我希望这不是我忽略的简单事情。
TL;DR
是否可以将文件名复制到临时变量中,这样当我重命名文件时,我可以将临时变量的名称复制回 "old" 以避免重命名所有内容?或者更好的是,我可以只切换文件名吗?
是的,您可以在 PowerShell 中执行类似的操作。 "algorithm" 与你描述的 C:
基本相同- 将旧的重命名为临时名称
- 将新名称重命名为旧名称
- 将旧名称重命名为新名称
所以我们需要跟踪的唯一信息是旧名称和新名称 + 临时文件名。
# Grab the current background and pick the new one
$BGPath = "C:\Windows\System32\oobe\info\backgrounds"
$CurrentBG = Get-Item (Join-Path $BGPath -ChildPath 'backgrounddefault.jpg')
$NewBG = Get-ChildItem $BGPath -Filter *.jpg -Exclude $CurrentBG.Name |Get-Random
# Store the current name of the new background in a variable
$NewBGName = $NewBG.Name
# Now comes the swap operation
# 1. Rename old file to something completely random, but keep a reference to it with -PassThru
$OldBG = $CurrentBG |Rename-Item -NewName $([System.IO.Path]::GetRandomFileName()) -PassThru
# 2. Rename new file to proper name
$NewBG |Rename-Item -NewName 'backgrounddefault.jpg'
# 3. And finally rename the old background back to the name previously used by the new background
$OldBG |Rename-Item -NewName $NewBGName
看看吧。使用变量引用文件,然后交换变量。
${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt}