execute exe from incomplete name in .bat - windows

I wanna make a .bat for installing service, I use that
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe "ServiceName.exe"
But the "v4.0.30319" can be "v4.0.otherVersion" and I look for a way to execute anyway.
I searched a lot of but didn't find something who work and I'm really bad with bash.
Thanks.

Based upon your provided information, this idea retrieves the location you require from the registry:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "Net4RegPath=HKLM\SOFTWARE\Microsoft\Net Framework Setup\NDP\v4"
Set "InstallUtil="
For /F "EOL=HTokens=2*" %%G In (
'%__AppDir__%reg.exe Query "%Net4RegPath%" /S /F "InstallPath" /V /E
2^>NUL^|%__AppDir__%find.exe "_SZ"')Do For /F "Delims=" %%I In (
'%__AppDir__%where.exe "%%~H\.":"InstallUtil.exe" 2^>NUL'
)Do Set "InstallUtil=%%I"
If Defined InstallUtil "%InstallUtil%" "ServiceName.exe"
Please note that this will not currently retrieve the appropriate file path if you're wanting to specifically identify the location of the x86 InstallUtil.exe file on an x64 OS. But as that is technically outside of the scope of your question at the time of answering, I'll leave that for you to implement if required.

Related

Syntax/Script to only select files created %date% (today) for batch file?

