I have a batch file like this
#echo off
::::::::::::::::::::::::::::::::::::::::::::
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' (goto gotPrivileges) else (goto getPrivileges)
:getPrivileges
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
echo If strArg = WScript.Arguments.Item^(0^) Then d = Left^(strArg, InStrRev^(strArg,"\"^) - 1^) >> "%temp%\getadmin.vbs"
echo args = args ^& " " ^& strArg >> "%temp%\getadmin.vbs"
echo Next >> "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", ^("/c start /D """ ^& d ^& """ /B" ^& args ^& " ^& exit"^), , "runas", 4 >> "%temp%\getadmin.vbs"
cscript "%temp%\getadmin.vbs" ""%~s0"" %*
del /q "%temp%\getadmin.vbs"
exit /b
:gotPrivileges
:: Your code here
echo "Downloading old version of OrthoSelect..."
curl -o ./6.7.5.zip "https://github.com/ruellm/OrthoSelect_6.7.5/archive/refs/tags/6.7.5.zip"
pause
It ask for an admin permission and executes curl to download the file, but unfortunately, even if the admin rights is given, the zip file downloaded is only 1Kb, the entire zip file is not downloaded properly,
The console window do not show error as well.
Anyone can help whats wrong?
Links to files on Github are not direct links, so if you try to simply curl the file, you'll end up only downloading the redirect page instead. If you use a text editor to open the file that you've downloaded, you'll see
<html><body>You are being redirected.</body></html>
This should tell you that you need to use the -L flag to follow redirects:
curl -L "https://github.com/ruellm/OrthoSelect_6.7.5/archive/refs/tags/6.7.5.zip" -o 6.7.5.zip
The script that I am using is from here: https://stackoverflow.com/a/12264592/1016343
I have a batch file that is made for installing Office 365 using their click-to-run installer. It will first check if the directory that contains the XML configurations and setup.exe exists. Once that is confirmed, it will begin Elevate.cmd - Version 4. The only modifications I have made were removing the following lines:
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
REM Run shell as admin (example) - put here code as you like
ECHO %batchName% Arguments: P1=%1 P2=%2 P3=%3 P4=%4 P5=%5 P6=%6 P7=%7 P8=%8 P9=%9
cmd /k
My code is supposed to start after these lines:
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Whenever I run the batch file, I get a UAC prompt. After I accept the prompt, it closes. To confirm that my code is not executed, I added #echo HELLO! > hello.txt right before call setup.exe %config%, but I could not find the file in the directory. It was not in C:\Windows\System32 either.
Here is my batch file, Install.bat:
#echo off
set directory="<PATH EXCLUDED FOR PRIVACY. ALSO ON A NETWORK DRIVE.>"
if not exist %directory% goto :install_not_found
::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
#echo off
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
if '%cmdInvoke%'=='1' goto InvokeCmd
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation
:InvokeCmd
ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
:ExecElevation
"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
:: https://www.computerhope.com/forum/index.php?topic=75310.0
:choice
#echo Enter the number for which install you would like:
#echo [1] Office 32-Bit
#echo [c] Cancel
goto :listen
:listen
set /P c=^>
if /I "%c%" EQU "1" (
set config="/configure office_32.xml"
goto: install
)
if /I "%c%" EQU "C" goto :cancel
if /I "%c%" EQU "c" goto :cancel
#echo Invalid entry
goto :listen
:install_not_found
#echo Error: The installation folder could not be found.
goto :cancel
:cancel
#echo Cancelling Installation...
goto :eof
:install
pushd %directory%
elevate
#echo on
call setup.exe %config%
#echo off
popd
#pause
I have used this batch script to create shortcut of an .exe but I want to get the .exe name also dynamically. In a folder I have batch file and also one .exe file I want to get that .exe name dynamically in my batch file.
#echo off
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player111.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%CD%\Pacman.exe" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%
I want to set oLink.TargetPath dynamically. It will be the path where the batch file located along with the .exe name. How can I get Pacman.exe as mentioned in above code dynamically? Remember, exactly one .exe is in the folder.
To retrieve the full path to the .exe located in the parent directory of the batch file, use the following code:
for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"
(Note: To use the current working directory instead, replace %~dp0*.exe by %CD%\*.exe.)
Finally, replace the line echo oLink.TargetPath = "%CD%\Pacman.exe" >> %SCRIPT% by:
echo oLink.TargetPath = "%EXEFILE%" >> %SCRIPT%
Here is the full script -- also with some other improvements:
#echo off
set "SCRIPT=%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
for %%F in ("%~dp0*.exe") do set "EXEFILE=%%~fF"
> "%SCRIPT%" (
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
echo sLinkFile = "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Player111.lnk"
echo Set oLink = oWS.CreateShortcut^(sLinkFile^)
echo oLink.TargetPath = "%EXEFILE%"
echo oLink.Save
)
cscript //NoLogo "%SCRIPT%"
del "%SCRIPT%"
to get a filename to a variable (assuming there is only one match):
for %%a in ("%~dp0*.exe") do set "file=%%a"
echo %file%
and later
echo oLink.TargetPath = "%file%" >> %SCRIPT%
I want my .bat script (test.bat) to create a shortcut to itself so that I can copy it to my windows 8 Startup folder.
I have written this line of code to copy the file but I haven't yet found a way to create the said shortcut, as you can see it only copies the script.
xcopy "C:\Users\Gabriel\Desktop\test.bat" "C:\Users\Gabriel\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
Can you help me out?
You could use a PowerShell command. Stick this in your batch script and it'll create a shortcut to %~f0 in %userprofile%\Start Menu\Programs\Startup:
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\%~n0.lnk');$s.TargetPath='%~f0';$s.Save()"
If you prefer not to use PowerShell, you could use mklink to make a symbolic link. Syntax:
mklink saveShortcutAs targetOfShortcut
See mklink /? in a console window for full syntax, and this web page for further information.
In your batch script, do:
mklink "%userprofile%\Start Menu\Programs\Startup\%~nx0" "%~f0"
The shortcut created isn't a traditional .lnk file, but it should work the same nevertheless. Be advised that this will only work if the .bat file is run from the same drive as your startup folder. Also, apparently admin rights are required to create symbolic links.
Cannot be done with pure batch.Check the shortcutJS.bat - it is a jscript/bat hybrid and should be used with .bat extension:
call shortcutJS.bat -linkfile "%~n0.lnk" -target "%~f0" -linkarguments "some arguments"
With -help you can check the other options (you can set icon , admin permissions and etc.)
Rohit Sahu's answer worked best for me in Windows 10. The PowerShell solution ran, but no shortcut appeared. The JScript solution gave me syntax errors. I didn't try mklink, since I didn't want to mess with permissions.
I wanted the shortcut to appear on the desktop.
But I also needed to set the icon, the description, and the working directory.
Note that MyApp48.bmp is a 48x48 pixel image.
Here's my mod of Rohit's solution:
#echo off
cd c:\MyApp
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%userprofile%\Desktop\MyApp.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\MyApp\MyApp.bat" >> CreateShortcut.vbs
echo oLink.WorkingDirectory = "C:\MyApp" >> CreateShortcut.vbs
echo oLink.Description = "My Application" >> CreateShortcut.vbs
echo oLink.IconLocation = "C:\MyApp\MyApp48.bmp" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs
The best way is to run this batch file.
open notepad and type:-
#echo off
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "GIVETHEPATHOFLINK.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "GIVETHEPATHOFTARGETFILEYOUWANTTHESHORTCUT" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs
Save as filename.bat(be careful while saving select all file types)
worked well in win XP.
Nirsoft's NirCMD can create shortcuts from a command line, too. (Along with a pile of other functions.) Free and available here:
http://www.nirsoft.net/utils/nircmd.html
Full instructions here:
http://www.nirsoft.net/utils/nircmd2.html#using (Scroll down to the "shortcut" section.)
Yes, using nircmd does mean you are using another 3rd-party .exe, but it can do some functions not in (most of) the above solutions (e.g., pick a icon # in a dll with multiple icons, assign a hot-key, and set the shortcut target to be minimized or maximized).
Though it appears that the shortcutjs.bat solution above can do most of that, too, but you'll need to dig more to find how to properly assign those settings. Nircmd is probably simpler.
link.vbs
set fs = CreateObject("Scripting.FileSystemObject")
set ws = WScript.CreateObject("WScript.Shell")
set arg = Wscript.Arguments
linkFile = arg(0)
set link = ws.CreateShortcut(linkFile)
link.TargetPath = fs.BuildPath(ws.CurrentDirectory, arg(1))
link.Save
command
C:\dir>link.vbs ..\shortcut.txt.lnk target.txt
To create a shortcut for warp-cli.exe, I based rojo's Powershell command and added WorkingDirectory, Arguments, IconLocation and minimized WindowStyle attribute to it.
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Start Menu\Programs\Startup\CWarp_DoH.lnk');$s.TargetPath='E:\Program\CloudflareWARP\warp-cli.exe';$s.Arguments='connect';$s.IconLocation='E:\Program\CloudflareWARP\Cloudflare WARP.exe';$s.WorkingDirectory='E:\Program\CloudflareWARP';$s.WindowStyle=7;$s.Save()"
Other PS attributes for CreateShortcut: https://stackoverflow.com/a/57547816/4127357
I present a small hybrid script [BAT/VBS] to create a desktop shortcut.
And you can of course modifie it to your purpose.
#echo off
mode con cols=87 lines=5 & color 9B
Title Shortcut Creator for your batch and applications files by Hackoo 2015
Set MyFile=%~f0
Set ShorcutName=HackooTest
(
echo Call Shortcut("%MyFile%","%ShorcutName%"^)
echo ^'**********************************************************************************************^)
echo Sub Shortcut(ApplicationPath,Nom^)
echo Dim objShell,DesktopPath,objShortCut,MyTab
echo Set objShell = CreateObject("WScript.Shell"^)
echo MyTab = Split(ApplicationPath,"\"^)
echo If Nom = "" Then
echo Nom = MyTab(UBound(MyTab^)^)
echo End if
echo DesktopPath = objShell.SpecialFolders("Desktop"^)
echo Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& Nom ^& ".lnk"^)
echo objShortCut.TargetPath = Dblquote(ApplicationPath^)
echo ObjShortCut.IconLocation = "Winver.exe,0"
echo objShortCut.Save
echo End Sub
echo ^'**********************************************************************************************
echo ^'Fonction pour ajouter les doubles quotes dans une variable
echo Function DblQuote(Str^)
echo DblQuote = Chr(34^) ^& Str ^& Chr(34^)
echo End Function
echo ^'**********************************************************************************************
) > Shortcutme.vbs
Start /Wait Shortcutme.vbs
Del Shortcutme.vbs
::***************************************Main Batch*******************************************
cls
echo Done and your main batch goes here !
echo i am a test
Pause > Nul
::********************************************************************************************
I would like to propose different solution which wasn't mentioned here which is using .URL files:
set SHRT_LOCA=%userprofile%\Desktop\new_shortcut2.url
set SHRT_DEST=C:\Windows\write.exe
echo [InternetShortcut]> %SHRT_LOCA%
echo URL=file:///%SHRT_DEST%>> %SHRT_LOCA%
echo IconFile=%SHRT_DEST%>> %SHRT_LOCA%
echo IconIndex=^0>> %SHRT_LOCA%
Notes:
By default .url files are intended to open web pages but they are working fine for any properly constructed URI
Microsoft Windows does not display the .url file extension even if "Hide extensions for known file types" option in Windows Explorer is disabled
IconFile and IconIndex are optional
For reference you can check An Unofficial Guide to the URL File Format of Edward Blake
I created a VB script and run it either from command line or from a Java process.
I also tried to catch errors when creating the shortcut so I can have a better error handling.
Set oWS = WScript.CreateObject("WScript.Shell")
shortcutLocation = Wscript.Arguments(0)
'error handle shortcut creation
On Error Resume Next
Set oLink = oWS.CreateShortcut(shortcutLocation)
If Err Then WScript.Quit Err.Number
'error handle setting shortcut target
On Error Resume Next
oLink.TargetPath = Wscript.Arguments(1)
If Err Then WScript.Quit Err.Number
'error handle setting start in property
On Error Resume Next
oLink.WorkingDirectory = Wscript.Arguments(2)
If Err Then WScript.Quit Err.Number
'error handle saving shortcut
On Error Resume Next
oLink.Save
If Err Then WScript.Quit Err.Number
I run the script with the following commmand:
cscript /b script.vbs shortcutFuturePath targetPath startInProperty
It is possible to have it working even without setting the 'Start in' property in some cases.
Based on Rohit's answer, I created this batch script which accepts the input parameters: AppPath, AppName, AppExtension and ShortcutDestinationPath.
MakeShortcut.bat:
#echo off
set AppPath=%~1
set AppName=%~2
set AppExtension=%~3
set ShortcutDestinationPath=%~4
cd %AppPath%
echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%ShortcutDestinationPath%\%AppName%.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "%AppPath%\%AppName%.%AppExtension%" >> CreateShortcut.vbs
echo oLink.WorkingDirectory = "%AppPath%" >> CreateShortcut.vbs
echo oLink.Description = "%AppName%" >> CreateShortcut.vbs
echo oLink.IconLocation = "%AppPath%\%AppName%.bmp" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
rem del CreateShortcut.vbs
Example usage to create a shortcut to C:\Apps\MyApp.exe in the folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp:
MakeShortcut.bat "C:\Apps" "MyApp" "exe" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp"
I am trying to make a batch script that makes a config file for each file i a specified folder.
After it has created the config files then it executes a program that uses a selected config file then converts the specified file in the config.
here is my current code:
:start
#echo off
cls
cd /d %~dp0
goto source
rem This sets up the working folder. I usually do this because I need to but I am
rem not sure about it this time...
:source
echo Please select the folder with the ARF files in it.
set /p source=E.G. C:\Users\Elliot\Desktop\toconvert:
goto dest
rem This sets the folder location for the ARF files.
rem I am going to add a menu for simplicity sake later.
:dest
echo Please select where the mp4 files should appear.
set /p dest=E.G. C:\Users\Elliot\Desktop\Converted:
goto cfgname
rem This sets the output folder for the converted files.
rem Again I will make a menu in the future.
:cfgname
goto createcfg
rem this sets the name of the CFG that will be created for the ARF file
:createcfg
echo [Console] >> %MP4%.cfg
echo inputfile=%source%\1.arf >> MP4.cfg
echo media=MP4 >> MP4.cfg
echo showui=1 >> MP4.cfg
echo [UI] >> MP4.cfg
echo chat=1 >> MP4.cfg
echo qa=0 >> MP4.cfg
echo largeroutline=1 >> MP4.cfg
echo [MP4] >> MP4.cfg
echo outputfile=%dest%\1.mp4 >> MP4.cfg
echo width=1024 >> MP4.cfg
echo height=768 >> MP4.cfg
echo framerate=10 >> MP4.cfg
rem This makes the config file that is required by the player to convert
:convert
cd C:\programdata\webex\webex\500
nbrplay.exe -Convert "%cfg%"
:end
echo Thanks for using Elliot Labs Auto Converter.
echo for feature requests please email: elliot-labs#live.com
pause | echo Press any key to exit...
exit
:preloop
set num=0
goto loop
:loop
cls
echo %num%
pause
set /a num+=1
if %num%==6 (goto end) ELSE goto loop
This only works for one file. How do I make it apply to a whole folder.
#echo off &setlocal
for %%a in ("%cd%\*.arf") do call:MakeCFG "%%~a"
goto:eof
:MakeCFG
setlocal
set "MP4=%~n1"
set "source=%~dp1"
set "filename=%~n1"
set "dest=X:\destination"
(
ECHO([Console]
ECHO(inputfile=%source%%filename%
ECHO(media=MP4
ECHO(showui=1
ECHO([UI]
ECHO(chat=1
ECHO(qa=0
ECHO(largeroutline=1
ECHO([MP4]
ECHO(outputfile=%dest%\%filename%.mp4
ECHO(width=1024
ECHO(height=768
ECHO(framerate=10
)>"%MP4%.cfg"
exit /b 0