I am trying to create a batch file that will return a file location, including partition (or drive) so I can use it for more manipulation in that batch file.
The file name is known but the partition and directory are not known. Is that possible?
You can try something like that :
#echo off
Title Searching for file by name
Mode con cols=75 lines=3
cls & color 0A
Set SearchResult=SearchResult.txt
If Exist %SearchResult% Del %SearchResult%
echo(
set /P "FileName=Type the file name for looking for = "
Setlocal EnableDelayedExpansion
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
Set MyDrive=%%i
echo( & cls
echo( & echo Please Wait for moment .... Searching for "%FileName%" on "!MyDrive!\"
#where /r !MyDrive!\ "%FileName%" >> %SearchResult%
)
Start %SearchResult%
The following script -- let us call it findfile.bat -- searches for a file with the name given as a command line argument (for instance, findfile.bat "lostfile.txt") on local disks and network drives as an example:
#echo off
for /F "skip=1" %%I in (
'wmic LOGICALDISK WHERE ^( ^
DriveType^=3 OR^
DriveType^=4^
^) GET DeviceID'
) do (
for /F "delims=" %%J in ("%%I") do (
2> nul where /R %%J\ "%~1"
)
)
The wmic command is used to retrieve the drives available on the current system. In the example there are two DriveType values allowed: 3, meaning local disks, and 4, meaning network drives. You can adapt the filter as you like -- reference site Win32_LogicalDisk class for all the possible values. To not filter any drive types, simply remove the entire WHERE clause and use the remaining wmic command line wmic LOGICALDISK GET DeviceID instead.
Related
I want a batch script to search for software's on a computer by a specific vendor and save the results to a CSV file.... I am tried it with the following script, but it always returns NULL values, not able to figure out the issue.
#echo off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%Microsoft%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
exit
NB : % is needed to be used as wildcard
In this case you should add another percent character % to %Microsfot% to escape the wilcard of WMIC in your batch and become like %%Microsoft%%
#echo off
Title Batch Script to Search for Softwares by Vendor to a CSV file
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
:Sub
echo(
echo( Please wait a while ... Searching for installed softwares ....
Set "serial=%*"
Set "LogFile=%serial%.CSV"
If Exist "%LogFile%" Del "%LogFile%"
wmic /Append:"%LogFile%" product where "Name like '%%Microsoft%%'" get Name, Version /Format:CSV
Start "" "%LogFile%"
Exit
I have been using this batch file to collect the Serial number and UUID number and output to a CSV and now it no longer works.
#echo off
set outputfile="Y:\HP\UUDI.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct Get "UUID" /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
echo UUID,Serial,>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,>>%outputfile%
If someone can look at this file and help me understand what went wrong I would appreciate it
I don't understand what did you mean by "No Longer Works" ? Please be more explicit when you ask a question !
here is a test and tell me if this works or not on your side and i will edit this aswer according to your response !
#echo off
set "outputfile=%~dp0UUDI.csv"
#for /f %%i in (
'wmic csproduct Get "UUID" /value ^& wmic bios get serialnumber /value'
) do (
#for /f %%j in ("%%i") do set "%%j" & echo "%%j"
)
echo UUID,SerialNumber>"%outputfile%"
echo %UUID%,%SERIALNumber%>>"%outputfile%"
If exist "%outputfile%" Start "" "%outputfile%" & Exit
The only reason I can see for your provided code to change its behavior, is that which was commented already by Mofi. That is, you've somehow caused the location of WMIC.exe to have been removed from the %Path% environment.
I have decided to provide an alternative method of achieving your goal using your chosen command utility WMIC.exe, and using its full path, to prevent such a reliance in future.
The WMIC command is traditionally one of the slower ones, so this method invokes it only once. All you should need to do is Echo your commands, currently on lines 12and 14, each separated as in line 13. If any of your commands requires to Get more than one property, you should separate those with caret escaped commas, e.g. Get Property1^,Property2. The results, (subject to line/environment length limitations), will then be saved to variables, %Title%, and %Record%, which can later be output to a file outside of the loop. Note: all commands should use /Value, or the more correct, /Format:List.
Example, (don't forget to adjust your output file path on line 4 as needed):
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "outputfile=Y:\HP\UUDI.csv"
Set "WMIC=%SystemRoot%\System32\wbem\WMIC.exe"
Set "FIND=%SystemRoot%\System32\find.exe"
Set "Title="
Set "Record="
For /F "Tokens=1,* Delims==" %%G In ('
(
Echo CSProduct Get UUID /Value
^&
Echo BIOS Get SerialNumber /Value
^)
^| %WMIC% ^| %FIND% "="
') Do (If Not Defined Title (Set "Title=%%G") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Title!") Do (EndLocal
Set "Title=%%I,%%G"))
If Not Defined Record (Set "Record=%%H") Else (
SetLocal EnableDelayedExpansion
For /F "Tokens=*" %%I In ("!Record!") Do (EndLocal
Set "Record=%%I,%%H")))
If Defined Title ( Echo %Title%
Echo %Record%) 1>"%outputfile%"
Iv got my code to show a list of USb devices connected to my laptop and i can select the USB as well but when i try to back up a folder into the USB what i get instead is a folder named f in the folder i was trying to save. This is the code i have for selectong a USB device
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"
:USB
echo Avaiable Drive ==^> !$List!
set /p "$Drive=Enter the letter of the Backup drive : "
echo !$List! | find /i "%$Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:USB
set backupcmd=robocopy
and this is the code that backup the folder
%backupcmd% "%cd%" "%$Drive%"
Give this one a go:
#echo off
setlocal enabledelayedexpansion
set num=0
for /f %%a in ('wmic logicaldisk where drivetype^=2 get drivetype^, deviceid ^| find ":"') do (
set /a num+=1
set cnt=!cnt!!num!
echo (!num!^) %%a
set "d!num!=%%a"
)
choice /c !cnt! /M "select a number to choose a drive: "
echo You've chosen drive !d%errorlevel%! you can do everything with it from here..
You will notice that I set delayedexpansion here because of the setting and using of variables inside of the parenthesised code block.
Choice is a good option here as it does not allow typing the wrong values.
How do I look for a file and assign file name to a variable?
Example:
Let's say I am looking for a file in below location. File name constantly changes and only way to pick up the right file is by looking for today's date in file name string:
X:\VoyagerBackups\PickupLocation\tsiuiolou_live_Full_PCV048DB42_201812140000.Lts.bak
Here's what I have so far in my .bat file. Execution works as expected if I hard-code file name:
echo on
Rem Determine date
Set mm1=%date:~4,2%
Set dd1=%date:~7,2%
Set yyyy1=%date:~10,4%
Set rundate1=%yyyy1%%mm1%%dd1%
Set executefile1=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.Lts.bak
set downloadfile=tsiuiolou_live_Full_PCV048DB42_%rundate1%0000.bak
extractor64.exe -F %executefile1% -E %downloadfile%
move %executefile1% X:\VoyagerBackups\BackupFiles
Don't use the locale/user settings dependant %date% variable.
Use either wmic or PowerShell instead, they both have more stable methods.
:: Q:\Test\2018\12\14\SO_53787816.cmd
#Echo off
for /f "usebackq" %%A in (`
powershell -NoP -C "(Get-Date).ToString('yyyyMMdd')"
`) Do Set "Today1=%%A"
Echo PowerShell today:%Today1%
for /f "tokens=1-3 delims=.+-" %%A in (
'wmic os get LocalDateTime^|findstr ^^[0-9]'
) do Set "Today2=%%A"
Set "Today2=%Today2:~0,8%"
Echo wmic today:%Today2%
Set "Base=X:\VoyagerBackups\PickupLocation\"
PushD "%Base%" || (Echo can't find base:%Base%&Pause&Goto :Eof)
For /f "delims=" %%A in ('Dir /B "*%Today1%*" ') Do (
Echo found file:%%~fA
)
PopD
This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
I also have a variable called %file_name% in the batch file already defined.
So lets say that is it is %file_name% = V001-video_folder_6.mp4
As you can see there is some more extra information, V001- and .mp4.
I would like to use the var %file_name% to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6 and then put this information in a new var, let us say, %folder_path%.
I think I would use findstr however I have been playing around and not getting the best results.
The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.
#echo off
setlocal EnableDelayedExpansion
rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
set "line=%%a"
for %%b in (!line:/^= !) do set "lastPart=%%b"
set "folder[!lastPart!]=%%a"
)
set "file_name=V001-video_folder_6.mp4"
rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"
echo Folder path is: %folder_path%
Let us assume the posted lines are in file Test.txt in current working directory.
#echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little code.
The command goto inside FOR loop results in an immediate exit from loop processing output of findstr.exe after first found line containing the folder path of interest.
Perhaps better in case of searched folder is not found in text file:
#echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
This should work:
::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt
for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%
Which does what you want in effectively two lines.