を失敗した私は、文字列型の2つの変数を連結しようとすると、私の結果は、最初の変数です:VB - 2つの文字列、変数の連結は
Function newXlsx(ByVal sFilepath As String) As Boolean
Dim sFileName As String
Dim sTest As String
sTest = sFilepath.Trim()
sFileName = Format(Now, "yyyy-MM-dd") & ".xls*"
MsgBox(sTest & "\" & sFileName)
If My.Computer.FileSystem.FileExists(sFilepath & "\" & sFileName) Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
End Function
注:sFilepath
関数に与えられたが、「Hです:\ A74 "の後ろに複数のスペースがあるので、" 012 "、" 0 "、なぜか私はTrim()
という文字列です。だからsTest
は "H:\ A74"で、sFileName
は "2016-06-22.xls *"ですが、結果は "H:\ A74 \ 2016-06-22.xls *"ではなく、ちょうど "H:\ A74"です。あなたの代わりにあなたのトリムされた文字列のMy.Computer.FileSystem.FileExists(sFilepath & "\" & sFileName)
を使用している
Function newXlsx(ByVal sFilepath As String) As Boolean
Dim sFileName = Date.Now.ToString("yyyy-MM-dd") & ".xls"
Dim path = System.IO.Path.Combine(sFilepath.Trim(), sFileName)
Dim exists = System.IO.File.Exists(path)
If exists Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
Return exists
End Function
注:このような問題を防ぐために
IO.Pathメソッドを使用しますブレークポイントとコードをステップ実行すると、これらの問題を見つけるのに役立ちます。 –
私は可能な限り正確な問題を記述しました。私は、その問題を見つけるためにbpを使用しました。 – flohdieter
拡張子の末尾にアスタリスクが付いていると問題になります。 –