I am really new to coding (I started around 3 weeks back) I have found Robocopy and have written a really simple batch file that basically copies a folder from a master directory and then pastes in into a sub folder in 2 destinations that I have specify when I launch the batch file.
This looks messy and I have been trying HTAs and trying to get it so that I can type the variable into the HTA and then that do everything else for me. I was thinking this would just look nicer than a CMD window.
I have got the HTA in a simple version that I will style later but I just cannot figure out how to pass the variable into the batch file or if there is a better way to do this. I have read through so many posts on this, but they all seem far more complex than what I need and ultimately I am failing.
My batch file is currently
CODE:
set /p dest=Please enter destination:
robocopy "D:\MasterFolder\- Details" "E:\Sets\%dest%" /E /V /NP /R:10 /W:30
robocopy "D:\MasterFolder\- Details" "F:\%dest%" /E /V /NP /R:10 /W:30
This does work well but just isn't very nice to look at, I was hoping that with a HTA I could get away from the first line and have that input in a GUI. So far I have managed to come up with the following I just need some help to get this to take the variable from the HTA and place this into the batch file. Or some guidance if there is a better way that this can be achieved
<head>
<title>Folder Creator</title>
<HTA:APPLICATION
APPLICATIONNAME="HTA Folder Creator"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
>
</head>
<script type="text/javascript">
window.resizeTo(400,200);
</script>
<body>
<label for="dest">Destination:</label>
<input type="text" id="dest">
<br><br>
<button onclick="closeHTA(true);">Create</button>
</body>
Here is an example to take a variable from HTA to batch script :
Reply_from_HTA.bat
<!-- :: Batch section
#echo off
Title Pass variable from HTA to batch
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply from HTA GUI : "%HTAreply%"
Set "dest=%HTAreply%"
echo robocopy "D:\MasterFolder\- Details" "E:\Sets\%dest%" /E /V /NP /R:10 /W:30
echo robocopy "D:\MasterFolder\- Details" "F:\%dest%" /E /V /NP /R:10 /W:30
pause>nul
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA GUI Form</TITLE>
<SCRIPT language="Vbscript">
Sub window_onload()
CenterWindow 200,150
Focus
End Sub
Sub CenterWindow(x,y)
window.resizeTo x, y
iLeft = window.screen.availWidth/2 - x/2
itop = window.screen.availHeight/2 - y/2
window.moveTo ileft, itop
End Sub
Function closeHTA(reply)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetStandardStream(1).WriteLine(reply)
window.close()
End Function
Sub Focus()
txt.Focus
End Sub
</SCRIPT>
</HEAD>
<BODY>
<center>
<label for="dest">Please enter destination</label>
<br>
<input type="text" id="txt">
<br><Input type="Submit" onclick="closeHTA(txt.value)" value="Submit">
</center>
</BODY>
</HTML>
Related
Okay.I want to do a little app thing, and it sounds like this:
#echo off
CLS
start https://www.youtube.com/
timeout 4
#if (#CodeSection == #Batch) #then
#echo off
CScript //nologo //E:JScript "%~F0"
rem Open the browser here
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys("{K}");
>nul
It basically starts a video, when you open the file, runs in the background and presses K (my attempt to solve the problem), and then I want the video to automaticaly start.
P.S.I want to use .bat, not any other extension or program.
So:
How can I make the tab to open, and the video to automaticaly play?
You can also do like this in powershell:
#Echo Off
start /d "C:\Program Files (x86)\Google\Chrome\Application\" chrome.exe new-tab "www.youtube.com/watch?v=BISQwNjP2NE"
powershell -command "& {$wsh = New-Object -ComObject Wscript.Shell; $wsh.AppActivate("Chrome"); Start-Sleep -Seconds 5; $wsh.Sendkeys(" "); $wsh.Sendkeys("+{F10}"); $wsh.Sendkeys("{DOWN}"); $wsh.Sendkeys("{ENTER}")}"
Exit /b
Thanks #It wasn't me for classic idea.
Well, this is some kind of duplicate question here, anyway, I suggest using vbs to get this result.
Obs.: Take a time to see the link video about SendKeys/VBS
<!-- :
#echo off && mode 50,03 && title <nul && title .\%~nx0
start /d "c:\Program Files (x86)\Google\Chrome\Application\" chrome.exe -new-tab "www.youtube.com/watch?v=BISQwNjP2NE"
%__APPDIR__%wScript.exe "%~dpnx0?.wsf" && goto :EOF
--> <job> <script language = "vbscript">
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.AppActivate "chrome"
WScript.Sleep 4000
objShell.SendKeys " " ' use space (charater) to play/pause
'objShell.SendKeys ("+{F10}") left click | in your question, the
'objShell.SendKeys "{DOWN}" left click | left click is not works
'objShell.SendKeys "{ENTER}" left click | for play the video
</script></job>
Following is the hybrid batch script:
<!-- :: Batch section
#echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%{HTAreply}%"
pause
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="closeHTA(1);">First option</button>
<button onclick="closeHTA(2);">Second option</button>
<button onclick="closeHTA(3);">Third option</button>
</BODY>
</HTML>
Output:
I obfuscated it as stated here : [Post#21]
https://www.dostips.com/forum/viewtopic.php?t=7990&start=15
but then the HTA app output isn't as before. It is as follows:
The entire batch content is displayed in the HTML window.
I want to know why this happens and secondly is there any way to obfuscate this hybrid batch script
Same happens when I try to convert the batch file to exe.
Edit
Problem solved. There was an error in html code. apologies. I would have edited to show the problem but I guess it was too trivial to do that.
I want to create a HTA that uses a batch file, when a user fills in the textboxes it will pass the data to the batch file.
My batch file is :
//batch
#ECHO OFF
CD "h:\tools\ffmpeg\bin"
set /p input="Enter input file: "
set /p output="Enter output file: "
ffmpeg.exe -i %input% %output%
I want to pass the input and output from the HTA to the batch file
set /p input="enter input"
set /p output="enter output"
%input% %output%
Pretty sure you don't need to use a batch file at all for this.
Something like this should work:
Set Shell = CreateObject("WScript.Shell")
Shell.run "h:\tools\ffmpeg\bin\ffmpeg.exe -i " & Input & " " & Output
Note - I am just guessing at the variable names (Input and Output) you are using in the HTA, so you will have to rename those to whatever the variables are that contain the inputs you originally wanted to pass through to the batch file. Also only guessing that it will be in VBScript...
EDIT:
To answer the comment more completely, something like this:
<html>
<head>
<title>HTA Test</title>
<HTA:APPLICATION
ID="objTest"
APPLICATIONNAME="HTATest"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
<SCRIPT LANGUAGE="VBScript">
Sub TestSub
Set Shell = CreateObject("WScript.Shell")
Shell.run "h:\tools\ffmpeg\bin\ffmpeg.exe -i "& InputTextbox.Value & " " & OutputTextbox.Value
End Sub
</SCRIPT>
<body>
Input : <input type="text" name="InputTextbox" size="30"><P>
Output: <input type="text" name="OutputTextbox" size="30"><P>
<input id=runbutton type="button" value="Run Script" name="run_button" onClick="TestSub">
</body>
Note for people actually trying to pass arguments to a batch file from HTA:
Assuming you've taken input from the user already you can pass it to the batch file (just like the parameters are passed to the EXE above), but you wouldn't need the set /p lines. Instead you would reference input and output as %1 and %2 in the batch file.
I’m trying to create a script to run all the tools I use working in a particular project (dor instance, gulp, phpstorm, php server, etc.)
I’d like to run shortcuts but not executable files. I can see two possible ways to achieve it in Windows:
VbScript
cmd.exe
Unfortunately, I’ve failed both:
1) WshShell fire .lnk file and I can't find an alternative way to do that:
To do so I’m trying to write .cmd file or .vbs
However, I’ve got the problem in each way.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe" ' Works alright
WshShell.Run "C:\Users\eugene\Desktop\gulp EngMe.lnk" ' got an Exception: Unknown Exception
2) .cmd script waits for closing previously started app:
#rem urls and filesystem shortcuts start alright:
"C:\Users\eugene\Desktop\8000.url"
"C:\Users\eugene\Desktop\project_folder.lnk"
#rem the following two commands are starting sequentially, when the previous one has closed.
"notepad.exe"
#rem shortcut to %windir%\system32\notepad.exe:
"C:\Users\eugene\Desktop\Notepad_.lnk"
What I'm doing wrong and is there another way to do what I'm trying?
Paths with spaces must be "quoted" when fed to .Run so:
WshShell.Run """C:\Users\eugene\Desktop\gulp EngMe.lnk"""
Will work fine.
Try using powershell for scripting.
Look at the question - Execute shortcuts like programs
Look at Mark Schill's answer using invoke-item to launch .lnk files like programs.
This is probably your best way to address your script challenges.
You can check the shortcutjs.bat which is capable to read a .lnk file properties. With this you will execute all .lnk files in the current directory in the correct working directory:
#echo off
for %%# in (*.lnk) do (
call :executeShortcut "%%#"
)
exit /b %errorlevel%
:executeShortcut - %1 - shortcut to execute
setlocal enableDelayedExpansion
for /f "tokens=1,* delims=:" %%a in ('call shortcutjs.bat -examine %1') do (
if /i "%%a" equ "Target" (
set "target=%%b"
set "target=!target:~1!"
)
if /i "%%a" equ "Working Directory" (
set "workdir=%%b"
set "workdir=!workdir:~1!"
)
)
start "%target%" /d "%workdir%" /wait /b "%target%"
exit /b %errorlevel%
I'm a Static Web Dev. & I Don't know much about CMD & Batch Files.
But I need to make a batch file that does this:It first prompts user to enter a specific word & there are some links that have URL & a some kind of name. then if user's input matches one of the link's names, The URL opens in a browser (the default one). I think it is like this with HTML+JS:
<!doctype html>
<html>
<head>
<script>
function input(){
var stackoverflow = "http://stackoverflow.com";
var val = document.getElementById("input").value;
if (val = stackoverflow){
window.open(stackoverflow);
}
}
</script>
</head>
<body>
<input type="text" id="input" onKeyUp="input()"/>
</body>
</html>
but there should be more options & I think it should be done with START command.
Any help is appreciated.THX
What you are looking for is a simple menu,
:: turn off "verbose" command writeback.
#echo off
:: write a simple list of options to console,
:main
echo Options;
echo 1 : StackOverflow
echo 2 : Google
echo 3 : Youtube
echo 4 : This question
:: Prompt for input,
set /p "strMenu=Enter desired URL number:"
:: Compare input through if commands,
:: `if not defined strMenu goto :menu` can be used here if prefered.
if "%strMenu%" equ "1" start "" "https://www.stackoverflow.com"
if "%strMenu%" equ "2" start "" "https://www.Google.com"
if "%strMenu%" equ "3" start "" "https://www.youtube.com"
if "%strMenu%" equ "4" start "" "http://stackoverflow.com/questions/35945614/opening-urls-with-user-input-in-cmd-batch-file"
exit
Note the start command will use the default assigned browser, but this can be set with something like;
start "" "chrome" "URL"
Numbers are used for laziness in having to type more than 1 character before hitting enter.