How can I - Count the occurances of a \ in a line in a text file using windows batch file?

  • 9 months ago
Comment

ChatGPT
1 / 1

You can count the occurrences of a backslash \ in a line of a text file using a Windows batch script. Here's how you can do it:

batch
@echo off
setlocal enabledelayedexpansion

set "inputFile=input.txt"
set "backslash=\"

for /f "delims=" %%A in ('type "%inputFile%"') do (
set "line=%%A"
set "count=0"
for %%B in (!line!) do (
set "token=%%B"
if "!token!"=="%backslash%" (
set /a "count+=1"
)
)
echo !count! backslashes found in: !line!
)

endlocal

Replace "input.txt" with the actual path to your input text file.

This script reads each line from the input file, tokenizes the line by spaces (assuming the backslashes are separated by spaces), and then checks each token for being a backslash. If it's a backslash, it increments the count variable. Finally, it outputs the count and the original line for each line in the input file.

Keep in mind that this script assumes that backslashes are separated by spaces. If your input file has a different format, you might need to adjust the script accordingly.