I need to realize the following algorithm in a Windows 10 batch file.
Loop through a directory and search for files.
For each file found, get its manufacturer's name.
If the manufacturer's name is for example "Microsoft", delete the file.
I've already made some tries with a FOR loop and the WMIC DATAFILE without any success; I just can't get the manufacturer's name into a variable for comparison.
I use the following code to loop through all the files (and directories):
pushd C:\<path>\Temp
for /f "tokens=*" %%F in ('dir /a /b') do (<something_with_%%F>)
popd
Then, I use the following code to get a file's manufacturer name:
wmic datafile where name="C:\\<path>\\Temp\\<file_name>.<ext>" get manufacturer
This preceding code will output (if used with the "echo" command) the following (if the target file belongs to Microsoft):
Manufacturer
Microsoft
Now, the problems begin when I try to use those two pieces of code together to realize the algorithm (pseudo-code) described above. Unless there's another way to do this?
There is no need to piece those two codes together, there is functionality buit in to the WMIC command.
For Example:
For /F "Skip=1 Delims=" %%A In ('
"WMIC DataFile Where (Drive='C:' And Path='\\<path>\\Temp\\' And Not Manufacturer Is Null) Get FileName, Manufacturer, Version"
') Do For /F "Delims=" %%B In ("%%A") Do Echo(%%B
You will obviously need to replace \\<path>\\Temp\\ with your true path, (must begin and end with \\).
You could even target only exe files by adding And Extension='exe' to your Where block.
Related
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.
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"
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.
We have around 200-300 assemblies and exe's which includes 3rd party assemblies in Binrel folder and we have to digitally sign these assemblies and exe's that are in BinRel folder which are not following any naming pattern. We are restricted not to use post build event of the CSproj files.
So we need a script either in batch or VB which reads the assembly Vendor and basing on the vendor, we are expecting to digitally sign the files. Is that possible to read the vendor information of a dll????
Change root_dir to the folder you want to examine.
#echo off
set "root_dir=C:\some_bins"
setlocal enableDelayedExpansion
for %%# in ("%root_dir%\*") do (
set "file_path=%%~f#"
set "file_path_q=!file_path:\=\\!"
for /f "delims=" %%f in ('"WMIC DATAFILE WHERE name='!file_path_q!' get Manufacturer /format:Textvaluelist"') do (
for /f "tokens=2 delims==" %%M in ("%%f") do (
echo !file_path! vendor : %%M
)
)
)
endlocal
note: This uses the same source that npocmaka uses in its answer (faster than me this time), but the retrieval process is different
#echo off
setlocal enableextensions disabledelayedexpansion
rem Where to get the files
set "binFolder=c:\windows"
rem Retrieve input folder elements that we will use in the query
for %%a in ("%binFolder%\") do (set "binPath=%%~pa" & set "binDrive=%%~da")
rem Retrieve the required data from the source files
for /f "skip=2 tokens=2,3 delims=," %%a in ('
wmic datafile where (
drive^="%binDrive%"
and path^="%binPath:\=\\%"
and extension^="dll"
^) get caption^, manufacturer^, Version /format:csv
') do (
echo [%%a][%%b]
)
It just asks wmi for the required information, indicating exactly from where to retrieve it.
The resulting list will have a empty line, a header line (from here the skip=2) and the records with data. Records include the computer name (automatically included when asking for information in csv format), the full path to the file, the manufacturer, and the version, with commas as delimiters that are used to split the record an retrieve the needed fields.
The version field is included to avoid a problem with wmic output data that includes an aditional carriage return at the end of each line.
I was looking for a batchfile that could make a spreadsheet for all folder names at one location in 1st column and .xxx files in that folder in adjacent columns
eventually I would like to have a report that has folder name in first column and in each row corresponding .xxx file names in that folder
e.g. suppose folder_main has f1, f2, and so on and each folder has g1.xx, g2.xxx and so on
so report should show
column1 column 2 column 3 ............
f1 g1.xxx g2.xxx .....
f2 h1.xxx.......
Could you please help me to write a batch file or a script in vb to perform this function. I have thousand of folders.
Here's a starting point for you that creates a CSV-style output. You should be able to open it in any spreadsheet program.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%A IN ('DIR /AD /B 2^> NUL') DO (
SET line="%%A"
FOR /F "delims=" %%B IN ('DIR %%A\*.xxx /B 2^> NUL') DO SET line=!line!,"%%B"
ECHO !line!
)
I don't know how much you know about batch-scripting, so I leave it as-is for now. However, feel free to ask if something is unclear.
EDIT:
EnableDelayedExpansion is necessary to make the !-stuff work (see below).
The first FOR-line loops over all subdirectories in the current directory and throws away all possible error messages. The stuff between the DO ( and ) in the last line gets executed for each found subdirectory.
The line-variable will hold all files once the second FOR-loop has finished. It's initialised with the name of the subdirectory, i.e. the first column in the CSV-output.
That second FOR-loop looks for all files with extension xxx in the subdirectory found in the first loop and again throws away possible error messages. The SET appends the filenames to one another.
The ! is used instead of %, which you may be familiar with. It applies the delayed expansion to the code (if you want more information about this run SET /? on your command line).
If you want to dig even deeper into the directory structure, you would need to add /S to the DIR-command in the first loop, e.g. 'DIR /AD /B /S 2^> NUL'.