• need vbscript to determine file extension

    Home » Forums » Developers, developers, developers » DevOps Lounge » need vbscript to determine file extension

    Author
    Topic
    #479781

    I have a file [pathfilename.???] 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.

    Viewing 5 reply threads
    Author
    Replies
    • #1304535

      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)?

      • #1304651

        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”
        for NO
        [[or any other means to select choice]]

    • #1304778

      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:attachbootnow.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:attachbootnow.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
      • #1304782

        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.

        • #1304798

          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.

          • #1304902

            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.

        • #1350557

          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.
          [noparse]C:WindowsSystem32schtasks.exe /run /TN “Your Shortcut Name Here”[/noparse] :cheers:

          May the Forces of good computing be with you!

          RG

          PowerShell & VBA Rule!
          Computer Specs

    • #1304974

      I don’t know anything about the interaction of UAC and scripting. Maybe there is something on Microsoft’s Technet or MSDN sites?

    • #1304980

      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.

    • #1305050

      The Script Center at Technet has a ton of information and links to resources.

      Joe

      --Joe

    • #1350550

      Batch files will tell you what the extension is by using the “for” command.
      Type this at a command prompt:
      for %X in (c:windowsexplorer.exe) do echo Drive:%~dX Path:%~pX File Name:%~nX File Extension:%~xX

      cheers, Paul

    Viewing 5 reply threads
    Reply To: need vbscript to determine file extension

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: