Trying to use the directory outputed by this line in my batch code.
findstr /M search_text "C:\Users\user\Desktop\*"
This output's a file directory.
"C:\Users\whatever\blabla"
I know I can just add >> and print it to a text file but I'd rather not go that route as I'm not sure how to pull the dir back into the command line in the first place. Is there somehow I can do like a Set=%k command to a variable for later use. Sorry I'm still very new to this. Thanks for any help!
You can do this
for /f %%i in ('findstr /m "search_text" "C:\Users\user\Desktop\*"') do set file=%%i
Then you have the output in the %file% variable.
Hope this helps.
Related
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 %
I have a file called xxxxxxx_12345.pdf and I try to delete the suffix and the result would be xxxxxxx.pdf.
I tried following:
forfiles /S /M *_12545.pdf /C "cmd /c rename #file #fname*.pdf"
but it could not change the file name.
Can somone help to solve this issue?
thanks
I'm sure there is a way to do the job with forfiles command, however, it would be much easier to use regular for loop for this.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
for %%f in (*_12545.pdf) do (
set "CurrentFileName=%%~nf"
set "RenameTo=!CurrentFileName:~0,-6!"
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
)
pause>nul
By setLocal EnableDelayedExpansion, user can use delayed environment variables, which will enable user to use something like !variable! instead of %variable%.
set "CurrentFileName=%%~nf"
%%~nf will give you the name of the file without extension, so in this case, CurrentFileName will be FileName_12545. Type for /? to see the full explanation of the syntax.
set "RenameTo=!CurrentFileName:~0,-6!"
Notice that I used !variable:~0,-6! to remove suffix. This is String Manipulation. The user is storing a string without suffix that has a length of 6 (which is _12545 in this case).
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
I placed echo in front of ren so you can check before you actually rename it. %%~xf will return the file extension including .(dot) in front of it. In this case, the file extension is .pdf.
I hope this help a bit.
I am in the need of a batch script that checks a drive (D:) for the 'last modified' attribute of *.czi files (Carl Zeiss image files) and append the data to a file on another drive. I have tried solutions with the following line:
FOR /F "delims=" %%I IN ('DIR %source%*.czi /A:-D /O:-D /T:W /B') DO COPY "%%I" > %target%
that does give me the last file, but it copies the entire file which is not that smart since they can be big. As a biologist I will spare you for my desperate attempts that did not work (spent 4-5 hours). I figure this can be done dead easily, that is if you know how... Any good suggestions? Any reply will be appreciated! Thanks in advance.
Let's assume just the last modified file time from newest file is wanted from all *.czi files in directory C:\Temp containing for example:
30.01.2017 08:13 First Listed.czi
28.01.2017 21:26 Oldest Image.czi
03.02.2017 17:13 Newest Image.czi
The batch code for this task could be:
#echo off
set "Source=C:\Temp\"
set "Target=%Source%List.txt"
for /F "delims=" %%I in ('dir "%Source%*.czi" /A:-D /B /O:-D /T:W 2^>nul') do (
echo File "%%I" last modified on %%~tI>>"%Target%"
goto AfterLoop
)
:AfterLoop
The command DIR searches in for *.czi files in directory C:\Temp and outputs the list sorted by last modification time in reverse order from newest to oldest.
In case of no *.czi file could be found, command DIR would output an error message to handle STDERR. This output message is redirected with 2>nul to device NUL to suppress it whereby the redirection operator > must be escaped here with ^ to be interpreted as redirection operator on execution of DIR command line and not already on parsing FOR command line.
%%I references the name of the file as output by DIR and %%~tI references the last modification date of the file. The help of command FOR output by running in a command prompt window for /? explains those modifiers.
The loop is exited after first output of the text redirected to the target file on which the line is appended if it is already existing because of using >> instead of just >.
For the example files list the following line is appended to C:\Temp\List.txt:
File "Newest Image.czi" last modified on 03.02.2017 17:13
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
for /?
goto /?
set /?
See also the Microsoft article Using command redirection operators.
Your question is unclear, so let me try to rephrase it:
I think you want to find the Most Recently Modified file with a .CZI extension, and copy only that newest file to some target destination.
To list all .CZI files in all subdirectories, sorted by newest-file first:
(for /f %a in ('dir *.CZI /s /b') do #echo %~ta %~fa) | sort
If the first line of this output is the file that you want, then all you need to do is copy that one file to your target.
(and please, take the time to write detailed and clear questions so we can provide good answers)
I am extracting the findstr results as a text file, but i don't know how to delete the files which are extracted as the result by findstr ?
My code is like:
#echo off
findstr /s /m "del" "C:\*.*">>"result.txt" 2>nul
Now, i want to delete the files which are printed as a name on result.txt in the batch file automatically.
Please Help Me !
Echoing #Stephan's comment above, make very certain that you want to delete those files. I'm guessing you will get some files you didn't want and might make your system unstable. But in the spirit of answering a question... try something like the following:
for /f %%i in (result.txt) do del %%i
first, take a look to your "result.txt". I'm quite sure, you will be
surprised... Are you really sure, you want to delete them all? –
Stephan 54 mins ago
.
Yes ! I want to delete all files which are printed on result.txt...
Please Help Me ! – Ekagra Srivastava 4
Well, you have been warned...
for /f "delims=" %%i in (result.txt) do del "%%i"
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.