Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does FOR /F "skip=0" fail?

Tags:

batch-file

cmd

In a standard windows batch file (.cmd) I want to do:

FOR /F "skip=0" %%G IN (filename.txt) DO ( ECHO %%G )

but I get " was unexpected at this time.

"skip=1" works fine (In my actual code 0 is a variable).

According to ss64.com the default is skip=0, but it seems not to work when explicitly set.

Am I doing it wrong? Or is there a workaround I can use?

Edit:

I have tried this on both Windows server 2003 and Windows 7.

The content of filename.txt could be:

something
stuff
unicorns
like image 334
steenhulthin Avatar asked Jun 23 '11 10:06

steenhulthin


1 Answers

It really doesn't like the 0, causing the parser to expect more after it (You can also trip it when trying to use 09 which it tries parsing as octal, which fails).

I guess you need to create an environment variable holding the entire skip=n part or nothing and insert that into the argument list. Something like

if %N% GTR 0 (
  set SKIP="skip=%N%"
) else (
  set SKIP=
)

and then use

for /f %SKIP% %%G ...

(or without the quotes if you need to pass more options).

like image 135
Joey Avatar answered Sep 29 '22 06:09

Joey