Extract a line with a string with double quote by using findstr - batch-file

First of all thank you for this site, I'm a terrible coder and so i need your help with making a batch script, i try to extract only lines that contains Copy " in that line from a text file by using findstr which actually works without that whitespace and double quote. But it will extract the line with "Copy and Help" too which i don't need.
Example:
My text contains (source.txt)
a
asd Copy and help with these command prompt:
a
asd Copy "c:\.." a b c(white space)
a
asd Copy and help with these command prompt:
Copy "d:\.." a c c(tab space)
avs
Copy "e:\.." a a c
vvddf
Output file (op.txt) (should be)
Copy "c:\.." a b c
Copy "d:\.." a c c
Copy "e:\.." a a c
After my original code I tried to usefindstr /c:"Copy ^"" > op.txtbut this doesn't work. To let the findstr know to search a line containing Copy " Copy[whitespace][double quote]
Updated section
First off my required batch script is working from help by foxidrive. There is still some tweak to do, but i will post it as another question. This question is answered.
This is my updated code for now.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="details.txt"
IF EXIST %source% (
FIND /i "copy " <%source% |FIND "\" >op.txt
) ELSE (
Exit
)
Thanks foxidrive for the start off and solving my first issue.
Sorry for my English, it's poor like my coding

A simplistic solution is to use this
find ":\" <source.txt >op.txt
Or this is another workaround:
find ":\" <source.txt |find /i "copy " >op.txt

Related

Apply batch OCR through command line

