How can I run this batch script on filenames with space and "(" ")"?
:Start
#Echo off
Set _SourcePath=C:\tifs\*.tif
Set _OutputPath=C:\txts\
Set _Tesseract="C:\Program Files\Tesseract-OCR\tesseract.exe"
:Convert
For /F "usebackq delims=" %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
You have 2 problems:
1) You need some additional quotes.
2) You are using the wrong form of FOR. Your code is using the /F option with an unquoted IN() cluase. This attempts to read the contents of a file, which can't possibly work because your name includes a wildcard. I think you want a listing of .TIF files which is best done using the simple form of FOR (no /F option).
for %%A in (%_SourcePath%) do echo Converting "%%A"...&%_Tesseract% "%%A" "%_OutputPath%%%~nA"
I would change it to something like this:
:Start
#Echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set _SourcePath=C:\tifs\*.tif
Set _OutputPath=C:\txts\
Set "_Tesseract=C:\Program Files\Tesseract-OCR\tesseract.exe"
:Convert
For /F "usebackq delims=" %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% "%%A" "%_OutputPath%%%~nA"
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Now, my answer might not work but I think it might give you enough hints to figure it out.
Related
Hi I don't have much experience in batch-programming and have a problem. I have a .bat script that reads a file with a list of paths and i want to get the filename of these paths. I use the script in cygwin.
My code in the Script:
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("%value%") DO SET MYPATHFILE=%%~nxI
)
When i run the Script %value% is empty.
Value of error1.txt:
a/b/c/d/TextIWant
you need delayed expansion or you can directly use %%a:
for /F %%a in (error1.txt) do (
FOR /F %%I IN ("%%a") DO SET MYPATHFILE=%%~nxI
)
or
setlocal enableDelayedExpansion
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("!value!") DO SET MYPATHFILE=%%~nxI
)
It looks like you will need Delayed Expansion.
The current problem is, that you want to use a variable in the same set of brackets where you changed the value in (the surrounding For-Loop).
Add setlocal EnableDelayedExpansion to your code at the top and change the %value% to !value!
You can test the problem yourself with this code:
#echo off
setlocal EnableDelayedExpansion
set foo=bar
For /L %%a (1,2,1) do (
set foo=foobar
echo.old value %foo%
echo.new value !foo!
)
I hope it helped :)
Greetings
geisterfurz007
I'm trying to write a very simple batch file for personal use...It's complete except for one thing I'm stumped on. Hopefully this is an easy fix (I'm effectively illiterate when it comes to code).
Basically what I'm trying to do is have the script choose a random line from a text file, do this a couple times with a couple different text files, then I wish to assign the output from each text file to a variable so that I can easily use them in various combinations...then repeat the process.
Here is what I have right now...
#ECHO OFF
:START
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A list1random=(%RANDOM% %% %NumLines%)
IF "%list1random%"=="0" (SET "list1random=") ELSE (SET "list1random=skip=%list1random%")
FOR /F "usebackq tokens=* %list1random% delims=" %%A IN (`TYPE %list1%`) DO (
>> output.txt ECHO %%A
)
:Finish
ENDLOCAL
GOTO START`
This procures the random line, and spits it to a text file. All is well, next step, take that random result and assign it to a variable...
#ECHO OFF
:START
SETLOCAL
SETLOCAL EnableDelayedExpansion EnableExtensions
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A list1random=(%RANDOM% %% %NumLines%)
IF "%list1random%"=="0" (SET "list1random=") ELSE (SET "list1random=skip=%list1random%")
FOR /F "usebackq tokens=* %list1random% delims=" %%A IN (`TYPE %list1%`) DO (
SET output1=%%A
)
>> output.txt ECHO %output1%
:Finish
ENDLOCAL
GOTO START
Now the output ceases to be random...instead it is always the last line of the referenced text file.
EDIT: The site suggested another question that was similar to mine. However, that person was having trouble getting the script to choose a valid line. I get a valid line every time, and a random one too (when I check it via echo), but a non-random line when proceeding on, assigning the output to a variable. I don't understand because it seems like a post-facto derandomization. I.E. the difference between the two scripts has nothing to do with procuring the random result, only what to do with that result AFTER it has it, right?
I appreciate any help in advance, this is the last step before I know everything I need to finish this, I'm excited!
Sorry, you're right...anyways, I figured out a simple workaround, probably not the quickest in terms of processing time, but whatever. Basically allow the initial part of the script to spit out the random result to a text file (as seemed to work just fine) then reference the text file as a variable.
#ECHO OFF
:START
SET "list1=list1.txt"
FOR /f %%a IN ('type "%list1%"^|find /c /v ""') DO SET /a numlines=%%a
SET /A listchoice=(%RANDOM% %% %NumLines%)
IF "%listchoice%"=="0" (SET "listchoice=") ELSE (SET "listchoice=skip=%listchoice%")
FOR /F "usebackq tokens=* %listchoice% delims=" %%A IN (`TYPE %list1%`) DO (
>> listoutput.txt ECHO %%A
)
Set /p list=<listoutput.txt
>> result.txt ECHO %list%
:Finish
DEL listoutput.txt
GOTO START
This is easy to do in PowerShell using the built-in Get-Random cmdlet.
$line = (Get-Content file.txt | where { $_ } | Get-Random)
Which makes it also easy in batch.
set filename=file.txt
for /f "tokens=*" %%a in ('powershell -ex bypass -c "gc %filename% | ? { $_ } | Get-Random"') do (
set "var=%%a"
)
The where { $_ } clause is only necessary to filter out any blank lines. You can omit it if you know your file has none.
I know there have been previous posts about this but none of them have worked for me. I want to do a find and replace string in a text file using the windows command prompt. No parameters, all hard coded. Here is what I have so far:
..........
setlocal enabledelayedexpansion
set SEARCHTEXT=oldtext
set REPLACETEXT=newtext
for /f "tokens=1 delims=" %A in ( C:\in.txt) do (
set string=%A
echo set string:%SEARCHTEXT%=%REPLACETEXT% >> C:\out.txt)
..............
This code just writes "set string:oldtext=newtext" to out.txt for each line in in.txt.
How can I get it to actually replace oldtext with newtext?
Thanks.
Test this:
#echo off
setlocal enabledelayedexpansion
set "SEARCHTEXT=oldtext"
set "REPLACETEXT=newtext"
for /f "usebackq delims=" %%A in ("C:\in.txt") do (
set "string=%%A"
set "string=!string:%SEARCHTEXT%=%REPLACETEXT%!"
>>"C:\out.txt" echo !string!
)
remove the echo.
It is there for testing the code without really destroying anything.
If the output is what you need, just remove it.
EDIT: ah wait - there is a logical failure in the code. It should obviously look like:
...
for /f "tokens=1 delims=" %%A in ( C:\in.txt) do (
set string=%%A
set string=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !string!>> C:\out.txt
)
The task is to iterate through each line in a file named alts.txt. Then I grab the line and split it at the semicolon and print out the text before the semicolon and after the semicolon.
My file looks something like this...
username:password
username2:password2
username3:
My current code is this:
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=*" %%A in (%file%) do (
set str=%%A
set "username=%str::="^&REM #%
set "pass=%str:*:=%"
echo username=%username% pass=%pass%
)
pause
If someone would be kind enough to show me my error and EXACTLY how to fix the error it would be greatly appreciated.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=1,2 delims=:" %%A in (%file%) do (
set "$user=%%A"
set "$pass=%%B"
echo username=!$user! pass=!$pass!
)
pause
Be careful using %username%. It's a system variable. You can test writing echo %username% in the CMD prompt. You better choose another name for the Variable like i did.
How you can read a file (text or binary) from a batch file? There is a way to read it in a binary mode or text mode?
Under NT-style cmd.exe, you can loop through the lines of a text file with
FOR /F %%i IN (file.txt) DO #echo %%i
Type "help for" on the command prompt for more information. (don't know if that works in whatever "DOS" you are using)
The FOR-LOOP generally works, but there are some issues.
The FOR doesn't accept empty lines and lines with more than ~8190 are problematic.
The expansion works only reliable, if the delayed expansion is disabled.
Detection of CR/LF versus single LF seems also a little bit complicated.
Also NUL characters are problematic, as a FOR-Loop immediatly cancels the reading.
Direct binary reading seems therefore nearly impossible.
The problem with empty lines can be solved with a trick. Prefix each line with a line number, using the findstr command, and after reading, remove the prefix.
#echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!"
echo(!var!
ENDLOCAL
)
Toggling between enable and disabled delayed expansion is neccessary for the safe working with strings, like ! or ^^^xy!z.
That's because the line set "var=%%a" is only safe with DisabledDelayedExpansion, else exclamation marks are removed and the carets are used as (secondary) escape characters and they are removed too.
But using the variable var is only safe with EnabledDelayedExpansion, as even a call %%var%% will fail with content like "&"&.
EDIT: Added set/p variant
There is a second way of reading a file with set /p, the only disadvantages are that it is limited to ~1024 characters per line and it removes control characters at the line end.
But the advantage is, you didn't need the delayed toggling and it's easier to store values in variables
#echo off
setlocal EnableDelayedExpansion
set "file=%~1"
for /f "delims=" %%n in ('find /c /v "" %file%') do set "len=%%n"
set "len=!len:*: =!"
<%file% (
for /l %%l in (1 1 !len!) do (
set "line="
set /p "line="
echo(!line!
)
)
For reading it "binary" into a hex-representation
You could look at SO: converting a binary file to HEX representation using batch file
You can use the for command:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do #echo %i %j %k
Type
for /?
at the command prompt. Also, you can parse ini files!
One very easy way to do it is use the following command:
set /p mytextfile=< %pathtotextfile%\textfile.txt
echo %mytextfile%
This will only display the first line of text in a text file. The other way you can do it is use the following command:
type %pathtotextfile%\textfile.txt
This will put all the data in the text file on the screen. Hope this helps!
settings.ini
name="John"
lastName="Doe"
script.bat
#echo off
for /f "tokens=1,2 delims==" %%a in (settings.ini) do (
if %%a==name set %%a=%%b
if %%a==lastName set %%a=%%b
)
echo %name% %lastName%
Well theres a lot of different ways but if you only want to DISPLAY the text and not STORE it anywhere then you just use: findstr /v "randomtextthatnoonewilluse" filename.txt
Corrected code :
setlocal enabledelayedexpansion
for /f "usebackq eol= tokens=* delims= " %%a in (`findstr /n ^^^^ "name with spaces.txt"`) do (
set line=%%a
set "line=!line:*:=!"
echo(!line!
)
endlocal
pause