Results 1 to 12 of 12
  1. #1
    3 Star Lounger
    Join Date
    Dec 2009
    Posts
    212
    Thanks
    36
    Thanked 0 Times in 0 Posts

    need vbscript to determine file extension

    I have a file [path\filename.???] The file extension must be changed between .ini and .len relative to my needs.
    I need a script that will tell me the current file extension.
    Can a condition be added to allow me to change the extension to the other extension if appropriate to my current need?
    Thanks.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    The Windows Scripting library's FileSystemObject provides access to the full file name (Name Property (FileSystemObject)), and also the option to rename the file.

    Are you running your code at a system prompt or from another host (e.g., an Office application, an ASP page)?

  3. #3
    3 Star Lounger
    Join Date
    Dec 2009
    Posts
    212
    Thanks
    36
    Thanked 0 Times in 0 Posts
    I am running simple .vbs scripts.
    File name is either bootnow.ini or .len. What I would like is to return just the extension [preferably] or if necessary the entire name and give me an opportunutiy to change it to the other. If extension is .ini, I need a way to change it to .len, if .len, I need an option to change it to .ini
    My thought is an if/then argument
    Ex:
    Current extension is .ini [or .len]
    {or current name is Bootnow.ini}
    Would you like to change extension to .len? [or .ini]
    {change name to bootnow.len}

    Press "Y" or YES
    "Extension changed"
    <Enter> for NO
    [[or any other means to select choice]]

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    This is an example using an arbitrary folder name. Not the most elegant code, but seems to work in a quick test:

    Code:
    Dim fso, fname, f
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")
    fname = "c:\attach\bootnow.ini"
    If fso.FileExists(fname) Then
      If MsgBox("Rename bootnow.ini to bootnow.len?", vbYesNo+vbQuestion) = vbYes Then
        Set f = fso.GetFile(fname)
        f.Name = Left(f.Name, InStrRev(f.Name, ".")) & "len"
      End If
    Else
      fname = "c:\attach\bootnow.len"
      If fso.FileExists(fname) Then
        If MsgBox("Rename bootnow.len to bootnow.ini?", vbYesNo+vbQuestion) = vbYes Then
          Set f = fso.GetFile(fname)
          f.Name = Left(f.Name, InStrRev(f.Name, ".")) & "ini"
        End If
      End If
    End If
    Set f = Nothing
    Set fso = Nothing

  5. #5
    3 Star Lounger
    Join Date
    Dec 2009
    Posts
    212
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Thanks jscher2000. It works except....
    My file needs to be in C:\windows and the script des not work here. I have all permissions to allow and have run as admin with shortcut.
    First part runs stating file and asking to change. If I choose yes, the file des not cahnge but if I try it again I get this error msg. See attached.
    Attached Images Attached Images

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by skipro View Post
    My file needs to be in C:\windows and the script des not work here. I have all permissions to allow and have run as admin with shortcut.
    First part runs stating file and asking to change. If I choose yes, the file des not cahnge but if I try it again I get this error msg. See attached.
    It didn't occur to me to check whether the file exists already. Are you saying it doesn't actually exist when you get this message??

    If the issue is special permissions relating to the Windows folder, we could slide this discussion over to the relevant operating system board. Or you could post there with a link back to this thread to see whether there is a solution for that folder.

  7. #7
    3 Star Lounger
    Join Date
    Dec 2009
    Posts
    212
    Thanks
    36
    Thanked 0 Times in 0 Posts
    It appears to be A UAC issue. I need it to allow the UAC to request permission and me to grant it for the file to make the changes. Can a "wait" be included between the first screen & chice and the actual file rename to give Windows time to get permission?
    Interesting, simple batch files allow the UAC request and response to occur.

    What would I need to add to check if neither file exists, then give choice which to add.

    How do I cross thread to Win 7 forum? I posted there and was informed that was improper.
    Last edited by skipro; 2011-11-01 at 19:43.

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    I don't know anything about the interaction of UAC and scripting. Maybe there is something on Microsoft's Technet or MSDN sites?

  9. #9
    3 Star Lounger
    Join Date
    Dec 2009
    Posts
    212
    Thanks
    36
    Thanked 0 Times in 0 Posts
    jscher2000,
    What would I need to add to your script to check if neither file exists, then give choice which to add.

    I have figured a work around UAC issues.

  10. #10
    Administrator
    Join Date
    Mar 2001
    Location
    St Louis, Missouri, USA
    Posts
    23,573
    Thanks
    5
    Thanked 1,057 Times in 926 Posts
    The Script Center at Technet has a ton of information and links to resources.

    Joe

  11. #11
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,179
    Thanks
    47
    Thanked 983 Times in 913 Posts
    Batch files will tell you what the extension is by using the "for" command.
    Type this at a command prompt:
    for %X in (c:\windows\explorer.exe) do echo Drive:%~dX Path:%~pX File Name:%~nX File Extension:%~xX

    cheers, Paul

  12. #12
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Quote Originally Posted by skipro View Post
    Thanks jscher2000. It works except....
    My file needs to be in C:\windows and the script des not work here. I have all permissions to allow and have run as admin with shortcut.
    First part runs stating file and asking to change. If I choose yes, the file des not cahnge but if I try it again I get this error msg. See attached.
    You can solve this problem by setting up a Scheduled Task {run on demand} with Admin Privileges then create a shortcut to the Task.
    C:\Windows\System32\schtasks.exe /run /TN "Your Shortcut Name Here"
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

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