Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the first 3 characters in var in batch file?

Tags:

batch-file

How do I do this? I tried:

set /p var=""
set var=%var:~3%
echo %var%

For example, if I type "Hello World" it should echo "lo World".

Sorry I was too vague. That code isn't really the code but this is:

@echo off
setlocal EnableDelayedExpansion
set /p file=""
set cnt=0
for /F "delims=" %%j in (%file%.txt) do (
  set /A cnt+=1
  set line!cnt!=%%j
)
set cde=0
:code
set /a cde+=1
set line=!line%cde%!
if %line:~0,9% == err echo.%line:~3%
goto code

I was just trying to make it shorter well still showing the error.

like image 739
BBMAN225 Avatar asked Feb 21 '12 04:02

BBMAN225


1 Answers

I just tried it and it works just as you would expect. What are you getting?

C:\>type test.bat
set /p var=""

set var=%var:~3%

echo %var%
C:\>test

C:\>set /p var=""
Hello World

C:\>set var=lo World

C:\>echo lo World
lo World

C:\>

So - it seems like you need possibly two things: 1) Some kind of exit condition from your second loop. Between the :code label and the goto code for when the matching condition is hit (i.e., if %line:~0,3% == err) Not knowing what is desired from your code, I would place something like the following

if %line:~0,3% == err echo.%line:~3% & pause & exit

This will pause and exit when it finds the matching line

2) Some kind of exit condition if you reach the end of the lines and there was no match. My suspicion is this is what is causing the error you are seeing as your input file probably is not satisfying this condition.

like image 169
Steven Schroeder Avatar answered Oct 02 '22 02:10

Steven Schroeder