Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
The purpose of this program is to rename lost of files. When I select a file , press F8 to rename it. Why I don't use loop because have same error when I use loop.

Could anyone help me with this?

**
Error on "Dim strClip As String = Clipboard.GetText()'Get current file name."
in Sub procedure : Rename_AddSameValue.

***
Thinks for tell me there's much simplet ways to do that.
:)

VB
Public Class Form1

    Private Sub tmr_Tick(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) Handles tmr.Tick

              If (Win32API.GetAsyncKeyState(Keys.F8) < 0) Then
                Rename_AddSameValue("Test_")

                DownArrow()'press DownArrow jump to next file.

               End If
    End Sub

    Sub Rename_AddSameValue(ByVal strToAdd As String)
        F2()' press F2.

        Sleep(10)
        Ctrl_C()' press Ctrl + C.

        Sleep(10)
        Dim strClip As String = Clipboard.GetText()'Get current file name.

        'Create a new file name by conbine "test_" and current file name.
        Sleep(10)
        strToAdd = strToAdd & strClip

        Sleep(10)
        PasteTxt(strToAdd)'Press Ctrl + V to paste new name.

        Sleep(10)
        EnterPress()'press enter 
    End Sub
End Class
Posted
Updated 8-Jul-11 3:23am
v6
Comments
Slacker007 8-Jul-11 7:09am    
Edited for readability and spelling.
Dave Kreskowiak 8-Jul-11 8:00am    
First, we don't know what the error is so there isn't much anyone can tell you.

Second, why are you poking keyboard commands to rename a file?? There's much simplet ways of doing this without going through all of this unreliable and unsupportable garbage.

For starters, why are you using a timer for a manual process that should be handled by the KeyPress event instead.
Secondly, we can't really give you a lot of other help if you don't tell us what the exception itself is. We aren't mind readers, you know... Cheers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 9:47am    
Well said. 5.
--SA
Sergey Alexandrovich Kryukov 8-Jul-11 10:00am    
Oh... (sigh)... I now I saw the code... It's gone too far. :-) Well, please see my solution.
--SA
All wrong!

Throw out this code and never do anything similar. Don't poll keyboard using timer, use keyboard input events provided by .NET. In general, avoid using timers; use threads whenever possible instead of timers.

Don't use Thread.Spleep for waiting for some condition or without purpose. Never use immediate (hard-coded) constants, especially for delays and strings. Don't simulate key presses in UI development. It might be used is some very special cases when key presses themselves are of interest, for example, for playing keyboard macro, creation of virtual keyboards.

Never use auto-generated names like Form1 or tmr_Tick — they violate good Microsoft naming conventions. Always rename such things to give them semantic names.

This was about what not to do. I could possible tell you what to do if you explain the goals of your activity.

—SA
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jul-11 10:37am    
I totally agree with Sergey on this one. I suspected something was wrong when I saw you were using the timer, but this is kind of scary.
Sergey Alexandrovich Kryukov 8-Jul-11 10:50am    
Scary is a right word. I wanted to say "Oh, not again!", but the problems are even a bit more concentrated then the usual :-).
Thank you, Marcus.
--SA
It sounds as though what you are attempting to do is rename a lot of files. For this you should use the Directory.GetFiles method.

MSDN Link:
http://msdn.microsoft.com/en-us/library/07wt70x2.aspx[^]

Here is a link where files are being renamed with some sample code using Directory.GetFiles.

http://social.msdn.microsoft.com/forums/en-us/vblanguage/thread/73FE5D95-81A8-482E-A491-47A91E42EF8A[^]

I agree with what the other answers here, your method is not the best way to approach this. But I will give you credit for trying to write code first then asking for help rather than just asking for code.

Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900