1

This is my first post and I am not very skilled with batch so sorry if I really mess up.

Basically I'm working on a little batch script where once run, the user inputs a file path and line number and the specified line of the specified file will be output to the command line. I have all my variables and commands working, and the command to specify the line of the text file works fine, its just when I put my variables in it doesn't work. Now I'm guessing what I'm doing is obviously wrong since I'm new to batch, but anyway here's my code:

@echo off    
color b      
:top     
title specified line copy tool    
echo input full path to txt file    
set /P filepath=">"    
cls    
echo what line would you like to copy?    
set /P lineoriginal=">"    
set /A actualline=%lineoriginal%-1   
for /F "skip=%actualline% delims=" %%i in (%filepath%) do if not defined output     set "output=%%i"    
echo %output%    
pause  

See if you can see what I did wrong, thanks.

Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • 2
    Ah, the classic "it doesn't work" error. What _is_ it doing, instead? – SomethingDark Sep 10 '16 at 22:48
  • If I give it a quoted filename, as needs to be done if it contains spaces, then it is no longer a filepath but a string. If this is the case the use `UseBackq` option `usebackq - specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in file-set.` As a quoted string is only one line the skip commands skips it which is why `%output%` is blank. Also when debugging remove any `echo off` from the file as it hides errors. –  Sep 10 '16 at 22:59
  • @SomethingDark when the for loop runs, it says delims=" was unexpected at this time. – t.anonymous Sep 10 '16 at 23:21

1 Answers1

0

From this link Windows Batch file to echo a specific line number

you can call this function like that : Call:ReadNthLine <File> <nLine>

:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO ECHO.%%B
EXIT /b

And your code can be re-written like that :

@echo off
color b
:top
cls
title specified line copy tool
echo input full path to txt file
set /P "filepath=>"
cls    
echo what line would you like to copy ?    
set /P "nLine=>"
cls
CALL :ReadNthLine "%filepath%" %nLine%
PAUSE >NUL & goto:top
GOTO :EOF
::***************************************************************************************
:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO ECHO.%%B
EXIT /b
::***************************************************************************************
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70