How to give the option for the user to choose the destination folder in BATCH?

Asked

Viewed 695 times

1

I do not want to enter the destination manually, for example:

set /p destino=Escolha o local de destino:
robocopy /s /e pasta %destino%

RESOLVED: Using cmd (set & for) + Powershell (new-Object & . Browseforfolder)

    set "psCommand="(new-object -COM 'Shell.Application')^.BrowseForFolder(0,'Por favor, escolha um destino para guardar os seus dados.',0,0).self.path""
    for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
    setlocal enabledelayedexpansion

2 answers

3

• Testado nas seguintes versões do Windows®:

XP Pro 32 bits, 7 Pro 64 bits, 8.0 Pro 64 bits & 10 Pro 64 bits


• There is an option to use resources from VBS to the folder choice via window / GUI, using a bat which creates at runtime a vbs file. and running in a way that saves the briefcase chosen.

• Navegando até a pasta alvo usando a interface gráfica e mouse/rato:

• For use in a bat capturing in the variable the briefcase selected/chosen, use:

@for /f "tokens=*delims= " %%i in ('cscript //nologo escolhe_pasta.vbs')do @echo/%%i

• For use in command line capturing in the variable the briefcase selected/chosen, use:

@for /f "tokens=*delims= " %i in ('cscript //nologo escolhe_pasta.vbs')do @echo/%i


inserir a descrição da imagem aqui


Escolher_Pasta.vbs ::• Adaptação de Scripts VBS de autoria de Rob van der Woude's

Option Explicit
Dim strPath
strPath = SelectFolder( "" )
If strPath = vbNull Then
    WScript.Echo "Cancelled"
Else
    WScript.Echo strPath
End If
Function SelectFolder( myStartFolder )
    Dim objFolder, objItem, objShell
    On Error Resume Next
    SelectFolder = vbNull
    Set objShell  = CreateObject( "Shell.Application" )
    Set objFolder = objShell.BrowseForFolder( 0, "Select Folder", 0, myStartFolder )
    If IsObject( objfolder ) Then SelectFolder = objFolder.Self.Path
    Set objFolder = Nothing
    Set objshell  = Nothing
    On Error Goto 0
End Function

• Replying command line/no_file bat

rem :: na linha de comando ::
@for /f "tokens=*delims= " %i in ('cscript //nologo escolhe_pasta.vbs')do robocopy /s /e pasta "%~i"

rem :: no arquivo bat ::
@echo off & for /f "tokens=*delims= " %%i in ('cscript //nologo escolhe_pasta.vbs')do robocopy /s /e pasta "%%~i"


0

Use the say in a loop for to go through the returned directories:

@echo off
rem Habilita a expansão de variáveis de ambiente atrasada
    setlocal EnableDelayedExpansion
    echo.
    echo ** Pastas disponíveis:
    echo.
rem Monta a lista de bases disponíveis
    set CONTADOR=0
    for /f %%i in ('dir /AD /B') do (
       set /a CONTADOR=!CONTADOR! + 1
       echo [!CONTADOR!] - %%i
    )
    echo.
    set /p PASTA=Qual pasta deseja selecionar?
    echo.
rem
rem ....
rem
rem Finalização
     endlocal
  • Thank you for the short reply Murillo!

  • Murillo, is there the possibility of displaying the Windows window? That is, that the user searches the target folder with the mouse.

  • In this case you would need to use a visual framework, such as Java Swing, or Javafx, or Delphi, etc.

  • Thank you Murillo!

Browser other questions tagged

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