'How to find specific text with extension in Batch File and then move it
First off, I'm new to batch files.
I want to find all files with extension .txt in a folder and then scan them so that it scans the 5th line of the text file which is in the format:
txt||||open.doc|||
It then fetches this doc file and gives it as output
This is the code I am using:
@echo off
for %%f in (*.txt) do (
if "%%~xf"==".txt" do (
for /f "tokens=1* delims=:img|||" %%a in ('findstr /n .* %%f') do (
if "%%a" equ "4" (
echo.%%b
set str=%%b
set str=%str:~1,-1%
echo str: %str%
)
)
)
)
pause
Solution 1:[1]
if "%%~xf"==".txt" do (
change to
if /I "%%~xf"==".txt" do (
to allow txt in any case.
delims=:img||| means delimiters are :,i,m,g and |. The delimiters are a set, not a string.
Tips : Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces. In comparisons; don't assign a terminal \, space or quotes - build pathnames from the elements - counterintuitively, it is likely to make the process easier.
Use if "thing1" == "thing2" ... to avoid problems caused by spaces in thing1/2.
You need delayedexpansion because str is being varied within the loop. See
Stephan's DELAYEDEXPANSION link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Magoo |
