I'm making a program that needs a pendrive, and I want detect if a pendrive is in the pc and distinguish he from a internal disk or anything else, somebody can help me?
Here's an example.
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For /F "Skip=2 Tokens=*" %%A In ('WMIC DiskDrive Where InterfaceType^="USB"^
Assoc /AssocClass:Win32_DiskDriveToDiskPartition 2^>Nul') Do (
For /F UseBackQ^ Delims^=^"^ Tokens^=2 %%B In ('%%A') Do (
For /F Delims^=^":^ Tokens^=6 %%C In (
'WMIC Path Win32_LogicalDiskToPartition^|Find "%%B"') Do (
For /F "Skip=1 Delims=" %%D In ('WMIC LogicalDisk Where^
"DeviceID='%%C:'" Get DeviceID^, VolumeName') Do Echo( %%D
Set "_C=!_C!%%C")))
If Not Defined _C Echo( You do not have a USB drive connected && GoTo :EndIt
If "%_C:~,1%" Equ "%_C%" GoTo :Picked
Echo( Enter the USB drive letter from the above [%_C%]:
For /F "Delims=? Tokens=2" %%A In ('Choice /C %_C%') Do Set "Letter=%%A:"
:Picked
If Not Defined Letter (Call :Task %_C%:) Else (Call :Task %Letter%)
:EndIt
>Nul Timeout 5
Exit/B
:Task
Rem Place your commands here
Echo Your selected pen drive is %1
Pause
Related
I would like to input a variable to a text file in batch. I've tried a couple of things but I can't seem to find the proper variable. Can anybody help?
Here's my code:
echo.
echo List of all recorded wifi networks and their passwords:
echo.
setlocal enabledelayedexpansion
for /f "tokens=2delims=:" %%a in ('netsh wlan show profile ^|findstr ":"') do (
set "ssid=%%~a"
call :getpwd "%%ssid:~1%%"
)
echo.
echo.
choice /M "Do you want to save results to a txt file?"
set path=%errorlevel%
if %path% EQU 1 goto save_to_txt
if %path% EQU 2 goto END
:save_to_txt
if not exist List.txt (
echo Output of Wifi password grabber.bat > List.txt
)
echo ssid: %ssid% pass: %%i>> List.txt
pause
exit /b
:END
echo This is the no area
pause
exit /b
:getpwd
set "ssid=%*"
for /f "tokens=2delims=:" %%i in ('netsh wlan show profile name^="%ssid:"=%" key^=clear ^| findstr /C:"Key Content"') do echo ssid: %ssid% pass: %%i
Epicurieux.
You cannot use %%i outside of the for loop. it is a temporary variable used by for. You don't need to use multiple labels either, just ask beforehand if you want to save to file:
#echo off & set outfile=
setlocal enabledelayedexpansion
if not exist list.txt echo Output of Wifi password grabber.bat>list.txt
choice /c yn /M "would you like log SSID results to file?
if not errorlevel 2 set "outfile=>>list.txt"
for /f "tokens=2*delims=:" %%a in ('netsh wlan show profile ^|find /i "All User Profile"') do (
set "ssid=%%~a"
for /f "tokens=2*delims=:" %%i in ('netsh wlan show profile name^="!ssid:~1!" key^=clear ^| findstr /C:"Key Content"') do (echo ssid: "!ssid:~1!" pass: %%i)%outfile%
)
or without setting variables in the for loops and without the need for delayedexpansion
#echo off & set outfile=
if not exist list.txt echo Output of Wifi password grabber.bat>list.txt
choice /c yn /M "would you like log SSID results to file?
if not errorlevel 2 set "outfile=>>list.txt"
for /f "tokens=2*delims=:" %%a in ('netsh wlan show profile ^|find /i "All User Profile"') do for /f "tokens=2*delims=:" %%i in ('netsh wlan show profile name^=%%a key^=clear ^| findstr /C:"Key Content"') do (echo ssid: %%a pass: %%i)%outfile%
As a result of my comment, (beneath the other currently existing answer), I searched my script personal collection for an example of something similar.
The following, (which may fail if certain things do not match the specific criteria under which this was designed), is intended to return the Profile name, the Interface name, the SSID, and the password Key.
Other than the addition of the 'results to file' portion of the code, it is tested on Windows 8+ [English]
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "LDN=_tmp"
For /F Tokens^=*^ Delims^=^ EOL^= %%G In ('Dir "%TEMP%" /B /A:D /O:N 2^>NUL'
) Do Set "LDN=%%G%LDN%"
MD "%TEMP%\%LDN%" 2>NUL || GoTo :EOF
For /F "Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Call /? ^|
%SystemRoot%\System32\find.exe "=="') Do Set "HT=%%G"
(Set LF=^
% 0x0A %
)
For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "CR=%%G"
%SystemRoot%\System32\choice.exe /M "Print WLAN Profile results to file"
If ErrorLevel 2 (Set "FOD=CON") Else Set "FOD=%~dp0list.txt"
For /F Tokens^=2^,4^ Delims^=^" %%G In ('%SystemRoot%\System32\netsh.exe WLAN
export profile key^=clear folder^="%TEMP%\%LDN%"') Do (Set "_=%%~nH"
Set "{="
For /F "Tokens=* Delims=" %%I In ('%SystemRoot%\System32\cmd.exe /V /S /D
/C %SystemRoot%\System32\findstr.exe /R /I /C:"<keyMaterial>"^
/C:".*!CR!*!LF![\ \%HT:~-1%]*<\/SSID>" "%%H"') Do (Set "]=%%I"
SetLocal EnableDelayedExpansion
If "!]:~-6,1!" == "/" (For %%J In ("!]:~,-7!") Do (EndLocal
Set "}=%%~J")) Else For %%J In ("!]:~,-14!") Do (EndLocal
Set "{=%%~J")
If Defined { (SetLocal EnableDelayedExpansion
For /F Tokens^=*^ Delims^=^ EOL^= %%K In ('
Echo(^| Set /P "=!_:-%%G="^&:"!" 0^<NUL') Do (
(Echo Name="%%G" Interface="%%K" SSID="!}:*>=!" Key="!{:*>=!"
) 1>>"%FOD%" & EndLocal))))
RD /Q /S "%TEMP%\%LDN%"
If /I "%FOD%" == "CON" (Setlocal EnableDelayedExpansion
For /F "Tokens=1,2" %%G In ("!CMDCMDLINE!") Do (Endlocal
If /I "%%~nG" == "cmd" If /I "%%~H" == "/C" Pause))
EndLocal
GoTo :EOF
The file list.txt, if you choose to write to one, will be located along side the batch file, %~dp0, it should create it if it does not exist, or add to it, if it does.
I recently asked you guys some help for a double conditions in batch file. You guys helped me very well but I'm now struggling with a new trouble... I'm trying to optimize my script.
Previous question (How to double conditions in batch file?)
I would like to list all files with the .doc extension that are in the subfolders named on the current date only. I think I'm making a mistake on the use of this double loop.
#echo off
chcp 1252
set Pathname="D:\testDir"
set year=%date:~-4%
set month=%date:~-7,2%
set day=%date:~-10,2%
set logfile=%Pathname%\logs\log.txt
cd %Pathname%
d:
for /D /R %%i in (*%year%_%month%_%day%*) do (
for /R %%i %%s in (*.doc) do (
echo "file : %%s worked and does an output at %time%" >> %logfile%
)
)
Can you give me a little help? Thanks for your help and advices.
EDIT: I need to perform actions on each .doc file in folders containing the string YEAR_MONTH_DAY. But the .doc files can also be located in other subdirectories.
It could be something like that :
D:\testDir\directory1_2021_11_16\test.doc
D:\testDir\directory1_2021_11_16\test.log
D:\testDir\directory1_2021_11_16\subDirectory1\test.doc
D:\testDir\directory1_2021_11_16\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\test.doc
D:\testDir\directory1_2021_11_17\test.log
D:\testDir\directory1_2021_11_17\subDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory1\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\test.log
D:\testDir\directory1_2021_11_17\subDirectory2\subSubDirectory1\test.doc
D:\testDir\directory1_2021_11_17\subDirectory2\ubSubDirectory1\test.log
D:\testDir\directoryThatIDontCare\test.doc
D:\testDir\directoryThatIDontCare\test.log
D:\testDir\directoryThatIDontCare\subDirectory1\test.doc
D:\testDir\directoryThatIDontCare\subDirectory1\test.log
Any solutions ? Thanks for your time guys !
Here's an example script you can learn from, and run, to achieve what your submitted code was intending to do.
#Echo Off
SetLocal EnableExtensions
Set "CurDate="
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
For /F "Delims=" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
Win32_LocalTime Get Day^, Month^, Year /Format:List
2^>NUL') Do For /F "Tokens=*" %%H In ("%%G") Do Set /A "_%%G + 10000"
For /F "Tokens=1,* Delims==" %%G In ('"(Set _) 2>NUL"') Do (
SetLocal EnableDelayedExpansion & If %%H Gtr 10059 (For %%I In (!%%G:~-4!
) Do EndLocal & Set "%%G=%%I") Else For %%I In (!%%G:~-2!
) Do EndLocal & Set "%%G=%%I")
Set CurDate=%_Year%_%_Month%_%_Day%
For /F "Delims==" %%G In ('"(Set _) 2>NUL"') Do Set "%%G="
If Not Defined CurDate GoTo :EOF
Set "BaseLocation=D:\testDir"
PushD "%BaseLocation%" 2>NUL || GoTo :EOF
Dir "logs" /B /A:D 1>NUL 2>&1 || (MD "logs\%CurDate%" 2>NUL || GoTo :EOF)
Set "LogFile=%BaseLocation%\logs\log.txt"
For /F "EOL=? Delims=" %%G In ('Dir "*_%CurDate%" /B /A:D 2^>NUL'
) Do For /F Delims^= %%H In ('Set "PATHEXT^=" ^& %SystemRoot%\System32\where.exe
/F /R "%%G" "test.doc" 2^>NUL'
) Do (Echo file : %%~H worked and does an output at %TIME%) 1>>"%LogFile%"
If you want to know how any of it works, please use the built-in help information for each command, use the site search facility and/or your chosen search provider. I am not a private tutor, so will not be performing such a role.
How can I create a batch script that opens a random folder within a specific directory? This code here prints out a randomly chosen file(I need it to open the folder, not the file) but I could not figure out how to open it.
#Echo Off
:Start
set directory="D:\Movies"
set count=0
for /f %%f in ('dir "%directory%" /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /a-d /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
echo %filename%
pause
goto Start
I suddenly realised what I was doing wrong and solved the problem. Here is the final and working code:
#Echo Off
:Start
set directory="D:\Film"
set count=0
for /f %%f in ('dir "%directory%" /ad /b /s') do set /a count+=1
set /a randN=%random% %% %count% +1
set listN=0
for /f "tokens=1* delims=:" %%I in ('dir "%directory%" /ad /b /s^| findstr /n /r . ^| findstr /b "%randN%"') do set filename=%%J
:Found
%SystemRoot%\explorer.exe %filename%
exit /b
goto Start
You really shouldn't need to increment a count and use findstr for such a task; just assigning and sorting a random number should do:
#Echo Off
Set "source=D:\Film"
SetLocal EnableDelayedExpansion
For /D %%A In ("%source%\*") Do Set "$[!RANDOM!]=%%A"
For /F "Tokens=1* Delims==" %%A In ('"Set $[ 2>Nul|Sort"'
) Do Set "target=%%B" & GoTo Found
Exit /B
:Found
Explorer "%target%"
If you wanted a recursive directory search then change line 5 to:
For /D /R "%source%" %%A In (*) Do Set "$[!RANDOM!]=%%A"
I want to search all files with .xlsx extension, for now I'm using this:
for /R c:\ %%f in (*.xlsx) do set target=%%f
echo %target%
But, only searchs in "C" and does not includes the hidden files. So, my questions:
1) How can I search in all locations, I mean: C, D, E ... drives?
2) How I can search for hidden files too?
You can try something like that :
#echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx"
Title %~nx0 to search all *.%Ext% files
set "Log=%~dp0%Ext%_PATH.txt"
If Exist "%Log%" Del "%Log%"
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "Fixed_Drive=%%i"
Call :ShowMsg !Fixed_Drive!
(
#For /f "delims=" %%x in ('Dir /b /s /a "!Fixed_Drive!\*.%Ext%"') do (
#echo "%%x"
)
)>> "%Log%"
)
Start "" "%Log%"
Exit
::******************************************************************
:ShowMsg
Cls
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
exit /b
::******************************************************************
Edit :
To make a multiple search by extension like .xlsx .docx at the same time and get a separte log archive per each extension, you should try like this way :
#echo off
Color 9A & Mode con cols=70 lines=5
Set "Ext=xlsx docx"
For %%a in (%Ext%) Do (
if exist "%~dp0%%a_PATH.txt" del "%~dp0%%a_PATH.txt"
Call :Search "%%a" "%~dp0%%a_PATH.txt"
)
For %%a in (%Ext%) Do (
If Exist "%~dp0%%a_PATH.txt" Start "" "%~dp0%%a_PATH.txt"
)
Exit
::**********************************************************************************
:Search <Ext> <Log>
Cls
Title %~nx0 to search all "*.%~1 files"
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "Fixed_Drive=%%i"
(
#For /f "delims=" %%x in ('Dir /A:-D /b /s "!Fixed_Drive!\*.%~1"') do (
#echo "%%x"
)
)>> "%~2"
)
exit /b
::**********************************************************************************
At "for in", hidden files cannot be searched. Using option "/a" of "dir", hidden files can be srarched.
To search all of files with the extension of "xlsx" on a drive, I thought following method. But about this, each drive name has to input by user.
Drive C:
dir c:\*.xlsx /b /s /a
Drive D:
dir d:\*.xlsx /b /s /a
If you want to use data from "dir" at a batch file, how about following script?
#echo off
setlocal enabledelayedexpansion
set ct=0
for /f "usebackq tokens=*" %%a in (`dir c:\*.xlsx /b /s /a`) do (
set target[!ct!]=%%a
set /a ct=!ct!+1
)
set /a ct=%ct%-1
for /l %%i in (0,1,%ct%) do echo !target[%%i]!
The Where command searches 'hidden files', so using a method similar to Hackoo's:
#Echo(Searching...&#(For /F "Skip=1" %%A In ('WMIC LogicalDisk Where^
"DriveType>1 And DriveType!=5 And FreeSpace Is Not Null" Get DeviceID'
) Do #For %%B In (%%A) Do #For /F "Delims=" %%C In (
'Where/F /R %%B\ *.xlsx') Do #Echo=%%C)&Timeout -1
I have a program to ping computers, check there registry, and tell me the results.
I am now trying to script it so that I don't have to know the ip address just the host name of the computer.
I found a script to give me the ip off of a hostname
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
I have tried to simply insert this into a biger loop that uses a file to give it the host names.
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping -4 %%a ^| find "Pinging"') do (
set host=%%a
set ip=%%c
echo "."
echo %ip% %host%
pause
) >>%2
)
I have also tried it like this,
for /f %%a in (%1) do (
set /p hostname=%%a
for /f "tokens=1,2 delims=[]" %%b in ('ping -4 %%a ^| find "Pinging"') do set ip=%%c >nul
echo %ipaddress% %hostname%
)
Please any help on this would be greatly appreciated.
Thank you.
AFTER MUTCH HEAD BANGING
#echo off
SETLOCAL EnableDelayedExpansion
for /f %%A in (%1) do (
for /f "tokens=1,2 delims=[]" %%a in ('ping /4 /n 1 %%A ^| findstr "Pinging"') do echo "%%A %%b"
)