使用文件名将我的项目重命名为变量,但无法使用Powershell进行工作

问题描述 投票:0回答:1

我正在使用Powershell。我的目标是将一项复制到另一个目录(我已经知道该怎么做),获取userID(我已经知道该怎么做),并使用存在于变量中的文件名重命名该文件(不知道我在做什么错)。

当我运行代码时,它给我一个错误:Rename-Item:由于参数null而无法将参数绑定到参数'NewName'。我不知道我在做什么错。

我是从https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-7那里得到的

我的代码在下面列出:

$name = [Environment]::UserName
$mainFile = "\\example\example.txt"
$copiedFile = "\\example\example\copiedFolder\example.xlsx"
$name = [Environment]::UserName
#Once I have copied the file to the new directory, I do the following code which does not work:
$copiedFile = Rename-Item -Path $copiedFile -NewName $name.xlsx
powershell file-rename
1个回答
0
投票

您已经解决了这个问题。这是一个简单的复制命令。

Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results

    Directory: D:\temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        03-Feb-20     12:54            644 reconcile.txt
#>

$mainFile   = 'D:\temp\reconcile.txt'
$FileTarget = 'D:\temp\'
Copy-Item -Path $mainFile -Destination "$FileTarget\reconcile.xlsx" -Force
Get-ChildItem -Path 'D:\temp\reconcile.*'
<#
# Results


    Directory: D:\temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        03-Feb-20     12:54            644 reconcile.txt                                                                                                 
-a----        03-Feb-20     12:54            644 reconcile.xlsx
#>
© www.soinside.com 2019 - 2024. All rights reserved.