0

Have a small bash script which I would like to put it into a method in ruby the script content is as follows.

def test_method
  `@echo off`
  `SETLOCAL ENABLEDELAYEDEXPANSION`
  `SET LinkName=Projects`
  `SET Esc_LinkDest=%userprofile%\Links\!LinkName!.lnk`
  `SET Esc_LinkTarget=C:\Projects`
  `SET cSctVBS=CreateShortcut.vbs`
  `SET LOG=".\%~N0_runtime.log"`
  `((`
    `echo Set oWS = WScript.CreateObject^("WScript.Shell"^)`
    `echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)`
    `echo Set oLink = oWS.CreateShortcut^(sLinkFile^)` 
    `echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)`
    `echo oLink.Save`
    `)1>!cSctVBS!`
  `cscript //nologo .\!cSctVBS!`
  `DEL !cSctVBS! /f /q`
  `)1>>!LOG! 2>>&1`
end

It executes till (( i.e line number 8 and gives me the error No such file or directory - (( That part in the bash script is something which I am writing some content to a file.

Just want to execute that method and make it work like if I had written the same content in a bat file it works absolutely fine.

1 Answer 1

2

You are trying to execute each single line of your script, what is wrong. Just prepare your script and then execute it all in once.

cmd = <<EOC
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=Projects
SET Esc_LinkDest=%userprofile%\Links\!LinkName!.lnk
SET Esc_LinkTarget=C:\Projects
SET cSctVBS=CreateShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)
echo Set oLink = oWS.CreateShortcut^(sLinkFile^) 
echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>>!LOG! 2>>&1
EOC

# OK, time to rock’n’roll!
`#{cmd}`
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. The logic looks rite and I can understand where I went wrong. But I am not able to get this script to work. When I execute the last line the logic is still not working.
This basically creates a vb scipt for creating a shortcut icon in the favorites folder. If I paste this in a bat file it works. But not when I run it like how you given it above. Can you please help me out ?
Sorry, I do not know anything about Windows OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.