0

I am not able to embed VBScript in the Windows batch file.

Part of code cadastro.bat :

@echo off
 echo.
set /p opcao3= DESEJA EXPORTAR ? (S-SIM / N-NAO):
if %opcao3%== s goto:Export else goto :Error
if %opcao3%== S goto:Export else goto :Error
if %opcao3%== n goto:Buscar else goto :Error
if %opcao3%== N goto:Buscar else goto :Error
echo.

:Export

 For /f "delims=" %%i in ('Cscript //nologo "script_.vbs" "Selecione uma pasta"') do Set "folder=%%i\%codigoPesq%__RELATORIO.txt"
 For /f "eol=- delims=" %%i in ('find /I "%codigoPesq%__" Registros') do >>"%folder%" Echo.%%i

START %folder%
ECHO.
ECHO.
ECHO.
ECHO.
ECHO                      VERIFIQUE O LOCAL [%folder%]
ECHO.
echo.
echo                      ENTER PARA VOLTAR AO MENU PRINCIPAL

script.vbs:

Dim objFolder, objShell
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Selecione uma pasta.", &H4000, 0)
If Not (objFolder Is Nothing) Then
   wscript.echo objFolder.Self.path
Else
   wscript.echo 0
End If

I just wanted vbscript to be inside the batch, thank you for helping me.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
WillianRC
  • 41
  • 5

1 Answers1

2

As I understand the question you want to have a single file with both vbscript and batch code. If yes try this:

@echo off
 echo.
set /p opcao3= DESEJA EXPORTAR ? (S-SIM / N-NAO):
if %opcao3%== s goto:Export else goto :Error
if %opcao3%== S goto:Export else goto :Error
if %opcao3%== n goto:Buscar else goto :Error
if %opcao3%== N goto:Buscar else goto :Error
echo.

:Export

 For /f "delims=" %%i in ('cscript //nologo "%~f0?.wsf" //job:VBS') do Set "folder=%%i\%codigoPesq%__RELATORIO.txt"
 For /f "eol=- delims=" %%i in ('find /I "%codigoPesq%__" Registros') do >>"%folder%" Echo.%%i

START %folder%
ECHO.
ECHO.
ECHO.
ECHO.
ECHO                      VERIFIQUE O LOCAL [%folder%]
ECHO.
echo.
echo                      ENTER PARA VOLTAR AO MENU PRINCIPAL

exit /b %errorlevel%

<package>
  <job id="VBS">
    <script language="VBScript">
        Dim objFolder, objShell
        Set objShell = CreateObject("Shell.Application")
        Set objFolder = objShell.BrowseForFolder(0, "Selecione uma pasta.", &H4000, 0)
        If Not (objFolder Is Nothing) Then
           wscript.echo objFolder.Self.path
        Else
           wscript.echo 0
        End If
    </script>
  </job>
</package>

This uses a 'hack' described here. Though here the batch part is not put in xml comment block it should work fine and I find it more readable.Usually the cscript parser looks into .wsf files and gets only whats between <package> tags , though for robustness you can put a comment block for the batch code (or CDATA for even more robust code).

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Hello friend, I tested the code that you gave me and I could not export the search. When it goes to search it opens another .BAT. – WillianRC Mar 16 '17 at 22:25