返回列表 上一主題 發帖

[發問] 判斷檔案存在否? 結果為"False不存在" 的檔案輸出文字檔案

本帖最後由 elinbit 於 2011-8-6 07:47 編輯

Dim NotExistFile As String
    Dim TarFile As String
    Dim OpenFileNum As Integer
   
    NotExistFile = "下列檔案不存在:" & vbCrLf
   
    '以下判斷請自行變化
    If Dir("path/fileA") = "" Then NotExistFile = NotExistFile & "path/fileA" & vbCrLf
    If Dir("path/fileB") = "" Then NotExistFile = NotExistFile & "path/fileB" & vbCrLf
    .
    .
    .
    .
        
    TarFile = "D:\Temp\FilesNotExist.txt" '你想輸出儲存的檔案名稱路徑
      
    OpenFileNum = FreeFile
    Open TarFile For Output As #OpenFileNum
    Print #OpenFileNum, NotExistFile
    Close #OpenFileNum
BOB

TOP

回復 5# webbliu


    1.Sorry不知道你的環境是VB.Net,上述語法是VB6環境下執行的
2. R.Length <  10 和 size <  10 應該都可以
3.Console.WriteLine(Size + "小於" + Apath) 應該不會寫入文字檔
4.底下是MSDN寫入文字檔的範例,請參考修改:
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextToFile
    Private Const FILE_NAME As String = "MyFile.txt"
    Public Shared Sub Main()
        If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists.", FILE_NAME)
            Return
        End If
        Using sw As StreamWriter = File.CreateText(FILE_NAME)
            sw.WriteLine("This is my file.")
            sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
            sw.Close()
        End Using
    End Sub
End Class
BOB

TOP

回復 7# webbliu


    改寫MSDN的範例程式碼應該不難才對~

       Dim path as string

        If File.Exists(path) Then         '如果檔案存在
           If File.Length <  10 Then   '隨便你要加什麼程式碼         
           Else
           End If
       Else        '如果檔案不存在
           Using atxt As StreamWriter = File.CreateText("C:\exists.txt")  '開檔
            atxt .WriteLine("下列檔案不存在:" )  '寫入一行
            atxt .WriteLine(path) '寫入不存在的檔案路徑
            atxt .Close() '關閉檔案
           End Using
        End If

希望對你有幫助~
BOB

TOP

回復 9# webbliu


1. FileInfo類別的File.Length屬性本來就是回傳指定的檔案大小,單位為bytes,不需另外寫一個Function
2.我還沒看過檔案大小為負數的檔案,所以應該用不到檔案大小<0 這樣的判斷式
3.不存在的檔案你無法列出,但是用檔案大小比對卻可以全部列出,表示不是你列出有問題,問題應該出在你的判斷式邏輯,建議你多嘗試不同的邏輯寫法,或許能自己找到你要的結果
BOB

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題