set batch variable from WSH script

Viewed 33

I am trying to run a batch file that gets a variable from WSH script. When I run the script I get the correct output but I can't seem to set it to a variable

@if (@codesection == @batch) @then


cscript //nologo //e:JScript "%~f0"

set getclip=cscript //nologo //e:jscript "%f~0"

echo "%getclip%"
pause


goto :EOF
@end

WSH.Echo(WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text'));

the second line echos the clipboard but not the getclip variable

1 Answers

You have a small error in this part: //e:jscript "%f~0"

In order to get the output of a command, you need to use the for /F command.

Also, a variable can only store one line. If you want to get several lines, you need to use an array:

@if (@codesection == @batch) @then

@echo off
setlocal EnableDelayedExpansion

cscript //nologo //e:JScript "%~f0"

ECHO ===============

set "line=0"
for /F "delims=" %%a in ('cscript //nologo //e:JScript "%~f0"') do (
   set /A line+=1
   set "getclip[!line!]=%%a"
)

for /L %%i in (1,1,%line%) do echo !getclip[%%i]!
pause

goto :EOF

@end

WSH.Echo(WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text'));
Related