简体   繁体   English

Powershell 中变量定义文件路径内的 Out-File 文本输入

[英]Out-File text input inside of a variable defined filepath in Powershell

I have a powershell GUI script that is taking simple inputs from the user.我有一个 powershell GUI 脚本,它从用户那里获取简单的输入。 the inputs are stored as following:输入存储如下:

trackingnumber = "enter tracking number: " + $txtTrack.Text

I also have a dynamic folder that I would like to write to created that is named based off of the user input我还有一个动态文件夹,我想将其写入创建的文件夹,该文件夹是根据用户输入命名的

folderOut = "\\\C:\Users\name\Desktop\" + $($txtTrack.Text)

I am trying to write the user input in a simple text file inside of that dynamic folder (folderOut)我正在尝试将用户输入写入该动态文件夹 (folderOut) 内的一个简单文本文件中

I tried using: $txtTrack.Text | out-file -FilePath ($folderOut + ($txtTrack.Text) + ".txt")我尝试使用: $txtTrack.Text | out-file -FilePath ($folderOut + ($txtTrack.Text) + ".txt") $txtTrack.Text | out-file -FilePath ($folderOut + ($txtTrack.Text) + ".txt")

However, it never saves inside of the $folderOut custom folder path it just saves on the desktop.但是,它永远不会保存在它只是保存在桌面上的 $folderOut 自定义文件夹路径中。 How can I save the user's input in a text file inside of the dynamic folder path ($folderOut)如何将用户的输入保存在动态文件夹路径 ($folderOut) 内的文本文件中

I can't see where trackingnumber is used in your commands listed, but if the GUI text box is $txtTrack and you are using that as the tracking number input then the below should work.我看不到在您列出的命令中使用了trackingnumber的位置,但是如果 GUI 文本框是$txtTrack并且您将其用作跟踪号码输入,那么下面应该可以工作。

$folderOut = "C:\Users\name\Desktop\" + $($txtTrack.Text)
$txtTrack.Text | out-file -FilePath ($folderOut + ".txt")

Previously you had it as ($folderOut + ($txtTrack.Text) + ".txt") .以前你把它作为($folderOut + ($txtTrack.Text) + ".txt") Assuming the tracking number was 123 then the output file would write 123 into the text file called C:\Users\name\Desktop\123123.txt as you doubled up the $txtTrack.Text in the file path.假设跟踪号码是123 ,那么 output 文件会将123写入名为C:\Users\name\Desktop\123123.txt的文本文件,因为您将文件路径中的$txtTrack.Text加倍。

If you have an OnClick event for a button or something, I would recommend doing som error checking to ensure the text box has valid text inside of it.如果你有一个按钮或其他东西的OnClick事件,我建议你做一些错误检查以确保文本框里面有有效的文本。

It would be a good idea to use Join-Path to concatenate directories.使用Join-Path连接目录是个好主意。

folderOut = Join-Path -Path (Join-Path -Path $Env:USERPROFILE -ChildPath 'Desktop') -ChildPath $txtTrack.Text
$txtTrack.Text | Out-File -FilePath ($folderOut + ($txtTrack.Text) + ".txt")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM