I am trying to use Tesseract-OCR to read and OCR all .png files, not only in current folder, (as there is answer for that) but also in all subfolders.
This works for folder:
for %%A in ("C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png") do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~fA" "%%~dpnxA"
I tried with this to go through all subfolders that I have in "temp" folder:
(for /r %%a in (*.png) do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~nxa" "%%~dpnxA")
but I got this errors for every file:
C:\Users\x\AppData\Local\Tesseract-OCR\temp>C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "01.png" "%~dpnxA"
Tesseract Open Source OCR Engine v4.1.0-elag2019 with Leptonica
Error, cannot read input file 01.png: No such file or directory
Error during processing.
It is obvious that the script finds all files in all of the subfolders, but then it cant read then for some reason?
Also, this script works for one folder, but when I try to use with /r it doesnt go through all subfolders:
:Start
#Echo off
Set _SourcePath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png
Set _OutputPath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\
Set _Tesseract="C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Any ideas?
Perhaps this sort of thing is what you're looking for:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_OutputPath=%LocalAppData%\Tesseract-OCR\temp"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%_OutputPath%\%%~nA"
Note, this assumes that tesseract allows for specifying the output directory and accepts doublequoted strings etc. It also assumes that you intend for all output files to be placed in %_OutputPath%.
If you wanted them to be placed along side their respective .png's then perhaps this will do it:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%%~nA"
Related
I want to loop through a folder and let run an algorithm on each .tif file found. Unfortunately this does not work for files which have a space character in their name. As my path already contains folders with space, i put the variable which stores the path name in double-quotation marks.
FOR /F %%k IN ('DIR /B "%mypath_import%"*.tif') DO (
SET infile=%%k
SET outfile=!infile:.tif=_UTM.tif!
REM ... do something
This is my attempt so far but it won't work for the files which include a space as well.
You done need all that. You can use the normal for loop without having to use /f
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~i"
echo "!infile:.tif=UTM.tif!"
)
The above will however echo full path to and file name, if you want filename only with extension:
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~nxi"
echo "!infile:.tif=UTM.tif!"
)
or without the need to delayedexpansion
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~dpni_UTM%%~xi
and again if you require the name and extension only.
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~ni_UTM%%~xi
EDIT
As per comment from #Stephan, keep in mind if you are doing actual renames and you run the script more than once it will keep on appending _UTM each time. So you'll get filename_UTM_UTM.tif etc. So you can exclude files from the loop by including findstr
for /f "delims=" %%i in ('dir /b *.tif ^|findstr /eiv "_UTM.tif"') do echo %%~ni_UTM%%~xi
I have a batch file for project backup, that i want to modify to exclude unnecessary files.
My current file uses simple 7z command to pack whole folder into one archive:
!7zipPath! a -t7z "archive_name" "path_to_add".
From what i found in other people questions, to exclude given files, i can use flag, for example -x!.txt, or -xr!.txt if path consist subfolders.
!7zipPath! a -t7z "archive_name" "path_to_add" -xr!*.jt
This should exclude all files with .jt extension (software specific extension), but -x!*.jt returns "incorrect wildcard typemaker *.jt
So from what i understand 7zip does not know this extension so i can not use it (is that correct thinking?)
So instead i tried to use include only. Answers from other question says that it should be simply added like this:
!7zipPath! a -t7z "archive_name" "path_to_add" ./*.xml
In my understanding this should create archive that includes only .xml files while keeping structure, but it just ignores that wildcard and put all the files from the path. It does not return any kind of error.
How can i use 7zip command options in batch file 7z command to get expected results?
PS; Will it work with extension on a folder? So that it would include whole folder.cojt for example?
EDIT: Full code
#echo off
setlocal EnableDelayedExpansion
SETLOCAL ENABLEEXTENSIONS
rem Get the time from WMI - at least that's a format we can work with
set X=
for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined X set X=%%x
rem dissect into parts
set DATE.YEAR=%X:~0,4%
set DATE.MONTH=%X:~4,2%
set DATE.DAY=%X:~6,2%
echo Today is: %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
if not exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Making Backup
For /F "Delims=" %%A In ('where /r "C:\Program Files\7-Zip" /f 7z.exe') do Set zipPath=%%A
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r C:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r D:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo 7zip not found on this PC
echo You can close this window now
cmd /k
)
md %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
cd %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
!zipPath! a -t7z "%DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%_TuneCells" "M:\01\TuneCells\"
)
if exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Backup already created %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
)
PAUSE
As mentioned in one of commends, the search for 7z is there so that multiple people can easily use the code regardless of their installation path preferences (which are surprisingly wide)
I have a folder containing many files named as such: JBMA_23456.docx, JMRI_21456.docx, CM_22554.docx, QUA_11224.docx. How do I create a sub-folder for each file bearing the same name as the file but without the .docx file extension? Additionally, I want to store the filename only as variables.
For example, I need to create a sub-folder named JBMA_23456 from the document
JBMA_23456.docx. Can anyone point me in teh right direction?
#echo off
for %%A in (*.docx) do if not exist "%%~nA" md "%%~nA"
This creates a folder with the same name as each .docx file.
View modifiers in for /? or in call /?. The n modifier is the name.
Path modifiers:
dpnx is drive, path, name and extension.
Here's an example batch file which attempts to perform the tasks as laid out in your question:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=C:\Users\Compo\Desktop\test"
Set "FileExt=.docx"
For /F "Delims==" %%A In ('Set var[ 2^>Nul') Do Set "%%A="
Set "i=0"
For /F "Delims=" %%A In ('Where "%BaseDir%":*%FileExt% 2^>Nul') Do (Set /A i+=1
Call Set "var[%%i%%]=%%~nA"
If Exist "%%~dpA%%~nA\" (Echo Sub-Folder %%~nA already exists in %BaseDir%
) Else (Set /P "=Creating sub-folder %%~nA in %BaseDir%"<Nul
MD "%%~dpA%%~nA">Nul 2>&1 && (Echo= was successful) || Echo= failed))
Set var[ 2>Nul
Pause
In order to use it, you would first ensure that the directory holding your files, (excluding any trailing backslash), is placed between the = and " on line 4, and the single file extension, (including the leading period, .), similarly on line 5.
It is not entirely clear what you are asking for...
Anyway, here is the code i made
echo off
chcp 65001
cls
for /f "usebackq delims=." %%0 in (`dir /b "*.docx"`) do (
set filename=%%0
md %filename%
)
cmd /k
It creates a new folder for every file. Feel free to ask if this was not what you expected
I am trying to write a batch file to find and replace a string in multiple files within a folder. But I am getting this error:
Cannot perform a cyclic copy
Any idea why that happens?
#echo off
SETLOCAL
for %%* in (.) do set foldername=%%~n*
SET stringtofindreplace=XXXX
for %%f in (*.fmw) do (
echo Processing %%f...
fOR /F "delims=" %%l IN (%%f) DO (
SET "line=%%l"
SETLOCAL ENABLEDELAYEDEXPANSION
set "x=!line:%stringtofindreplace%=%foldername%!"
echo(!x!
ENDLOCAL)
)>%%~nf.new
)
GOTO:EOF
#ECHO OFF
SETLOCAL
:: no idea what this is aimed at doing...??
for %%* in (.) do set new=%%~n*
SET new=newstring
SET old=XXXX
for %%f in (*.fmw) do (
echo Processing %%f...
(
FOR /F "delims=" %%l IN (%%f) DO (
SET "line=%%l"
SETLOCAL ENABLEDELAYEDEXPANSION
set "x=!line:%old%=%new%!"
ECHO(!x!
ENDLOCAL
)
)>%%~nf.new
)
GOTO :EOF
I've no idea what you are trying to do with the first for, so I just made an obvious replacement string.
You need to add the "delims=" option to deliver the entire line to %%l.
Make sure there are no trailing spaces on the ECHO(!x!
This will make a new file called *.new from each *.fmw file.
Cannot perform a cyclic copy error occurs when the source folder includes the target folder, and so is trying to copy all the files, including the files it has already copied.
This will give you that error.
xcopy c:\apple\*.* c:\apple\backup\ /s
I know that there are several posts adressing this issue already. However I can't get my little batch script to work and I am a newbie so I would be very pleased if u could help me to solve that.
I have a bunch of pdf files named with a random number and "_text" e.g. 174098_text.pdf. Now I want to rename the file such that I only have 174098.pdf left (remove _text).
Here is my latest version of my file "Rename.cmd":
#echo off
#setlocal
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM ++++++++ Umbenennen von Dateien ++++++++++++++++
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM +++ Dateinamen und Pfad ermitteln
FOR /f "delims=" %%D in ('Dir /b %Path%\*_*.pdf') do (
FOR /f "delims=_ tokens=1-2" %%I in ('%%D') do (
ren %%D %%I.pdf
)
)
Endlocal
I hope you can help me and explain me what i have done wrong. Running the code it opens all the files but dosn't rename a single one of it.
/F parameter is for OPENING commands and/or get the output of a command, so when you use the second for /F you are telling the CMD to execute "%%D" file, so can't work that.
You can use a FOR without parameters, or a /R parameter if you need recursively a folder.
And you don't need to start setlocal for this job
#echo off
FOR %%# in ("C:\Folder\*.pdf") DO (
:: Set the "filename.extension"
Set "File=%%~nx#"
:: Rename it
Call Rename "%%#" "%%FILE:_TEXT=%%"
REM Explanation:
REM Rename "filename_text.pdf" with "Filename(_Text=NOTHING).pdf"
REM (That removes the "_TEXT" pattern in each filename)
)
pause&Exit
Remember, if you need recursive:
FOR /R "C:\Folder\" %%# in (*.pdf)