Created a batch file to go into our AutoCAD '.scr' files in a specific directory and that were created today. Im not sure if to do what i need, if i have to use FORFILES instead. The code works but not for files created today.
Ive tried making arguments such as
(%~ti :: FindSTR %DATE%)
and as suggested in the comments:
ECHO %~ti |FindSTR %DATE% && (echo today) || (echo other day)
to attempt too specify the date for the FOR command when looking for files to open and look for .open to replace as OPEN Y, but im not sure of the syntax OR where i can add it to the script.
The script seems to be going into all the files and saving even if the text DOESNT need to be changed, however changing it to only alter files that were created or last modified today would prevent that from happening.
#echo off
setlocal enableextensions disabledelayedexpansion
FOR /R "V:\PPY005\CSC\Script Maker" %%S in (*.scr) DO (
for /f "delims=" %%i in ('type "%%S" ^& break ^> "%%S" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%%S" echo(!line:.open=open y!
endlocal
)
)
I need it to only apply to files created today as it would create some confusion as we manually load these and it would be confusing if one would load that was dated in the title such as 9-10-IMP-EM.scr and appear on top as if it was created/modified for today.
I need the directory listed as this batch will not be in the root directory of the files being changed and the script is looking for files in that directory of that specific file type, we prefer putting scripts in a special directory and just referencing them from that networked location such as opening a linked location from a document or using a batch file that will have a menu to select and run specific batch files referenced in (dont need help with that, i know how to do that)
I am kinda pulling out my hair with this as I cannot find documentation online on how to do this with the FOR command. Im a beginner so to say at Batch files, ive written several at this point but im not sure how to do this.
I understand that this MIGHT be easier to do via Powershell, but the company I work for doesnt allow us to use powershell for security reasons.
You can use the FORFILES command to select files based on the date. You can then parse the output of the FORFILES command with a FOR /F command that then nests right into your original code.
#echo off
setlocal enableextensions disabledelayedexpansion
FOR /F "delims=" %%G IN (
'FORFILES /P "V:\PPY005\CSC\Script Maker" /M *.scr /D +0 /C "cmd /c echo #path"'
) DO (
for /f "delims=" %%H in ('type "%%~G" ^& break ^> "%%~G" ') do (
set "line=%%H"
setlocal enabledelayedexpansion
>>"%%~G" echo(!line:.open=open y!
endlocal
)
)

Assign content of DIR to a variable

I wrote the following cmd
DIR /B "%appdata%\Mozilla\Firefox\Profiles"
which returns the following single folder which exists at that dir location
4jktnrk2.default
I wish to store 4jktnrk2.default in a variable.
I tried the following
SET A=DIR /B "%appdata%\Mozilla\Firefox\Profiles"
and
SET A="DIR /B %appdata%\Mozilla\Firefox\Profiles"
However neither of these work.
I also think its best to not use a FOR loop as there is only One folder in that directory.
#echo off
setlocal enableDelayedExpansion
set "dir_c="
for /f "delims=" %%a in ('DIR /B "%appdata%\Mozilla\Firefox\Profiles"') do (
if "!dir_c!" equ "" (
set "dir_c=%%~a"
) else (
set "dir_c=!dir_c!;%%~a"
)
)
echo %dir_c%
You have no other option but to use FOR /F.There's no unix style for assigning the result of command to variable in batch.
Excuse me. I think that the real goal of this request is to "Get a single folder in a variable". The code segment below do that:
for /D %%a in ("%appdata%\Mozilla\Firefox\Profiles\*") do set "A=%%a"
The natural way of process folders in a Batch file is via for /D command (the same way than the natural way to process files is via plain for command). The for /F ... in ('dir /B ... command is a less efficient method that is used just when for or for /D commands don't provide the required results.
You really should use FOR /D or FOR /F as Aacini and npocmaka have suggested.
If you insist that you don't want to use FOR, then you will have to resort to a temporary file:
dir /b "%appdata%\Mozilla\Firefox\Profiles" >folder.tmp
set /p "a=" <folder.tmp
del folder.tmp

Windows Batch: getting Path without Shortening

I searched now for several Hours and didnt find any fitting solution for me.
When I try to get the current Path of the Batch File, I use normaly %~dp0.
This will leads in Paths like: D:\VM\TUTORI~2\STARTS~1.BAT
However this is not an issue, if only doing File Management, but I want to use the Script to call Vagrant commands.
It happens that the Vagrant System doesnt like this shortened Paths.
As soon as the Path is short enough or when I call it from the CMD directly, it works just fine.
I'm now searching for a way to get any unshortened Path to my working Directory.
But whatever I tried so far didnt work and google only delivers me how people Shorten their Paths.
Is there a practical Solution to this Problem?
I want to set a Variable in my Script looking like this:
D:\VM\tutorialtest
EDIT:
According to the Accepted Answer the working snippet for me is the Snippet without the "-2" on the 2. For Loop. This will give me the Path no matter where the folder is.
setlocal EnableDelayedExpansion
set myPath=%~DP0
set myPath=%myPath:*\=%
set fullPath=
pushd \
for %%a in ("%myPath:\=" "%") do (
set thisDir=%%~a
for /D %%d in ("!thisDir:~0!*") do (
set fullPath=!fullPath!\%%d
cd %%d
)
)
popd
echo Full path: %~D0%fullPath%
Result:
D:\VM\tutorialtest
Try this:
#echo off
setlocal EnableDelayedExpansion
set "myPath=%~DP0"
set "myPath=%myPath:*\=%"
set "fullPath="
pushd \
for %%a in ("%myPath:\=" "%") do (
set "thisDir=%%~a"
for /D %%d in ("!thisDir:~0,-2!*") do (
set "fullPath=!fullPath!\%%d"
cd "%%d"
)
)
popd
echo Full path: %~D0%fullPath%
Post the result, please.
EDIT: Bug fixed
The problem is that names that have less than 2 characters are cutted! (my mistake).
I did some testing and it seems that for /D %%d in ("TUTORI~2*") ... also returns the full name of the folder, so thisDir variable is not required. You may modify the for %%a loop this way and get the same result:
for %%a in ("%myPath:\=" "%") do (
for /D %%d in ("%%~a*") do (
set fullPath=!fullPath!\%%d
cd %%d
)
)
Edit - Corrected the code which can work with path with or without spaces.
for /f "delims=" %i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%i
set mypath=%mypath: Directory of =%
Above codes will work if you type them directly in command console, if you want to use it in batch file use it as below.
for /f "delims=" %%i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%%i
set mypath=%mypath: Directory of =%
I believe you are in need of getting the current working directory(something like pwd in Unix). 'dir' command usually returns the current path details along with variuos other details. We just pick the line with the directory name (line with the tag "Directory of") and then in the second line we remove the tag "Directory of" (replacing it with none).
Refer the below link for more string manipulation techniques.
http://www.dostips.com/DtTipsStringManipulation.php
Sample output - typed on command console
C:\test\new folder>for /f "delims=" %i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%i
C:\test\new folder>set mypath= Directory of C:\test\new folder
C:\test\new folder>set mypath=%mypath: Directory of =%
C:\test\new folder>echo.%mypath%
Did you try :
set $path=%cd%
echo %$path%
If this behaviour occurs when you open a cmd window, then type echo %comspec% at the cmd prompt and see what it returns: if it has command.com as part of it then it has been altered.
Another possibility is that the shortcut you are using to open a cmd window has command.com in the properties to launch it.
If you are using a shortcut then try to open a cmd prompt from the Win+R hotkey and type cmd and enter. See if this behaviour has changed but also check the first point above once more.

Batch File - FOR LOOP

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.
C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe
In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.
cd C:\FOLDER_ABC\
dir application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt
Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?
cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;
:FINISH
ECHO THE END
exit
Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.
cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G
you can try:
FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"
I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.
There is no need for a temporary file. Your code can be greatly simplified with FOR:
pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"
:FINISH
ECHO THE END
exit /b
Try using the START command:
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;
There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:
FOR /D %%i in (application-v*) DO START %i\%i.exe
UPDATE
From comments:
My only issue, which I just realized, is that the application folder and application name are not always identical.
In that case, you could try something like:
for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

Force passing long file names to dos for command

I have problem with following simple dos command:
FOR %%f IN (.\07_PROCEDURES\DataSources\*.sql) DO echo "%%f"
It does some action for every file with sql extension. Everything works fine, except long names. When directory contains files, with sql_ extensions, they are picked up too. But this behavior depend on whether 8.3 files are turned on or off on file system. If they are turned on (default choice on most computers) sql_ are passed, because extension is cropped.
How to force for fetch long file names ? Thanks !
P.S. Do not offer powershell upgrade.
You can try to examine the extension in the loop itself
for %%f in (*.sql) do (
if /i "%%~xf" EQU ".sql" echo %%f
)
You can try something like this:
for /f "usebackq delims=" %%f in (`dir /b .\*.sql ^| findstr /r .*\.sql$`) do #echo "%%f"

Resources