#1 25 Feb 2013 14:09

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 331

The plain FOR and FOR /L

After my plays the plain FOR (http://ss64.com/nt/for2.html) , here are some things that I want to add :-)  :
(As the FOR in this case should be used against files , but does not check file existence I'll use it as an iterator through strings)

1. , and ; can be used as delimiters instead of space (also tab):

@echo off
for %%A in (1,2;3) do (
	echo %%~fA
)

output:
C:\1
C:\2
C:\3

2.Quotes also can be used to enclose a file name / string with spaces in it.If you don't want quotes in the result the parameter should be dequoted.
The string in quotes can also contains , and ; :

@echo off
for %%A in ("string with spaces";"file;name with spaces") do (
	echo %%~A
)

output:
string with spaces
file;name with spaces

3.There's no way to print/escape wildcard symbols  if you use the command to process a set of strings:

@echo off
for %%A in ("^?";^*;) do (
	echo %%~A
)

4.As it does not check file existence file modifiers can be used against strings ,but will not produce a correct results:

@echo off
for %%A in (blablablabla) do (
	echo %%~sA
	echo %%~zA
	echo %%~tA
)

These works also with FOR /D

Last edited by npocmaka (06 Sep 2013 15:54)


http://www.facebook.com/npocmaka

Offline

#2 26 Feb 2013 01:41

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 771
Website

Re: The plain FOR and FOR /L

Good points, don't forget that '=' is also a valid delimiter.

Offline

#3 27 Feb 2013 20:53

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 331

Re: The plain FOR and FOR /L

Simon Sheppard wrote:

Good points, don't forget that '=' is also a valid delimiter.

In future I'll not :-)

This made me wonder about FOR /L .And surprisingly this works :-)

for /L %%A in (1=1=5;100) do echo %A

This also was not expected:

 
for /f "delims=" %%C  in (,='echo blabla';;) do (
	echo --%%C--
)

In for /f they are ignored outside of the quotes.

Last edited by npocmaka (27 Feb 2013 21:05)


http://www.facebook.com/npocmaka

Offline

#4 30 Apr 2013 07:33

Aacini
Member
Registered: 05 Dec 2012
Posts: 59

Re: The plain FOR and FOR /L

The standard delimiters in Batch commands are comma, semicolon and equal-sign. These delimiters works at any place where the space works as delimiter (not just in FOR sets), like in CALL or COPY commands, etc. For example:

copy one.txt+two.txt=both.txt
npocmaka wrote:

4.As it does not check file existence file modifiers can be used against strings ,but will not produce a correct results:

@echo off
for %%A in (blablablabla) do (
	echo %%~sA
	echo %%~zA
	echo %%~tA
)

If the FOR set include a wild-card, the FOR process just existent files that match the wild-card. If the FOR set does not include a wild-card, the strings in the set are processed as plain text (and are NOT assumed to be file names):

for %%A in (one two three) do echo %%A

In this case, if a file modifier is used, the string is processed as a file name. If the string have the right format, the requested information is returned:

@echo off
for %%A in (D:\Path1\Path2\Name.Extension) do (
   echo Drive: %%~dA
   echo Path:  %%~pA
   echo Name:  %%~nA
   echo Ext:   %%~xA
)

Any information missed in the string is managed via the standard rules (current drive, current directory, etc). Of course, any information that depends on a physical disk file does not exists in this case (like date or size), so it is expanded as an empty string (that is the correct result).

Last edited by Aacini (30 Apr 2013 07:47)

Offline

#5 01 May 2013 09:10

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 331

Re: The plain FOR and FOR /L

Aacini wrote:

The standard delimiters in Batch commands are comma, semicolon and equal-sign. These delimiters works at any place where the space works as delimiter (not just in FOR sets), like in CALL or COPY commands, etc.

and TAB ,despite it is hard (and funny) to be used in the console.

Aacini wrote:

If the FOR set include a wild-card, the FOR process just existent files that match the wild-card. If the FOR set does not include a wild-card, the strings in the set are processed as plain text (and are NOT assumed to be file names):

Just made few more checks.It's even more selective - it considers as files   only   items that have wildcards , the rest of the items in the set are treated as strings .


http://www.facebook.com/npocmaka

Offline

#6 06 Sep 2013 16:04

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 331

Re: The plain FOR and FOR /L

More experiments with FOR /L.
When arguments are something like NumberString it gets only the number and ignores the string (when the string does not begins with number it's taken for null).At least as long the strings does not contain a standard delimiter.Everything after the third parameter is ignored (except not quoted special symbols )

And this led me to this small trick - getting only the number at the beginning of a given string:

@echo off
set BottleRum=13DeadMen

for /l %%P in (%BottleRum% 1;%BottleRum%) do  set DevilDozen=%%P

echo %DevilDozen%
echo Yo-Ho-Ho!

http://www.facebook.com/npocmaka

Offline

#7 06 Sep 2013 17:29

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 331

Re: The plain FOR and FOR /L

And for /l works with octals and hexadecimals:

for /l %%P in (0xAB 1 0xAB) do   Echo %%P
for /l %%P in (013 1 013) do echo %%P

for /l %%P in (000xAB 1 0000xAB) do   Echo %%P
for /l %%P in (00013 1 00013) do echo %%P

Last edited by npocmaka (06 Sep 2013 21:05)


http://www.facebook.com/npocmaka

Offline

Board footer

Powered by FluxBB