Concatenate File Url and Check for path Powershell error

366 views Asked by At

I am trying to get file path from the user and concatenating the file name and creating that file in the folder and then checking if the file exists. File is created and path looks correct to me but when I try to check if file path exists using test-path, it keeps failing that it does not exist

I can see the file and it exists in my system. When I copy the path and try to open in windows explorer, it gives me an error but when I try to right click on the file and copy the name and then check in windows explorer, it works. I am not sure if concatenation is adding some special characters.

$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 
    }

What am I doing wrong? I am trying to get path from user and concatenating the file name and creating that file in the folder and then checking if the file exists

2

There are 2 answers

6
postanote On BEST ANSWER

Your concatenation is not valid.

Based on our exchange below, removed the original response, now that you've clarified your use case.

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
#>

Or

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
#>
0
Doug Maurer On

If the directory doesn't exist, you'll need to create that as well.

$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
    }
}