Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Renaming files (VB / VBS)

    I am working in both VB and VBS -- so I posted here. Remember, think of me as a scripting and programming idiot with no knowledge of coding whatsoever.

    I want to simply rename a file using the Scripting.FileSystemObject. Sounds easy, right?? For you! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    I also want a message box (Echo) to pop up along the way to verify what the heck I am doing!

    Let's keep it simple and use VBS first:
    -------------------------------------------------------

    Dim oFS, oFile

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.GetFile(C:WindowsDesktopjunk.txt)

    x = oFile.Name

    Call WScript.Echo("File Name is:" & x)

    oFile.Name = newjunk.txt

    Call WScript.Echo("New File Name is:" & x)
    --------------------------------------------------------------------

    OK, again being a complete idiot, that makes sense to me -- but I am SURE it is LOADED with errors. Please help an idiot. Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Renaming files (VB / VBS)

    I'll point out the most obvious error, but I don't work in VBS, so I may have missed something. You're echoing the "new file name" here:

    Call WScript.Echo("New File Name is:" & x)

    However, you don't have any code to populate the variable with the new file name. It still contains the original assignment.
    Charlotte

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Renaming files (VB / VBS)

    Well first, any time you give VBS a literal string, you need to use " marks around it. The script interpreter will give you weird messages if you don't, messages that fail to explain the problem, so just always check for this until it is second nature. And then check again.

    VBS lets you use the MsgBox function, which I expect is more flexible than the Echo method. So you could do this:

    Dim oFS, oFile, varReturn
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.GetFile("C:windowsdesktopjunk.txt")
    If MsgBox("File Name is: " & oFile.Name & " ... and I will rename it now. Cool?",1) = 1 Then
    oFile.Name = "newjunk.txt"
    varReturn = MsgBox("New File Name is: " & oFile.Name)
    End If

    This code assumes, of course, that the file exists. Otherwise, you get an error on the third line. If that is important, we can add some error checking. You might be able to find the code already baked here on this board.

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    I was hoping the "x" variable would populate the Echo box.

    The Echo box is not really my problem -- I only added that to allow me to "peak" inside as the script ran. The dang thing doesn't seem to run at all.[img]/forums/images/smilies/sad.gif[/img]

    The FileSystemObject is part of "scrrun.dll", isn't it?? And I have that on this computer...

    I am away from home. Let me download the MS Script De###### and see what it says...

    Thanks to both of you.

  5. #5
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    Ahh... Jeez! It must have been that stupid Echo box! Your script runs fine. Junk is NewJunk. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Now, I don't really need the Msg Box (it appears to have been a stupid idea on my part anyway), so now I need to change this into classic VB. The first thing I THINK I need to do is "declare" the variables a "types" -- correct?

    Remember, think of me as a 7 year old child. Make that a retarded 7 year old child.

    So, to do that I could MAYBE do this:
    _______________________________________________

    Dim oFS As ?Object? (duh, what do I call this?)
    Dim oFile As ?Object? (ditto)

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFile = oFS.GetFile("C:windowsdesktopjunk.txt")

    oFile.Name = "newjunk.txt"
    ______________________________________________

    Is that ALL I need to do to simply rename a file?? Thanks.
    __________________________________________________ ________

    NEXT....

    I want to create and populate a text file. To expand, I want to create a ".txt" file somewhere and then Rename it as something else. More specifically, I want to change the file extension from .txt to something different -- eh, like .xml.

    I want to create a VB application that will write a SIMPLE .xml file. From what I can read (given my minimalist level of programming knowledge), I THINK the simplest way for ME to do this is to make a .txt file using the FileSystemObject | TextStreamObject | WriteLine method.

    Many of the lines are "static" -- eh, like <XML> and the like. But others need to change based on what radio buttons the user selects.

    I can create the "user interface" with Visual Basic as good as the next guy -- I just have absolutely NO programming knowledge to make the dang thing work!! OK, to be honest, I am trying to learn -- but it kinda like waking up in the middle of China and only being able to speak English! ;-)

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Renaming files (VB / VBS)

    A quick lounge search for Scripting FileSystemObject led me to a number of posts, including <post#= 197697>post 197697</post#> which shows the syntax...

    Dim fso As New Scripting.FileSystemObject
    Dim f As Scripting.File
    Dim s as String
    ...

    Set f = fso.GetFile(s)
    ...

    StuartR

  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    You can name the file with any extension you like when you create it, so you may not need to rename, Also if the contents sre short you can compose the entire file as a variable and just write it to the file in one write operation.

    The following is an example :<pre>Sub WriteXML()
    Dim fso, f
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile("D:testxml.xml", ForWriting, True)
    With f
    .Write "This is a test" & vbCrLf & "Writing to text file"
    .Close
    End With
    Set f = Nothing
    Set fso = Nothing
    End Sub</pre>

    Andrew C

  8. #8
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    Thanks.

    OK, I have another little challenge. I want to create a simple VB script that can rename a file "B" if it is named "A", and then rename it "A" if it is "B". A "Toggle Rename" -- if you will.

    At first I thought I could do this with Select Case ... End Select. But when I tried to write it in a logical fashion, it did not make sense.

    So, I thought about this idea:
    -----------------------------------------------

    Dim oFS, oFileA, oFileB

    On Error Resume Next

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFileA = oFS.GetFile("C:windowsdesktopWorkjunk.txt")
    Set oFileB = oFS.GetFile("C:windowsdesktopWorknewjunk.txt")

    oFileA.Name = "newjunk.txt"
    oFileB.Name = "junk.txt"

    --------------------------------------------------------

    Is that logical??? Hmmmm... only in MY head -- not on my computer!

    I caught one error and changed it. Let's try again.

    Done! It worked!! It is "logical" from a programmer's view -- or is it just stupid, but it still works??
    Also, should I add:

    set oFS = Nothing

    at the end -- and why??

    I really thought "Select Case" was the way to go, but I could not get it to work....
    Thanks!!! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
    __________________________________________________ __________________________________

    A for directly creating an .xml file -- I guess I could, but what I want it a file with many lines in it and several options on each line. What I want to make is an interface that allows a user to create a personalized .xml file by:

    1) Opening my program
    2) Deciding on several radio button options (10-20)
    3) Clicking "OK" which then writes the file.

    I don't care if the initial file is a .txt file or not. Now that I can rename it in a second -- with functional VB code -- that is no longer an issue.

  9. #9
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    AFAIK in VB, no need for the FSO:

    Name "C:TESTA.txt" As "C:TESTB.txt"

    Renames the file A.txt to B.txt
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  10. #10
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    LOVE IT! [img]/forums/images/smilies/smile.gif[/img]

    OK, but I needed to learn to work with the FSO anyways, so I consider this a 'learning experience'.

    Please help me learn some more. OK, the Echo idea was a flop. So... I tired to use WshShell.Popup. But, again I am doing something wrong -- and I KNOW it is something really stupid -- but I don't know what!

    Take a look at this and tell me what I am doing wrong:
    ------------------------------------------------------------------------------

    Dim oFS, oFileA, oFileB, sTextA, sTextB, sTitleA, sTitleB

    On Error Resume Next

    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oFileA = oFS.GetFile("C:windowsdesktopWorkjunk.txt")
    Set oFileB = oFS.GetFile("C:windowsdesktopWorknewjunk.txt")

    oFileA.Name = "newjunk.txt"
    oFileB.Name = "junk.txt"

    sTextA = "File was renamed newjunk.txt"
    sTextB = "File was renamed junk.txt"
    sTitleA = "Rename One"
    sTitleB = "Rename Two"

    If oFileA.Name = "newjunk.txt" Then
    WshShell.Popup sTextA, 2, sTitleA
    End If

    If oFileB.Name = "junk.txt" Then
    WshShell.Popup sTextB, 2, sTitleB
    End If

    Set oFS =Nothing
    -----------------------------------------------------------------

    OK, the concept here is to rename the file -- and then BRIEFLY (2 secs) display a notification about the name change. This is better (to me) than having to click OK on a Msg Box.

    Please tell me where my logic falls apart. THANK YOU.

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Renaming files (VB / VBS)

    Okay, I will suggest that you take a look at the FileExists method, because otherwise your script could well halt on a nonexistent file. Others can help sketch in other details. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  12. #12
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    Thanks, Jefferson. I will look into that. I know the file exists and anyone using my final script will know the file exists as well. But I need to learn about that method regardles.

    How about the Popups?? Any ideas what I am doing wrong?

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Renaming files (VB / VBS)

    Another thought: Those files can't both exist at the same time, because the first rename would fail.

    I don't know anything about a popup method. Are you getting an error message?

  14. #14
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Renaming files (VB / VBS)

    Here is info on the WshShell.Popup method. I really don't want a button to be pushed -- I want the Popup to appear for 2 seconds, and then dismiss itself with no user intervention.

    I guess I could use a exclamation icon by sticking a "48" in there at the end....
    --------------------

    Exactly -- both files cannot exist at the same time. So, my simplistic thoughts were:

    If one file does not exist, the one of the If... Then statements would FAIL. No big deal -- I have "On Error Resume Next" up top, so the script should procede even if one line fails.

    Sounds good on paper, but the script does not work. This must have a simply answer. Do I need to post in a different forum??? <img src=/S/frown.gif border=0 alt=frown width=15 height=15> I thought the VB wizard would be here.

  15. #15
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Renaming files (VB / VBS)

    Comment out On Error Resume Next or we'll never figure it out!!

    But seriously, one should only use that after all debugging is completed. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •