Simple File Re-naming Question
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 256   1
URL.หัวข้อ / URL
Simple File Re-naming Question

มีคนถามคำถามนี้ไว้ที่  
http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=001880  
  
เนื้อหาของคำถามมีดังนี้  
  
Hi - and thanx for taking the time to view/answer this post.  
The problem is that I have a folder with a bunch of jpeg files in it.  
The software that created these files used the person's SSN as the file name.  
So what I have is 01234567.8 as the file name.  
How would I go about moving or removing the period. I think I can handle it from 
there as soon as I get the period removed.  
  
TIA,  
  
RoyBoy   
  
สรุป คือ เขามีไฟล์ jpeg อยู่ในโฟล์เดอร์ แต่ตั้งชื่อเป็น 01234567.8 เขาจึงต้องการที่จะเอา . (จุด) ออก จะทำอย่างไร  
  
ความคิดแรกที่เข้ามาในสมองของผมคือ  
  
1. ใช้ File Scripting เข้ามาดึงชื่อไฟล์ขึ้นมา  
2. ใช้คำสั่ง FileCopy และ Kill ในการเปลี่ยนชื่อไฟล์ แต่ผมใช้ Name ... As ... เอา เพราะสั้นดี และทำหน้าที่แทนทั้งสองฟังก์ชันให้แล้ว  
3. แต่ผมยังไม่เข้าใจว่า ไฟล์ของเขามีนามสกุล jpg แล้วหรือยัง  
  
จากโค้ดข้างล่าง ถ้ามีนามสกุลติดอยู่แล้ว เช่น 01234567.8.jpg ให้ใช้บันทัดสีแดง แต่ถ้าไม่มีนามสกุล ให้ใช้บันทัดสีน้ำเงิน  
  
Private Function fRenFiles()  
  
    Dim objFS As Object, objFolder As Object  
    Dim objFiles As Object, objF1 As Object  
    Dim strFill As String, strFolderPath As String  
    Dim TempString As String  
    Dim strOldName As String, strNewName As String  
      
    strFolderPath = "i:/test/"  
    Set objFS = CreateObject("Scripting.FileSystemObject")  
    Set objFolder = objFS.GetFolder(strFolderPath)  
    Set objFiles = objFolder.files  
          
    For Each objF1 In objFiles  
        strOldName = strFolderPath & objF1.Name  
        strNewName = strFolderPath & Replace(objF1.Name, ".", "") & ".jpg"  
        strNewName = strFolderPath & Replace(Left(objF1.Name, Len(objF1.Name) - 4), ".", "") & ".jpg"  
        Name strOldName As strNewName  
    Next  
      
    Set objF1 = Nothing  
    Set objFiles = Nothing  
    Set objFolder = Nothing  
    Set objFS = Nothing  
   
End Function  
  
หมายเหตุ ใน A2K หรือสูงกว่าจะมีฟังก์ชัน Replace() มาให้ แต่ถ้าเป็น A97 ต้องเขียนฟังก์ชันขึ้นมาใช้เอง เอ๊ะ จะเขียนยังงัยดี  
  
ติดตามตอนต่อไปครับ *** Edited by Supap Chaiya *** 3/8/2546 20:48:09

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R00435
ภาคสองมาแล้วครับ ถ้าเป็น A97 จะไม่มีฟังก์ชัน Replace() มาให้ ผมเลยเขียนฟังก์ชันข้างล่างมาใช้แทน Function MyReplace(strString As String, strWhat As String, strWith As String) As String Dim I As Integer, strAll As String, strOut As String If Len(strString) <> 0 Then For I = 1 To Len(strString) strOut = Mid(strString, I, 1) If strOut = strWhat Then strOut = strWith End If strAll = strAll & strOut Next I End If MyReplace = strAll End Function ฉะนั้น ถ้าใครใช้ข้างข้างต้นกับ A97 จะต้องเพิ่มฟังก์ชัน MyReplace เข้าไปด้วย แล้วแทน Replace ด้วย MyReplace ในโค้ดด้วยครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0665s