Stack community good day! Thank you in advance for your time
I would like to create a bat file in order to autocreate an iso file from the DVD drive. So the logic will be:
Find which is the CD/DVD drive (from many drives)
And use that result as a variable (of the drive: for example F:) which will be executed in the following command:
cdbxpcmd.exe --burn-data -folder:F:\ -iso:C:\donalds.iso -format:iso
So in the previous command, the F:\ will be the variable, lets say %input%:\ which the program cdbxpcmd will use in order to create an iso from that drive.
I have found the following script that finds the drive letter,
from here: https://itectec.com/superuser/windows-how-to-detect-dvd-drive-letter-via-batch-file-in-ms-windows-7/
#echo off
setlocal
for /f "skip=1 tokens=1,2" %%i in ('wmic logicaldisk get caption^, drivetype') do (
if [%%j]==[5] echo %%i
)
endlocal
Do you believe that we could combine them? And how? Any suggestions?
You could use cdbxpcmd.exe itself to locate your drive:
Two line batch-file example:
#Set "CDBXP=C:\Program Files\CDBurnerXP\cdbxpcmd.exe"
#For /F "Tokens=2 Delims=()" %%G In ('^""%CDBXP%" --list-drives^"') Do #"%CDBXP%" --burn-data -folder:%%G -iso:"C:\donalds.iso" -format:iso
Just change the location where you have your cdbxpcmd.exe command line utility between the = and the closing " on line 1.
Alternatively, you could still use WMI, but personally, I would not use Win32_LogicalDisk, I would instead use Win32_CDROMDrive, which could verify both that a CDROM disk is loaded, and that it contains readable data.
Single line batch-file example:
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path Win32_CDROMDrive Where "MediaLoaded='True' And DriveIntegrity='True'" Get Drive /Format:MOF 2^>NUL') Do #"C:\Program Files\CDBurnerXP\cdbxpcmd.exe" --burn-data -folder:%%G\ -iso:"C:\donalds.iso" -format:iso
Just change the location where you have your cdbxpcmd.exe command line utility, remembering to leave the doublequotes in place for best practice.
Related
I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"
Is there a way to list all executables available with your current PATH (environment variable) setting with a batch file?
I've been trying for a while and I can't seem to find a way (i ain't no batch intellectual).
Please help if you can even a link to an article can be useful.
Thank you!
surprisingly simple:
for %a in (%pathext%) do where *%a
the %pathext% lists all defined file extension that are executable (.exe, .bat, ...) and where lists all findings within the %path%
Note: this is command line syntax. For use in a batch file, use %%a instead of %a
As an addition to the already provided answer, and as a result of #aschipfl's comment, the following should use only the locations defined in the %Path% environment variable, and not include the current directory, (unless that itself was in %Path%).
From the Command Prompt:
For %A In (%PathExt%) Do #Where $Path:*%A 2>Nul
And from a batch file:
#For %%A In (%PathExt%) Do #Where $Path:*%%A 2>Nul
Obviously if you were only looking for .exe files as opposed to executables it would be much simpler:
Where $Path:*.exe 2>Nul
An alternative is to solely use CMD's native facilities without relying on external executables, which can also be used on older versions of windows like Windows XP where Where.exe is not available.
In a batch file:
#echo off
setlocal DisableDelayedExpansion
for %%D in ("%PATH:;=";"%") do (
for /F "eol=* delims=" %%F in ('cd /d "%%~D" 2^>nul ^&^& dir /b/a-d %PATHEXT:.=*.% 2^>nul') do (
for %%F in ("%%~D\%%F") do echo %%~fF
)
)
Technically, The innermost FOR is not vital, It is just to handle the anomalies that sometimes could be seen in the PATH environment variable, like where some path elements have trailing backslash, to output file paths in the standard format.
Because of it's length, Its not suited well for using at Command Prompt, however with my tests and to my sureprise, it is faster than the equivalent solution with Where.
One-liner for use at Command Prompt (For the sake of completeness):
#for %D in ("%PATH:;=";"%") do #for /F "eol=* delims=" %F in ('cd /d "%~D" 2^>nul ^&^& dir /b/a-d %PATHEXT:.=*.% 2^>nul') do #for %F in ("%~D\%F") do #echo %~fF
I am trying to get the file version of all the files inside a folder which I managed to do (but not in a good way) but now I want to stick the folder name along with the version so I would know which version is for which folder.
I am not very good in command line and only use it for some small tasks whenever I need it so my apology in advance..
Here is what I have done:
For /d %%a in (C:\high\low\*) Do (For /d %%* in (%%a) Do wmic datafile where name="%%~da\\high\\low\\%%~nx*\\bin\\Services.dll" get Version /value)
and I get output as:
`Version=2.2.0.1 Version=2.2.0.4 Version=2.2.0.4....Version=2.2.0.4
there are 20 folders under C:\high\low and I want to go into the bin directory of each sub folder so I can see which folder has been upgraded and which one is not.
Edit
There are more than 20 folders and structure is like this:
C:\high\low\office.Services.Bolton\bin\Services.dll
C:\high\low\office.Services.Slough\bin\Services.dll
C:\high\low\office.Services.Hull\bin\Services.dll
.
.
.
C:\high\low\office.Services.Cosham\bin\Services.dll
I want to check the version number of Services.dll and need the output as:
Bolton - 2.2.0.1
Slough - 2.3.0.1
Hull - 2.5.0.1
.
.
.
Cosham - 2.0.0.0
Thanks in advance..
Instead of stacking for /d you could do a dir /b/s to find all Services.dll and parse the nasty (cr,cr,lf) output of wmic with a for /f:
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
For /F "tokens=*" %%A in (
'Dir /B/S C:\high\low\Services.dll ^|findstr /i "bin\\Services.dll$"'
) Do (
Set "DLL=%%~fA"
Set "DLL=!DLL:\=\\!"
For /F "tokens=1* delims==" %%B in (
'wmic datafile where name^="!DLL!" get Version /value^|findstr Version'
) Do For /f "delims=" %%D in ("%%C") Do Echo Version: %%D %%~fA
)
The first For /f parses the output of the commamd inside the '' iterating through each line passed in %%A (I prefer upper case variables to better distinguish between lower case~ modifiers.
Since Dir will allow wildcards only in the last element I can't do a Dir /B/S C:\high\low\*\bin\Services.dll
To ashure I get only Services.dll in a bin folder I pipe dir output to findstr /i "bin\\Services.dll$ (findstr uses by default a limited RegEx so the \ has to be escaped with another one, the $ anchors the expression at the end of the line).
The wmic command needs the backslashes in the path also escaped what is possible with string substitution (works only with normal variables)
In a (code block) we need delayed expansion to get actual values for variables changed in the code block, so ! instead of % for framing the variable names
The 2nd For /f parses wmic output splitting at the equal sign, assigning content to var %%C
EDIT added another for /f to remove the wmic cr
So I'm making an installer which installs in a specific file that would be located in one of multiple drives, and the file could be in any drive (C:\, E:\, D:\, etc.). So for example: I want to install ZK47 in E:\KNX\44C, but I want the system to automatically find the file KNX without going to E:\ and without user input.
look in all harddisks (Mediatype=12) if the folder exists:
for /f "tokens=2 delims==:" %%i in ('wmic logicaldisk where mediatype^=12 get caption /value') do if exist %%i:\KNX\44C echo found on drive: %%i:
for /f %%i in ('command') do ... is a common way to process the output of a command.
wmic logicaldisk get caption lists all existing drive letters.
where mediatype=12 tells it to list only harddisks (inside the for construct you have to escape the = with a caret ^).
/value defines the output format.
"normally" you would use "tokens=2 delims==" to get the string after = (C:). By using : as additional delimiter I avoid dealing with the ugly line endings of wmic.
So, I've been working on a batch file to collect specific system information, I've run into a road block with opening an INI file that's on the network installation. So obtaining the path is:
for /f "tokens=2*" %%a in ('REG Query "HKCU\SOFTWARE\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
for /f "tokens=2*" %%a in ('REG Query "HKLM\SOFTWARE\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
for /f "tokens=2*" %%a in ('REG Query "HKLM\SOFTWARE\Wow6432Node\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
cd %StyleDir%
So in this scenario, let's say %StyleDir% is //Server/StyleDir/
Later on in the script we read the Style.ini file with the following:
for /f "tokens=2 delims==" %%a in ('findstr SQLiteHome style.ini') do set SQLiteHome=%%a
for /f "tokens=2 delims==" %%a in ('findstr Server style.ini') do set SQL=%%a
for /f "tokens=2 delims==" %%a in ('findstr DataHome style.ini') do set DataHome=%%a
At this point I get an error saying we're unable to read the Style.ini. Within the Style.ini I have the following:
[Default]
DataHome=C:\ProgramData\Zephyr\Data\
SQLiteHome=C:\ProgramData\Zephyr\Data\
[DataBaseList]
Tons of other lines I don't need to read Right now....
Later I populate a txt file that records the information. That script is as follows:
::Output
echo StyleDir: %StyleDir% >> SystemInformation.txt
echo SQLiteHome: %SQLiteHome% >> SystemInformation.txt
echo SQL Server: %SQL% >> SystemInformation.txt
echo DataHome: %DataHome% >> SystemInformation.txt
So is there a special way that I could get this info recorded from the INI file? I've had thoughts about temporarily mapping a network drive, but the problem with that is knowing what network drives are already mapped so that I don't break what's already there. I'm not even 100% sure that this has to do with the UNC path at all, I just know that when the INI is locally on C:\ that it can be read, but on the network it cannot. Any suggestions for what to try?
Another thing I've noticed is that I can open the Style.ini from a batch file just fine, regardless of the location. I just can't Read it for some reason.
You said:
I just know that when the INI is locally on C:\ that it can be read,
but on the network it cannot.
That's not true. You can read ini files with UNCs like this:
\\ServerName\directory\any.ini
The error may be somewhere else, such as unmatched quotes, authentication or missing file. Knowing what the exact error message you get would help debug the precise reason.
Expanded in response to complete error msg:
CMD does not support UNC paths
Implies removing this line
cd %StyleDir%
in your batch file as you cannot cd unless you map to a drive letter first. Consult map /help for details. Or you can avoid cd'ing to that folder by fixing the findstr command to use the UNC directly, such as:
findstr stringToSearch \\full\UNC\path\to\file.ini
Which option you choose will depend on what is being done to the found strings. You mention you are populating those strings, but not where. If populating to a file on the remote server, use the drive map option. If populating locally, then use the UNC option.
I figured it out, all you have to do is use
pushd \\server\dir
instead of
cd \\server\dir
when pointing to the path. Figures it would be something easy. I still get an error, but it'll proceed past it, which is fine by me :-)