【问题标题】:Switching the names of two files切换两个文件的名称
【发布时间】:2016-07-21 19:14:43
【问题描述】:

我正在编写一个脚本来更改计算机的登录背景。我已经让它做我需要的一切,但我试图让脚本更高效,因为就目前而言,在选择一个新脚本后,我创建的一个名为 OrderNames 的函数被调用以将所有内容重命名为随机,然后将它们重命名为 background1、2、3 等等。这是我正在使用的 sn-p:

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 等于 temp。但是当我创建一个值为 BGOld 的临时变量时,它不起作用。事实上,变量似乎并没有随着重命名函数而改变,并且设置一个等于另一个会导致一个

无法重命名,因为项目 at 不存在。

很好,所以我尝试将文件名设置为带有Select basename 的变量,但我收到一个错误

无法索引到 system.io.fileinfo 类型的对象。

另外,我尝试了$BGOld.FullName = $BGNew.FullName,尝试了Rename-Item 和其他一些我现在不记得了。

我试图复制项目名称,但这也不起作用。我希望这不是我忽略的简单事情。

TL;DR
是否可以将文件名复制到临时变量中,这样当我重命名文件时,我可以将临时变量的名称复制回“旧”变量以避免重命名所有内容?或者更好的是,我可以切换文件名吗?

【问题讨论】:

  • 您似乎回答了自己的问题:将第一个文件重命名为临时名称,将第二个文件重命名为第一个文件的名称,然后将临时文件重命名为第二个文件的名称。
  • 在将新文件重命名为旧文件之前,将磁盘上的文件重命名为随机文件 is 文件系统等效于您在 C 变量中在内存中所做的操作交换你描述的
  • @Bill_Stewart 我试过这样做,但它不会让我这样做 - 当我尝试得到有关文件名不存在的错误时,甚至更好,路径为空。
  • @Mathias R. Jessen 我理解,但它不允许我将名称存储到临时变量,然后将临时变量应用于旧变量的名称。
  • @user6111573 请看我发布的答案

标签: powershell renaming


【解决方案1】:

是的,您可以在 PowerShell 中执行类似的操作。这方面的“算法”与您为 C 描述的基本相同:

  1. 将旧名称重命名为临时名称
  2. 将新名称重命名为旧名称
  3. 将旧名称重命名为新名称

因此,我们需要跟踪的唯一信息是旧名称和新名称 + 临时文件名。

# 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

【讨论】:

  • 谢谢!我没有正确初始化我的临时名称,并且不知道像您列出的那样使用 [System.IO.Path]。也没有像我应该的那样进入 Rename-Item。原谅我的无知,还没有了解powershell的所有来龙去脉。
  • @user6111573 不要太担心,我们都生活和学习 :) 请注意,将集合通过管道传送到 Get-Random 也会从集合中返回一个随机项,比计算随机索引
【解决方案2】:

检查一下。使用变量引用文件,然后交换变量。

${c:file1.txt},${c:file2.txt} = ${c:file2.txt},${c:file1.txt}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多