I am relatively new to Batch and am having problems trying to find a string value within a txt file. I am using a neat program called CCExtactor to extract closed captioning from a file and need to grab the time of the closed captioning.
The output from CCExtractor looks like this:
###SUBTITLE#08:37#08:40#She ran away
My batch script looks like this:
for /F "delims=" %%a in (subtitle.txt) do ( echo %1|findstr /R /C:"^[^0-9][0-9]*$" )
I can't seem to get this to work! I am trying to skim the time values!
How would I go about doing this??
Here's an example.
#echo off
setlocal
set filename=subtitle.txt
for /f "delims=# tokens=2,3" %%I in ('findstr /r /c:"^###SUBTITLE#[0-9][0-9]:[0-9][0-9]#[0-9][0-9]:[0-9][0-9]#" %filename%') do (
echo start=%%I, end=%%J
)
In your for statement, you should make the delims value to be #. That way you can echo out %%d and %%e to get the start time and end time.
Related
I am searching a string on a list of files under many subfolders. If the string is found, then i'll perform more statements (which is not important anyways on my question).
So what I did is use the FIND instruction, using /c option. Then store the result of the FIND trimming the right most last character (which is the number of occurence of the string on a file). See below for the sample code...
for /F "delims=" %%A in ('find /c "PROGRAM-ID." "C:\FORTEST\CBL001.cbl"') do SET findstr=%%A
SET findstr=%findstr:~-1%
echo %findstr%
That code above gives me
1
Which is correct because the word "PROGRAM-ID" only occurred once in CBL001.cbl..
Now if I implement this code on a for loop to check for all files within a folder...
for /r "C:\FORTEST\" %%G in (*.*) do (
for /F "delims=" %%A in ('find /c "PROGRAM-ID." %%G') do SET findstr1=%%A
SET findstr1=%findstr1:~-1%
echo %findstr1%
)
The echo is returning
ECHO is off.
I dont understand how inserting the for loop into another for loop would mess up the code....
Thanks for reading.
=======
EDIT: Problem solved. Thanks to #npocmaka
So originally, before all this fiasco regarding storing FIND result into variable, I was using ERRORLEVEL to determine if the FIND instruction found the string or not (which sounds much simpler), but the ERRORLEVEL is not returning the correct value even if the string was not found. Hence my current solution of storing the result of FIND and using that data for comparison.
But it seems DelayedExpansion was the original culprit causing ERRORLEVEL to not produce the correct value.
See below for final code.
for /r "C:\FORTEST" %%G in (*.*) do (
find "PROGRAM-ID." %%G
if !ERRORLEVEL! EQU 0 (
echo ITS A COBOL
::do something
) else (
echo NOPE NOT A COBOL
)
)
Also thanks to other replies, tho I didnt bother using them, but I appreciate the efforts!
find doesn't write the dashes and filenames, if it gets it's input from a pipe. And there is no need for an additional variable (findstr1):
for %%G in (*.*) do (
for /F "delims=" %%A in ('type %%G^|find /c "PROGRAM-ID."') do echo %%A %%G
)
The correct answer is using EnableDelayedExpansion
Thanks to #npocmaka
I'm new to batch script, I have a file with a string containing the word "media"(quotes included) and I need to insert another string right before it.
I messed around with findstr but couldn't make heads or tails of it.
Edit2:
here's what i did, doesn't seem to work:
#echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f "delims=," %%a in (f1.txt) do (
set foo=%%a
if !foo!=="media"
set var=!foo:"media"=aa"media"!
echo !foo! >> f2.txt)
You have two options to do this. You can read the file with a FOR /F command or if you are just editing a single line file then you can use the SET /P command.
Here are both of those examples in a single batch file.
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%G in (sotemp.txt) do (
set "line=%%G"
set "foo=!line:"media"=aa"media"!"
echo !foo!
)
set /p "line="<sotemp.txt
echo %line:"media"=aa"media"%
pause
I have a series of files that I need to rename on a daily basis. The files I receive have the following format: yyyyMMdd_hhmmss_xxx.someFileName.txt I need to strip out the time stamp in the middle as well as the three digit field preceeding the filename and leave the date and the "someFileName.txt" piece. The resulting filename should look like: yyyyMMddsomeFileName.txt
I'm pretty clueless when it comes to bat files, I've done some experimenting:
#setlocal EnableDelayedExpansion
#for %%i in (.\*.txt) do call rename %%i
:rename
#set dateString=%%i:~0,8%
#set nameString=%%i:~20%
#set combinedString=%dateString%%nameString%
#echo %combinedString%
Clearly, this doesn't actually rename anything yet. It's just supposed to print the combinedString output. I'm getting a syntax error: "The syntax of the command is incorrect ~0,8 ~20"
What's going on here? What's the correct approach for this?
This should work for what you wanted.
#echo off
for /f "delims=" %%X in ('dir /a:-d /b *.txt') do (
for /f "tokens=1,2,3,* delims=_." %%A in ("%%~nxX") do (
echo %%A%%D
)
)
Replace the echo command with ren "%%~fX" "%%A%%D" when you want to rename them.
I program a batch file, but I'm new to for-loops which I need.
I now know how the syntax works, but I cannot figure out why my loop does not do what it should.
This code is an extract from my file:
#echo off & setlocal enabledelayedexpansion
set /p file=:
set /a numberofgoals=0
for /f "delims=" %%a in ("%file%.txt") do set /a "numberofgoals+=1"
echo %numberofgoals%
pause > nul
If I did everything right my output should be the length of the specified textfile and it worked for me before, but apperently I changed something in the code that I'm not sure about and the output of %numberofgoals% is everytime exactly 1 now, regardless of how long my text file is.
My question is: What have I done wrong and why is the output 1 now? I cannot even remember having changed something there...
EDIT: I changed "delims=" into "usebackq delims=" as suggested and it works now, thank you.
The quotes in the for loop's parentheses mean "process this string" rather than "read this file". Use the usebackq option to indicate that the quotes are providing a filename:
for /f "usebackq delims=" %%a in ("%file%.txt") do set /a "numberofgoals+=1"
and you should be golden.
Type help for in your cmd window for the gory details.
I'm trying to write a windows batch file that would read in a text file with certain text in it and increment some values in that file.
The text file would contain text like :
public static const COUNTER:int = 0
the batch file would then search for "COUNTER:int = 0" and increment the 0 value.
Unfortunately my knowledge of windows batch files is non-existent, so any advice or help on the matter is appreciated!
Thanks!
I'd advise against using batch files for parsing files. It just doesn't play nicely with such things.
If you are absolutely sure that your file can never contain the following characters: &, |, >, <, " then you can use a batch file. But catering for those characters is hard and and—in some cases—downright impossible.
In such cases you would be better off either using VBScript to process the file or using various UNIX tools to perform that task. This might be possible using awk.
Note that Windows 7 includes Windows PowerShell where such a task is really trivial. And it can be installed separately on Windows XP and higher.
However, I think I'd go with a VBScript solution here.
If your requirements match above constraints, you can do it with a batch file. The one below should work.
First of all, we need delayed expansion, so this has to be one of the very first lines in the batch:
setlocal enableextensions enabledelayedexpansion
You can iterate over the lines in a file using for /f:
for /f "delims=" %%x in (my_file) do call :process "%%x"
goto :eof
:process
...
goto :eof
This will call the subroutine process for each line of the file, handing over the line as argument. The delims= part specifies that we don't want tokenizing on that line. We now look at the contents of that routine.
Fist we need to know whether the line even contains the string we're looking for (Note that the loop variable, containing the line is only %%x inside of the loop, in the subroutine it becomes %1):
echo %1 | findstr "COUNTER:int" >nul 2>&1
if not errorlevel 1 (
...
) else (
echo %~1>>new_file
)
Inside, where now the ... are, we can handle that line in case it contains the search string. We first need to dissect it. The easiest way would be to split it up at the = character and then we increment the number and output everything again.
for /f "tokens=1,2 delims==" %%a in (%1) do (
set /a number=%%b+1
echo %%a= !number!>>new_file
)
So, putting it all together, it looks like this:
#echo off
setlocal enableextensions enabledelayedexpansion
del new_file
for /f "delims=" %%x in (my_file) do call :process "%%x"
goto :eof
:process
echo %1 | findstr "COUNTER:int">nul 2>&1
if not errorlevel 1 (
for /f "tokens=1,2 delims==" %%a in (%1) do (
set /a number=%%b+1
echo %%a= !number!>>new_file
)
) else (
echo %~1>>new_file
)
goto :eof
Code can be found in my SVN.
If you've got sed, you can do this:
sed "s/COUNTER:int *= *0/COUNTER = 1/" file.cs >newfile.cs
This assumes that you're only looking for a particular constant name and wanting to change '0' to '1'.