连接文件 Url 并检查路径 Powershell 错误
Concatenate File Url and Check for path Powershell error
我正在尝试从用户那里获取文件路径并连接文件名并在文件夹中创建该文件,然后检查该文件是否存在。文件已创建并且路径对我来说看起来是正确的但是当我尝试使用测试路径检查文件路径是否存在时,它总是失败它不存在
我可以看到该文件并且它存在于我的系统中。当我复制路径并尝试在 windows 资源管理器中打开时,它给了我一个错误,但是当我尝试右键单击文件并复制名称然后在 windows 资源管理器中签入时,它起作用了。我不确定连接是否添加了一些特殊字符。
$fileName = Read-Host -Prompt 'Enter file Name :' #user entered Test123
$filePath = Read-Host -Prompt 'Enter Path:' #user entered C:\Documents
$File = ($filePath + "\" + $fileName + ".json")
If (!(test-path $File)) {
Write-Host "File does not exist"
Exit
}
我做错了什么?我正在尝试从用户获取路径并连接文件名并在文件夹中创建该文件,然后检查文件是否存在
您的连接无效。
根据我们下面的交流,删除了原始回复,现在您已经阐明了您的用例。
Clear-Host
$filePath = Read-Host -Prompt 'Enter Path'
$fileName = Read-Host -Prompt 'Enter file Name'
# Debug - check variable content
($File = "$filePath$fileName.json")
If (!(test-path $File))
{Write-Warning -Message 'File does not exist'}
# Results - Using a bad name
<#
Enter Path: D:\Temp
Enter file Name: NoFile
WARNING: File does not exist
#>
# Results - using a good file
<#
D:\temp\NewJsonFile.json
#>
或
Clear-Host
$filePath = Read-Host -Prompt 'Enter Path'
$fileName = Read-Host -Prompt 'Enter file Name'
Try
{
Get-ChildItem -Path "$filePath$fileName.json" -ErrorAction Stop
Write-Verbose 'Success' -Verbose
}
Catch
{
Write-Warning -Message "Error using $fileName"
$PSitem.Exception.Message
}
# Results
<#
Enter Path: D:\temp
Enter file Name: BadFileName
WARNING: Error using BadFileName
Cannot find path 'D:\temp\BadFileName.json' because it does not exist.
#>
# Results
<#
Enter Path: D:\temp
Enter file Name: NewJsonFile
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05-Oct-20 13:06 0 NewJsonFile.json
VERBOSE: Success
#>
如果该目录不存在,您也需要创建它。
$fileName = Read-Host -Prompt 'Enter file Name :' #user entered Test123
$filePath = Read-Host -Prompt 'Enter Path:' #user entered C:\Documents
$File = (Join-Path $filePath "$fileName.json")
If (test-path $filePath)
{
Write-Host Folder exists! -ForegroundColor Cyan
}
else
{
Write-Host Folder does not exist! -ForegroundColor Yellow
$result = New-Item $filePath -ItemType Directory
if($result)
{
Write-Host Folder created successfully! -ForegroundColor Green
}
}
If (test-path $File)
{
Write-Host File exists! -ForegroundColor Cyan
}
else
{
Write-Host File does not exist! -ForegroundColor Yellow
$result = New-Item $file -ItemType File
if($result)
{
Write-Host File created successfully! -ForegroundColor Green
}
}
我正在尝试从用户那里获取文件路径并连接文件名并在文件夹中创建该文件,然后检查该文件是否存在。文件已创建并且路径对我来说看起来是正确的但是当我尝试使用测试路径检查文件路径是否存在时,它总是失败它不存在
我可以看到该文件并且它存在于我的系统中。当我复制路径并尝试在 windows 资源管理器中打开时,它给了我一个错误,但是当我尝试右键单击文件并复制名称然后在 windows 资源管理器中签入时,它起作用了。我不确定连接是否添加了一些特殊字符。
$fileName = Read-Host -Prompt 'Enter file Name :' #user entered Test123
$filePath = Read-Host -Prompt 'Enter Path:' #user entered C:\Documents
$File = ($filePath + "\" + $fileName + ".json")
If (!(test-path $File)) {
Write-Host "File does not exist"
Exit
}
我做错了什么?我正在尝试从用户获取路径并连接文件名并在文件夹中创建该文件,然后检查文件是否存在
您的连接无效。
根据我们下面的交流,删除了原始回复,现在您已经阐明了您的用例。
Clear-Host
$filePath = Read-Host -Prompt 'Enter Path'
$fileName = Read-Host -Prompt 'Enter file Name'
# Debug - check variable content
($File = "$filePath$fileName.json")
If (!(test-path $File))
{Write-Warning -Message 'File does not exist'}
# Results - Using a bad name
<#
Enter Path: D:\Temp
Enter file Name: NoFile
WARNING: File does not exist
#>
# Results - using a good file
<#
D:\temp\NewJsonFile.json
#>
或
Clear-Host
$filePath = Read-Host -Prompt 'Enter Path'
$fileName = Read-Host -Prompt 'Enter file Name'
Try
{
Get-ChildItem -Path "$filePath$fileName.json" -ErrorAction Stop
Write-Verbose 'Success' -Verbose
}
Catch
{
Write-Warning -Message "Error using $fileName"
$PSitem.Exception.Message
}
# Results
<#
Enter Path: D:\temp
Enter file Name: BadFileName
WARNING: Error using BadFileName
Cannot find path 'D:\temp\BadFileName.json' because it does not exist.
#>
# Results
<#
Enter Path: D:\temp
Enter file Name: NewJsonFile
Directory: D:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 05-Oct-20 13:06 0 NewJsonFile.json
VERBOSE: Success
#>
如果该目录不存在,您也需要创建它。
$fileName = Read-Host -Prompt 'Enter file Name :' #user entered Test123
$filePath = Read-Host -Prompt 'Enter Path:' #user entered C:\Documents
$File = (Join-Path $filePath "$fileName.json")
If (test-path $filePath)
{
Write-Host Folder exists! -ForegroundColor Cyan
}
else
{
Write-Host Folder does not exist! -ForegroundColor Yellow
$result = New-Item $filePath -ItemType Directory
if($result)
{
Write-Host Folder created successfully! -ForegroundColor Green
}
}
If (test-path $File)
{
Write-Host File exists! -ForegroundColor Cyan
}
else
{
Write-Host File does not exist! -ForegroundColor Yellow
$result = New-Item $file -ItemType File
if($result)
{
Write-Host File created successfully! -ForegroundColor Green
}
}