Reputation: 646
I have a text file named paths.txt It contains the following data,
LocalName RemotePath
N: \DANIEL-HP\Users\Public
Z: \DANIEL-HP\Users\Public\Favorites
LocalName RemotePath
N: \DANIEL-HP\Users\Public
Z: \DANIEL-HP\Users\Public\Favorites
I am running the following batch file to extract the desired info path letter, path.
@echo off
for /f "tokens=1 delims=" %%a in ('find ":" paths.txt') do (
echo %%a
)
pause
The result
---------- PATHS.TXT
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
Press any key to continue . . .
The way that I fix this is I modify the FOR as below,
for /f "skip=2 tokens=1 delims=" %%a in ('find ":" paths.txt') do (echo %%a)
And I get a clean desired output,
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
N: \\DANIEL-HP\Users\Public
Z: \\DANIEL-HP\Users\Public\Favorites
Press any key to continue . . .
Question is, what is wrong with my first FOR function that it is allowing the filename to passthrough.
Upvotes: 1
Views: 88
Reputation: 130819
The problem is your FIND command, not your FOR /F.
FIND always prints out the name of the file like that if it opens the file itself (if the file name is passed in as an argument).
You can avoid the file name by using redirection:
find ":" <paths.txt
or a pipe:
type paths.txt | find ":"
To use these in a FOR loop, the pipe or redirection has to be escaped.
for /f "tokens=1 delims=" %%a in ('find ":" ^< paths.txt') do echo %%a
for /f "tokens=1 delims=" %%a in ('type paths.txt ^| find ":"') do echo %%a
Another alternative is to use FINDSTR instead (but only if the file is ASCII):
findstr ":" paths.txt
If the file is unicode (UTF-16), then you can convert the content to ASCII by piping the output of the TYPE command (again, escape the pipe if used within FOR /F):
type paths.txt | findstr ":"
Upvotes: 3
Reputation: 61
need to be deleted ;))
/* i advice:
find ":" ^<paths.txt
type paths.txt ^| find ":"
findstr working great.. i not understand what the blank you have
@echo off
set "tmp=PATHS.TXT"----your value
echo,---findstr
for /f "tokens=1 delims=" %%a in ('findstr ":" %tmp%') do (echo %%a)
echo,---type
for /f "tokens=1 delims=" %%a in ('type %tmp% ^| find ":"') do (echo %%a)
echo,---redirect
for /f "tokens=1 delims=" %%a in ('find ":" ^<%tmp%') do (echo %%a)
*/
Upvotes: 1