2014-02-26 33 views
0

我打開完全更改此代碼。原始鏈接位於代碼本身中。我確信有一個更簡單的方法來做到這一點,實際的重命名部分不是我自己的代碼,所以我會重做它,所以它不是抄襲。我無法使用批處理文件重命名器來執行此操作;我需要自己做,以避免法律問題:)沒有灰色地帶!如何修復我的VBA重命名程序的0返回?

無論如何,經過幾十次嘗試後,我終於在網上抓住並抓住了這個代碼,該代碼應該重命名我指定的文件。我編輯它以適應我的參數和分配的變量/目錄。但是,當我運行它時,我總是得到零返回,並且文件不會被重命名。我能想到的一件事是,這個目錄將轉到文件夾的完整路徑名,而不是最後一個「\」之後的部分。但我不知道如何解決這個問題。我試圖告訴它只是告訴它拉,說的字符串的最後8個字符,但是這將無法正常工作,因爲這些字符串長度將從一個字符到20個左右字符的任何地方變化。

這裏是我的代碼:

Private Sub Apply_Click() 
'This will initiate Module 1 to do a batch rename to find and replace all 
'Module 1 will then initiate the resolving links process 

Dim intResponse As Integer 'Alerts user to wait until renaming is complete 
intResponse = MsgBox("Your folders are being updated. Please wait while your files are  renamed and your links are resolved.") 

If intResponse = vbOK Then 'Tests to see if msgbox_click can start a new process 
Dim i As Integer 
Dim from_str As String 
Dim to_str As String 
Dim dir_path As String 


from_str = Old_Name_Display.Text 
to_str = New_Name.Text 

dir_path = New_Name.Text 
If Right$(dir_path, 1) <> "\" Then dir_path = dir_path _ 
    & "\" 

Old_Name_Display = dir$(dir_path & "*.*", vbNormal) 
Do While Len(Old_Name_Display) > 0 
    ' Rename this file. 
    New_Name = Replace$(Old_Name_Display, from_str, to_str) 
    If New_Name <> Old_Name_Display Then 
Name Old_Name_Display.Text As New_Name.Text 
     i = i + 1 
    End If 

    ' Get the next file. 
    Old_Name_Display = dir$() 
Loop 

MsgBox "Renamed " & Format$(i) & " files. Resolving links now." 
If intResponse = vbOK Then 
MsgBox "You selected okay. Good luck coding THIS." 'Filler line to test that next step  will be ready to initialize 
Else: End 
End If 

Exit Sub 


'Most of batch renaming process used from VB Helper, sponsored by Rocky Mountain Computer Consulting, Inc. Copyright 1997-2010; original code available at http://www.vb-helper.com/howto_rename_files.html 


End Sub 

沒有人有,爲什麼我得到一個0返回另一個理論/如何解決上述問題的潛力?

+0

附:忽略這些模塊1註釋。自從Module引起我的問​​題之後,我決定在表單中自己做。 – meer2kat

+1

你到底想要做什麼?從查看代碼很難說清楚。這真的不應該被標記爲批處理文件,因爲它是vba代碼。它當然可以批量完成。編輯你的帖子,讓我們知道你要做什麼,具體來說,你會得到更好的答案。 –

+0

吉米知道了。你會怎樣稱呼它以備將來參考?我正在做一個批處理文件重命名器會做同樣的事情...我找到文件並將它們重命名爲一個組...對不起,夥計們。我是機械工程專業的學生,​​而不是程序員。我只是在做一個編程項目。 – meer2kat

回答

1

它看起來不像目錄在重命名中被引用。

變化

Name Old_Name_Display.Text As New_Name.Text 

Name Dir_Path & Old_Name_Display.Text As Dir_Path & New_Name.Text 
+0

不幸運:( 它仍然運行,但結果爲0. Old_Name_Display也包含完整路徑。在這種情況下(鏈接到我的虛擬項目):U:\ Common \ AWeaver \ Project Folder \ 10000 – meer2kat

+0

我可以設置目錄路徑,然後重新建立Old_Name_Display和New_Name作爲常規字符串,或者將它搞砸了嗎?我剛剛在這個週末開始使用VB,所以我不知道。 – meer2kat

+0

是的,您可以。Old_Name_Display我保證會丟失嘗試在調試器中設置一個監視器,並用F8逐步執行代碼,你將會看到更好的圖像.Dir $()將你的字符串從路徑中刪除。 –