I am totally new to batch scripting for cmd (Windows).
I have installed tesseract to work as a command line OCR tool.
Now I would like to run OCR on 100 images that I have stored in a folder.
How can I do it with batch ?
The command to run tesseract on an image and return the OCR text in a text file is:
"C:\OCR\tesseract" "C:\Image_to_OCR.jpg" "C:\out"
More information: http://chillyfacts.com/convert-image-to-text-using-cmd-prompt/
As you can see, I would probably need to make a for loop whith automatically iterates through the number of pictures and changes the name of the picture in the command accordingly and of course also the output name of the text file... but I don't know how to do it.
Any help would be very appreciated !
EDIT:
As suggested in the answer by Stephan, I could write:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
However, the command line (cmd) only apears quickly and closes imidiatley and nothing happens.
My files are not directly in C:\ but in "C:\Users\James\Desktop\", therefore I wrote the command as such:
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
...but as said before, it does not work somehow.
Also, can I change the output txt name to be the same as the input image name, like so ?
for %%A in (C:\Users\James\Desktop\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "%%~fA"
This worked :
I got two great answers! Thanks a lot. The final thing that worked was a mix between both answers:
#Echo off
PushD C:\Program Files (x86)\Tesseract-OCR || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\EPFL\Google Drive\EDx PDF Maker\Cellular Mechanisms of Brain Functions\Slides\1\*.jpg") do tesseract.exe "%%~fA" "%%~dpnxA"
I don't know your program C:\OCR\tesseract.exe but I assume it needs supporting tools/files present in the C:\OCR folder, so either you have to set that folder as the current one or have it contained in your path variable.
#Echo off
PushD "C:\OCR" || (Echo couldn't pushd C:\OCR & Exit /B 1)
for %%A in ("C:\Users\James\Desktop\*.jpg") do tesseract.exe "%%~fA" "%%~dpnA.txt"
The "%%~dpnA.txt" will save the text with same drive/path/filename and extension .txt
Use a for loop to iterate over the files:
for %%A in (C:\*.jpg) do "C:\OCR\tesseract.exe" "%%~fA" "C:\out"
%%A is the filenames (one at each run of the loop),
%%~fA is the fully qualified filename (just to be sure).
Read the output of for /? to learn more about those modifiers.
Note: this is batchfile syntax. To use it directly on command line, replace every %% with a single %

How do I find spaces in strings with batch

I've looked everywhere for an answer to this. Basically I need to find a " "(a space) in batch.
So, what i'm doing is making a custom command import/export batch script. Here is what i start by doing:
echo Enter commands file name without the .bat
set/p "file=>"
Now, after that set/p... line, I want to test if the "file" string contains a space.
How I can achieve this without using a different coding language? I'm keeping this strictly batch.
This works for me:
for /f "tokens=2" %%a in ("%file%") do Echo Contains Space
Which will only Echo Contains Space if there is a whitespace in file

How to delete the first line of a file using Batch?

How to delete the first line of a file using Batch?
I really need help... I've tried everything but it didn't work. :(
I've tried this :
#echo off
Type test.txt | findstr /I /V /C:"So, lets remove this line" >>Test2.txt
exit
Some tasks are clumsy with pure batch techniques (and mangles characters) - this is a robust solution and uses a helper batch file called findrepl.bat that uses built in jscript-ing from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file.
type "file.txt"|findrepl /v /o:1:1 >"newfile.txt"
Another method is to use more.exe but it has limitations with the number of lines at 64K and it mangles TAB characters.
more +2 <file.txt >newfile.txt
(echo off
for /f "skip=1 tokens=1* delims=" %A in (c:\windows\win.ini) do echo %A
echo on)
You'll need to loop through your file line by line and output each line, except the first one.
From your example it looks like you might be looking for a specific string.
try this question/answer .. it might help you get you on your way.

How to open a file using a string variable in path name in batch file

I have a text file, call it path.txt in C:\path.txt and it will only have one line of text at a time. That line of text will be a file path, call it C:\projects\test.txt.
What's the best way to first read the text from C:\path.txt and then second use the sort command in a batch file to alphabetize the file whose path is defined by the text string in path.txt ?
Lastly, I want to erase the line of text from that C:\path.txt file.
Please let me know if this is too vague or needs a better explanation and thanks in advance.
The batch file I have now reads:
FOR /F "tokens=*" %%i IN (C:\DONOTMODIFY.txt) DO #ECHO %%i
set "filename=%%i"
SORT filename /O filename
Why the artificial limitation of only one line? Anyway yes this is simple enough. Anyway this will process any number of lines in target.txt. Code off my head so test before real use.
#echo off
for /f %%i in (target.txt) do (
sort %%i /o %%i
)
echo. > target.txt
EDIT:
Instead of echo. > target.txt you could use copy /y nul target.txt > nul that actually creates a totally empty file unlike echo. that makes a blank line.
PS: a tip for future questions: Show that you actually tried something. This is not a do my program for you site.

Batch file to delete keyword in text file

I have a text file that looks something like this:
P4657_1
P1352_1
P3126_1
and so on.
I need a batch script that removes the "P" at the beginning and the "_1" at the end.
I found many script examples in this forum but none worked for my needs.
Can anyone help?
sed -E 's/^P//g; s/_1$//g' yourinfile > outfile
by virtue of the first substitution beginning P's get nuked, by the second final _1's get nuked and you are done.
For a list of options for modifying text files, see How can you find and replace text in a file using the Windows command-line environment?.
My favorite option is a hybrid JScript/batch utility I wrote called REPL.BAT. The script does not require installation of any 3rd party executables, and it will run on any modern version of Windows, starting with XP.
Using REPL.BAT, the solution is as simple as:
type "yourFile.txt" | repl "^P(.*)_1$" "$1" >yourFile.txt.new
move /y "yourFile.txt.new" "yourFile.txt" >nul
Thanks to everyone.
I did find a solution elsewhere: http://www.dostips.com/forum/viewtopic.php?f=3&t=1498
the code is very simple:
set "search=P"
set "replace="
for /F "delims=" %%a in (input.txt) DO (
set line=%%a
setlocal EnableDelayedExpansion
>> output2.txt echo(!line:%search%=%replace%!
endlocal
)
You can use echo apparently. So, print your file with type, and for each line, assign the line to a variable, edit the variable, and then echo the variable.

Resources