我有一个目录,其中包含一个名为 Bruce.txt 的文件。我需要将文件名复制到平面文件中。我尝试使用该copy-item命令,但它复制的是内容而不是名称。
是否有将 Bruce.txt(名称而不是内容)复制到平面文件的命令?所以在它完成后会有一个名为 process.txt 的文件,它的内容将是 Bruce.txt。我尝试使用
Copy-Item -Path "C:\Users\Bruce\deploy\*.txt" -Destination "C:\Users\Bruce\process.txt".
deploy 内部是一个名为 Bruce.txt 的文本文件,其内容为select count() from EMP_NR.
我需要 Bruce.txt 将名称复制到 process.txt 而不是 Select count() 等。
对于 Shell 脚本,我使用 ls 命令,它的效果很好,我可以为 Powershell 使用什么?
您需要使用Get-ChildItemcmdlet。
Get-ChildItem C:\Users\Bruce\deploy\*.txt | Select-Object -ExpandProperty Name | Out-File C:\Users\Bruce\process.txt -Force -Append
Run Code Online (Sandbox Code Playgroud)
然而,当你使用PowerShell的,ls实际上你在这里工作,因为会gci和dir,因为他们是为所有别名Get-ChildItem:
> get-alias | ? {$_.DisplayName -ilike "*get-childitem*" }
CommandType Name
----------- ----
Alias dir -> Get-ChildItem
Alias gci -> Get-ChildItem
Alias ls -> Get-ChildItem
Run Code Online (Sandbox Code Playgroud)
如果您愿意,您也可以使用>或>>代替管道Out-File。
由于Get-Childitemcmdlet 返回对象列表,因此您还需要选择要从对象中提取的信息。如果您ls在具有某些内容的目录中执行 a ,您将看到内容被格式化为表格。
通过使用Select-Objectcmdlet,您可以提取要写入文件的对象属性,在本例中为Name属性。
| 归档时间: |
|
| 查看次数: |
17869 次 |
| 最近记录: |