I’ve got a text file named ‘Settings.txt’
Within the file I have “Access: False”
Am I able to grab the text after ‘Access:’ to see what they have inputted?
Here is what I have tried:
for /f "token=2 delims=:" %%L in (Settings.txt) do (if %%L==True goto Start ELSE FALSE goto Fail )
You can incorporate findstr and also you did not use else correctly.
for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do (
if /i "%%i" == "True" (
goto :start
) else (
goto :fail
)
)
Obviously there are easier ways, this example does not need else it will fall through to start if the value is True else it will skip that label and goto fail label:
for /f "tokens=2 delims=: " %%i in ('type "settings.txt" ^| findstr /i "Access:"') do if /i not "%%i" == "True" goto :fail
:start
echo do start stuff here:
goto :eof
:fail
echo do fail stuff here
You will need to check for the word Access (unless there is only one line in Settings.txt), to add the SPACE behind the : to the list of delimiters, and you will need to adapt the if syntax and probably make it case-insensitive (/I):
for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
if /I "%%K"=="Access" if /I "%%L"=="True" (goto Start) else if /I "%%L"=="False" (goto Fail)
)
Which can be rewritten as this (to make the if blocks more obvious):
for /F "usebackq tokens=1* delims=: " %%K in ("Settings.txt") do (
if /I "%%K"=="Access" (
if /I "%%L"=="True" (
goto Start
) else (
if /I "%%L"=="False" (
goto Fail
)
)
)
)
Related
I am attempting to create a batch file that will rename a series of file based on an XML file. It works until it encounters a file name with the same name at which point it skips the file. I was able to amend the script to create an incremental version of the file, the issue that I am having now is the script will cycle through just a few files and then it exits. Any ideas why it's doing that?
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
goto :EOF
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF
Here is an example of the output that I am able to see at the moment:
C:\Recordings>for %z in ("C:\Recordings\AT1*.WAV") do (for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "%~nxz"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "%z" "%c-%t-%u_%d%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%z" "%c_%t-%u_%d%~xz"
goto :EOF
) ) )
C:\Recordings>(for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "AT1_ID1_TT3_ID6-1626034093.52156.WAV"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c-%t-%u_%d.WAV" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c_%t-%u_%d.WAV"
goto :EOF
C:\Recordings>if exist "John Doe 1_2021-07-11 15-08_9394056960_2.WAV" set /A "Number+=1" & goto NumberedRename
C:\Recordings>ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "John Doe 1_2021-07-11 15-08_9394056960_2.WAV"
) ) )
Shortly after posting the question I tinkered with the script and realized that one of the goto :EOF was causing the script to end prematurely. I removed the goto :EOF and it seems to be working as intended.
Updated Script
for %%z in ("C:\Recordings\AT1*.WAV") do (
for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
)
)
)
)
:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
ren %1 "%~n2_%Number%%~x2"
goto :EOF
It seems for me a very simple question, however I am struggling a lot with this and don't find an answer yet.
My aim is to count occurences of a specific string in a file.
I do or try to do this using following command:
for /f "tokens=2 delims=;" %%a in ('path to file with searchstring') do (
set count=('path to file I am searching in' find /c "%%a")
if !count! NEQ 0 echo !count!
)
Seems straightforward for me however the second line seems to be wrong as the script exits.
I want to save the result of findin a var because I only wants to outpout Nonzero resutls.
Here's an example batch-file using findstr, for better word matching, (It uses the filenames from your previous question):
#(For /F "UseBackQTokens=2Delims=;" %%G In ("classID.txt")Do #(Set "count=0"
For /F %%H In ('^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
)Do #(Set /A count+=1)&SetLocal EnableDelayedExpansion
If !count! Gtr 0 (Echo %%G: !count!)&Endlocal))&Pause
Here it is fully parenthesized over multiple lines for you to better understand:
#Echo Off
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /I /N "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
Pause
And a fully parenthesized example of it outputting the results to a file named results.txt:
#Echo Off
(
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
)>"results.txt"
I am attempting to create a batch script that will query the registry for uninstall packages. The goal is to create a batch script that will uninstall all VNC programs (UltraVNC, RealVNC, and TightVNC). This must be compatible with both Windows XP and Windows 7.
I started with the script from this StackOverflow solution Reg Query script for batch script . I am trying to alter the code to allow it to search for "VNC" in the DisplayName, strip the text between the {} in its corresponding UninstallPath, and then run msiexec.exe /qn /x {package-identifier} to silently uninstall the program.
When running the following code, it retrieves several programs that do not contain "VNC" in their DisplayName. The issue appears to lie in the following line of code:
if not "x!str1:VNC=!"=="x!str1!" (
Source of this line of code: How can I check if a variable contains another variable within a windows batch file?
I have tried the following code found at as an alternative, but it also listed way too many packages and did not properly pick only packages with "VNC" in their names.
set str1 = !product[%counter%]!
echo str1 > temp.dat
findstr /c:"VNC" "temp.dat" >nul 2>&1
if %errorlevel% == 0 (
Note: Many of the echo commands are there simply for testing purposes.
#echo off
setlocal
set regVar=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
set excluded=/c:" Microsoft" /c:"MDOP" /c:"Dell"
set count=1
for /f "tokens=1,2,*" %%a in ('Reg Query %regVar% /S ^| findstr "DisplayName HKEY_ UninstallString"') do (
setlocal EnableDelayedExpansion
for %%n in (!count!) do (
ENDLOCAL
SET HKEY=Y
IF "%%a"=="DisplayName" SET "HKEY="&set product[%%n]=%%c
IF "%%a"=="UninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
IF "%%a"=="QuietUninstallString" SET "HKEY="&IF NOT DEFINED uninstall[%%n] set uninstall[%%n]=%%c
IF DEFINED hkey IF DEFINED product[%%n] IF defined uninstall[%%n] SET /a count+=1&SET "hkey="
IF DEFINED hkey set "product[%%n]="&SET "uninstall[%%n]="
)
)
IF NOT DEFINED product[%count%] SET "uninstall[%count%]="&SET /a count-=1
IF NOT DEFINED uninstall[%count%] SET "product[%count%]="&SET /a count-=1
ECHO %count% entries found
set counter=%count%
pause
#setlocal enableextensions enabledelayedexpansion
:while
if %counter% GTR 0 (
set str1 = !product[%counter%]!
echo str1
if not "x!str1:VNC=!"=="x!str1!" (
echo !product[%counter%]!
echo !uninstall[%counter%]!
set UninstallString=!uninstall[%counter%]!
echo !UninstallString!
set RunUninstall = ""
for /f "tokens=2 delims={}" %%A in ("!uninstall[%counter%]!") do echo %%A REM set RunUninstall="msiExec.exe /qn /x{%%A}" (commented for testing purposes)
call !RunUninstall!
set /a counter-=1
GOTO While
) Else (
set /a counter-=1
GOTO While
)
)
Output Snippet:
Intel(R) Network Connections 15.7.176.0
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86418051F0}
26A24AE4-039D-4CA4-87B4-2F86418051F0 REM set RunUninstall="msiExec.exe /qn /x{26
A24AE4-039D-4CA4-87B4-2F86418051F0}"
ECHO is off.
Microsoft .NET Framework 4.5.2
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
MsiExec.exe /X{26784146-6E05-3FF9-9335-786C7C0FB5BE}
26784146-6E05-3FF9-9335-786C7C0FB5BE REM set RunUninstall="msiExec.exe /qn /x{26
784146-6E05-3FF9-9335-786C7C0FB5BE}"
ECHO is off.
Lenovo ThinkVantage Toolbox
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
MsiExec.exe /I{23170F69-40C1-2702-0920-000001000000}
23170F69-40C1-2702-0920-000001000000 REM set RunUninstall="msiExec.exe /qn /x{23
170F69-40C1-2702-0920-000001000000}"
ECHO is off.
OSFMount v1.5
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
MsiExec.exe /I{1B8ABA62-74F0-47ED-B18C-A43128E591B8}
1B8ABA62-74F0-47ED-B18C-A43128E591B8 REM set RunUninstall="msiExec.exe /qn /x{1B
8ABA62-74F0-47ED-B18C-A43128E591B8}"
UPDATE:
I modified the code provided by wOxxOm in an attempt to make an exception for TightVNC and uninstall it quietly. Since there is not a QuietUninstallString in the registry for TightVNC, I am attempting to accomplish this by utilizing the msiexec.exe /qn /x options. However it keeps throwing the following error:
else was unexpected at this time.
My modified version of wOxxOm's code is listed below:
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do (
set product=%%d
if "x!product:TightVNC=!"=="x!product!" (
for /f "tokens=2 delims={}" %%A in ("!Uninstall!") do (
echo Found !product!
Running msiExec.exe /qn /x{%%A} REM (COMMENTED FOR TESTING)
REM CALL msiExec.exe /qn /x{%%A}
)
) else (
echo Found %%d
echo Running !Uninstall! (COMMENTED FOR TESTING)
rem call !Uninstall!
echo.
)
)
)
)
)
Don't use spaces around = in set str1=!product[%counter%]!
Instead of if %counter% GTR 0 ( which doesn't work and makes the loop infinite)
use if %counter%==0 goto done and a :done label after the loop
With quotes you don't need to add x to the comparison.
Don't forget about 64-bit Windows and check Wow6432Node tree too (see the alternative code)
:while
if %counter%==0 goto done
set str1=!product[%counter%]!
if not "!str1:VNC=!"=="!str1!" (
..............................
rem call !RunUninstall!
)
set /a counter-=1
GOTO While
:done
Alternatively you can make the batch file much faster (the "arrays" are slow in case of many strings) by listing only the products with VNC in DisplayName first and then fetch the UninstallString:
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do echo Found %%d
echo Running !Uninstall! (COMMENTED FOR TESTING)
rem call !Uninstall!
echo.
)
)
)
)
pause
Modifying wOxxOm's code, I was able to create the following code to quietly uninstall TightVNC, UltraVNC, and RealVNC. It will also start the uninstaller for any other software with "VNC" in the DisplayName in the Windows registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall.
Hopefully this is handy to someone else out there as well.
#echo off
setlocal enableDelayedExpansion
for %%a in ("" "\Wow6432Node") do (
for /f "delims=" %%b in ('
reg query HKLM\SOFTWARE%%~a\Microsoft\Windows\CurrentVersion\Uninstall ^
/s /d /f "VNC" ^| findstr "HKEY_ DisplayName"
') do (
set "line=%%b"
if "!line:~0,4!"=="HKEY" (
set "key=!line!"
) else (
set Uninstall=
rem Sort /r makes QuietUninstallString the last line
for /f "tokens=2*" %%c in ('
reg query "!key!" ^| find "UninstallString" ^| sort /r
') do if not "%%d"=="" set "Uninstall=%%d"
if defined Uninstall (
for /f "tokens=2*" %%c in ("!line!") do (
set product=%%d
if NOT "x!product:TightVNC=!"=="x!product!" (
for /f "tokens=2 delims={}" %%A in ("!Uninstall!") do (
echo Found !product!
CALL msiExec.exe /qn /x{%%A}
echo.
)
) else (
if NOT "x!product:UltraVNC=!"=="x!product!" (
for /f "tokens=1 delims=." %%A in ("!Uninstall!") do (
echo Found !product!
CALL %%A.exe" /VERYSILENT /NORESTART
echo.
)
) else (
if NOT "x!Uninstall:RealVNC=!"=="x!Uninstall!" (
for /f "tokens=1 delims=." %%A in ("!Uninstall!") do (
echo Found !product!
CALL %%A.exe" /VERYSILENT
echo.
)
) else (
echo Found %%d
CALL !Uninstall!
echo.
)
)
)
)
)
)
)
)
So I wanted to have plaintext comments in my bat file with useage information at the top by skipping the text using goto but wanted to display the text as help info if say /? switch was used..
I managed to get the text to display with this method
#echo off
goto start
:help
some
text
here not commands
#echo off
goto:eof
:start
echo on && prompt $h && call :help 2>nul
which displays this:
some
text
here not commands
Does anyone know a way to remove the blank lines?
I typically begin lines with :: to indicate a comment, and ::: to indicate documentation. I can then use FOR /F with FINDSTR to easily print out the documentation, as long as no displayed documentation line begins with :.
#echo off
::Documentation
:::
:::some
:::text
:::here not commands
:::
:::
::Show help
call :help
exit /b
:help
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do #echo(%%A
exit /b
If I have a lot of documentation, then I may put a GOTO :START at the top to improve script start-up time.
#echo off
goto :start
::Documentation
:::
:::some
:::text
:::here not commands
:::
:::
:start
::Show help
call :help
exit /b
:help
for /f "tokens=* delims=:" %%A in ('findstr "^:::" "%~f0"') do #echo(%%A
exit /b
Another one! ;-)
#echo off
goto help-end
:help-start
some
text
here not commands
:help-end
if "%~1" neq "/?" goto exitHelp
set "start="
for /F "delims=:" %%a in ('findstr /N /B ":help" "%~F0"') do (
if not defined start (set "start=%%a") else set "end=%%a"
)
for /F "skip=%start% tokens=1* delims=:" %%a in ('findstr /N "^" "%~F0"') do (
if "%%a" equ "%end%" goto exitHelp
echo(%%b
)
:exitHelp
echo Normal process continue here
#rojo
like this?
#echo off
goto start
:help
call :heredoc help && goto helpend
some
text
here not commands
:helpend
goto:eof
:start
call :help
pause
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
set "line=%%A" && set "line=!line:*:=!"
if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
if "!line:~0,13!"=="call :heredoc" (
for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
if #%%i==#%1 (
for /f "tokens=2 delims=&" %%I in ("!line!") do (
for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
)
)
)
)
)
goto :EOF
Output:
some
text
here not commands
Press any key to continue . . .
It definitely works!
#echo off
for /f "tokens=* delims= " %%f in (myfile) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=1* delims=/" %%a in (%line%) do (
echo Got one token: %%a
set line=%%b
)
if not "%line%" == "" goto :processToken
goto :eof
In my text file I have lines like
"test a","test b","testdim"
"Line 2", "test d","testdin"
so when i see for /f "tokens=1* delims=/" %%a in (%line%) do ( with ECHO ON, %line% is showing as "test a" "test b" "testdim".
I dont know why commas are missing but I need those commas so that I can use "delims=," to get these values out of each line.
try this, added some double quotes:
#echo off &SETLOCAL
for /f "tokens=* delims= " %%f in (file) do (
set "line=%%f"
call :processToken
)
goto :eof
:processToken
ECHO "%line%"
for /f "tokens=1* delims=/" %%a in ("%line%") do (
echo Got one token: "%%a"
set "line=%%b"
)
if not "%line%" == "" goto :processToken
goto :eof
#echo off
for /f "tokens=* delims= " %%f in (myfile) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for %%a in (%line%) do (
echo Got one token: %%a
)
goto :eof