Return File Name in VBS

Asked

Viewed 859 times

2

Is it possible to return the name of a file/program by a VBS script? Type when dragging and dropping into the script or opening with... the script, it returns the name.

  • Jefter, I think what you want is Wscript.Arguments.

  • could recommend me some material on?

  • It’s not a language I know, but I’m making an example and I’m sending it to you.

2 answers

3


The Wscript.Arguments recognizes the file in the sequence of the parameters as if it were a normal executable and with GetFileName you can take the name and GetAbsolutePathName will take the full path.

An example of a script:

Dim Arg

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Verifica se selecionou arrastou ao menos um arquivo
If WScript.Arguments.Count > 0 Then

    'Itera todos arquivos que soltou em uma ação
    For Each Arg in Wscript.Arguments

        'Remove possíveis espaços
        Arg = Trim(Arg)

        Set objFile = objFSO.GetFile(Arg)

        'Exibe um dialogo somente para testes
        MsgBox("Nome: " & objFSO.GetFileName(objFile) & " - caminho: " & objFSO.GetAbsolutePathName(objFile))
    Next
End If

Example working from script:

exemplo


If you want to limit to one file only, then change the line:

If WScript.Arguments.Count > 0 Then

To

If WScript.Arguments.Count = 1 Then

Should stay like this:

Dim Arg

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Verifica se selecionou arrastou um arquivo
If WScript.Arguments.Count = 1 Then

    'Itera todos arquivos que soltou em uma ação
    For Each Arg in Wscript.Arguments

        'Remove possíveis espaços
        Arg = Trim(Arg)

        Set objFile = objFSO.GetFile(Arg)

        'Exibe um dialogo somente para testes
        MsgBox("Nome: " & objFSO.GetFileName(objFile) & " - caminho: " & objFSO.GetAbsolutePathName(objFile))
    Next
End If

To check if it is a file and not a folder, use FileExists/FolderExists


An example that checks whether it is a file and how many you have selected:

Dim Arg

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Verifica se selecionou arrastou ao menos um arquivo
If WScript.Arguments.Count = 1 Then

    'Itera todos arquivos que soltou em uma ação
    For Each Arg in Wscript.Arguments

        'Remove possíveis espaços
        Arg = Trim(Arg)

        If objFSO.FileExists(Arg) Then
            Set objFile = objFSO.GetFile(Arg)

            'Exibe um dialogo somente para testes
            MsgBox("Nome: " & objFSO.GetFileName(objFile) & " - caminho: " & objFSO.GetAbsolutePathName(objFile))
        Else
            MsgBox("Este arquivo não existe ou é uma pasta")
        End If

    Next
ElseIf WScript.Arguments.Count > 1 Then
    MsgBox("Você mais de um arquivo")
Else
    MsgBox("Você não selecionou nenhum arquivo")
End If

Note: Save the file . vbs as ANSI (windows-1252 or iso-8859-1 or compatible), UTF-8 or other type of Unicode may not be displayed on MsgBox (unless you make adjustments to the script)

2

Yes, it is possible. See Filesystemobject:

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Scripts\Test.txt")

Wscript.Echo "File name: " & objFSO.GetFileName(objFile)
  • I think you do not understand me, see, this script returns the name of the file specified by the variable objFile, my doubt is if it is possible to return the name of the file that was opened dragged and dropped in the script understood?

  • It’s unclear what you want. The answer I gave is to exactly your question. Dragged and dropped? In what part of your application is a Drag n drop?

  • good as can say, I get it in batch, but wanted in vbs see the following set Replay_Locale=%1 echo %Replay_Locale% if I drag and drop in batch it returns me the name/path of the understood file?

  • Marcelo, he wants Desktop interaction with dragdrop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.