I want to make a for loop that checks if specific files exist and if they do, checks if a specific keyword exists in each file. For the findstr
to work I have to change the encoding for the targeted files. If at least one file doesn't contain it's keyword, I want to set a variable to 0. If all files contain their keywords I want to set a variable to 1.
I've got the first part working but the whole findstr
section gets skipped. How do I implement findstr
into my for loop the way I want it to work?
@echo off
set "var="
call :script
echo var: %var%
pause
exit
:script
for %%g in ("*.txt") do if exist "%%g" (
if not exist "Test1.txt" set "var=0" & exit /b
if not exist "Test2.txt" set "var=0" & exit /b
if not exist "Test3.txt" set "var=0" & exit /b
) else (
powershell -command "(gc -raw Test1.txt) | sc -encoding ASCII Test1.txt; (gc -raw Test2.txt) | sc -encoding ASCII Test2.txt; (gc -raw Test3.txt) | sc -encoding ASCII Test3.txt"
findstr /b /c:"Fox" "Test1.txt"
findstr /b /c:"Dog" "Test2.txt"
findstr /b /c:"Cat" "Test3.txt"
powershell -command "(gc -raw Test1.txt) | sc -encoding Unicode Test1.txt; (gc -raw Test2.txt) | sc -encoding Unicode Test2.txt; (gc -raw Test3.txt) | sc -encoding Unicode Test3.txt"
if errorlevel 0 if not errorlevel 1 (
set "var=1"
) else (
set "var=0"
)
)
EDIT 1:
I have come up with that code which does the job for now:
@echo off
set "var=No"
if exist "*.txt" (
call :script
)
echo var: %var%
pause
exit
:script
for %%a in ("*.txt") do (
for %%b in ("Test1.txt" "Test2.txt" "Test3.txt") do (
powershell "(gc -raw Test1.txt) | sc -encoding ASCII -nonewline Test1.txt"
findstr /b /c:"Fox" "Test1.txt" || (
powershell "(gc -raw Test1.txt) | sc -encoding Unicode -nonewline Test1.txt"
exit /b
)
powershell "(gc -raw Test2.txt) | sc -encoding ASCII -nonewline Test2.txt"
findstr /b /c:"Dog" "Test2.txt" || (
powershell "(gc -raw Test2.txt) | sc -encoding Unicode -nonewline Test2.txt"
exit /b
)
powershell "(gc -raw Test3.txt) | sc -encoding ASCII -nonewline Test3.txt"
findstr /b /c:"Cat" "Test3.txt" || (
powershell "(gc -raw Test3.txt) | sc -encoding Unicode -nonewline Test3.txt"
exit /b
)
powershell "(gc -raw Test1.txt) | sc -encoding Unicode -nonewline Test1.txt; (gc -raw Test2.txt) | sc -encoding Unicode -nonewline Test2.txt; (gc -raw Test3.txt) | sc -encoding Unicode -nonewline Test3.txt"
set "var=Yes"
exit /b
)
)
EDIT 2:
Applying the above mentioned code in the real batch file does not work as intended. This is my code:
:localization
cls
color 7
set "fz="
set "localization01="
REM ### Checking for installation status here.
if exist "config\_temp\installed.fz" del /f /q "config\_temp\installed.fz"
call :subroutine01
echo %localization01% localization files
:subroutine01
set "dir=%apb%\APBGame\Localization\GER"
for %%a in ("%dir%\*.GER") do (
for %%b in ("%dir%\EULA.GER") do (
powershell "$dir = (gc 'config\_temp\path.txt')+'\APBGame\Localization\GER'; (gc -raw $dir\EULA.GER) | sc -encoding ASCII -nonewline $dir\EULA.GER"
findstr /c:"Localization files have been installed successfully." "%dir%\EULA.GER" || (
powershell "$dir = (gc 'config\_temp\path.txt')+'\APBGame\Localization\GER'; (gc -raw $dir\EULA.GER) | sc -encoding Unicode -nonewline $dir\EULA.GER"
set "localization01=uninstall"
exit /b
)
powershell "$dir = (gc 'config\_temp\path.txt')+'\APBGame\Localization\GER'; (gc -raw $dir\EULA.GER) | sc -encoding Unicode -nonewline $dir\EULA.GER"
set "localization01=install"
exit /b
)
)
I want to set the variable %localitation01%
to either install or uninstall depending on the outcome of the subroutine.
If the file EULA.GER exists and if it contains Localization files have been installed successfully I want to set "localization01=uninstall"
else set "localization01=install"
.
The same goes for if I have to check if multiple files exist and if those files contain specific keywords. If true set "localization01=uninstall"
else set "localization01=install
.
I hope the outcome I want to achieve is clear and that someone can help me figure out a way to make it work. Thanks in advance!
I am afraid your description is confusing, and your code is needlessly convoluted... The code below do the following: If all files Test1.txt, Test2.txt and Test3.txt exists and each file contains its corresponding word, then var=1
. Otherwise (a file doesn't exists or not contains its word): var=0
@echo off
setlocal
set "var=1"
for %%a in ("Test1.txt=Fox" "Test2.txt=Dog" "Test3.txt=Cat") do (
for /F "tokens=1,2 delims==" %%x in (%%a) do (
if not exist "%%x" (
set "var=0"
) else (
findstr /B /C:"%%y" "%%x"
if errorlevel 1 set "var=0"
)
)
)
echo var: %var%
pause
exit
The findstr
command can find an ASCII string not matters the file encoding as long as the bytes of the string be together in the file.