Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Single-line command

3 views
Skip to first unread message

Pegas...@yahoo.com

unread,
Jun 21, 2008, 3:28:25 AM6/21/08
to
Assuming that the file c:\temp\test.txt contains a single
line of the form

20080621

this batch file will write the string "2008" to the console:

@echo off
for /F %%a in ('type "c:\temp\test.txt"') do set IDate=%%a
echo %IDate:~0,4%

So far so good. I now have a need to generate a one line
command (not a batch file!) that can be invoked from some
other program, to generate the same output. Is this possible?
If so, how?

foxidrive

unread,
Jun 21, 2008, 6:41:46 AM6/21/08
to

for /F %a in ('type "c:\temp\test.txt"') do @set I=%a&@call echo %I:~0,4%

Timo Salmi

unread,
Jun 21, 2008, 6:45:26 AM6/21/08
to
Pegas...@yahoo.com wrote:
> 20080621
> this batch file will write the string "2008" to the console:
> @echo off
> for /F %%a in ('type "c:\temp\test.txt"') do set IDate=%%a
> echo %IDate:~0,4%
>
> So far so good. I now have a need to generate a one line
> command (not a batch file!) that can be invoked from some
> other program, to generate the same output. Is this possible?

Question one. Are SED or GAWK allowed?

Question two. Why is not a CALL to that batch not allowed?

But best, question three. How about the following?
for /F %a in ('type "test.txt"') do @set IDate=%a&@echo %IDate:~0,4%

It outputs 2008

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

foxidrive

unread,
Jun 21, 2008, 7:12:53 AM6/21/08
to
On Sat, 21 Jun 2008 13:45:26 +0300, Timo Salmi <t...@uwasa.fi> wrote:

>Pegas...@yahoo.com wrote:
>> 20080621
>> this batch file will write the string "2008" to the console:
>> @echo off
>> for /F %%a in ('type "c:\temp\test.txt"') do set IDate=%%a
>> echo %IDate:~0,4%
>>
>> So far so good. I now have a need to generate a one line
>> command (not a batch file!) that can be invoked from some
>> other program, to generate the same output. Is this possible?
>

>But best, question three. How about the following?
> for /F %a in ('type "test.txt"') do @set IDate=%a&@echo %IDate:~0,4%
>
>It outputs 2008

So it does. :)

foxidrive

unread,
Jun 21, 2008, 7:16:18 AM6/21/08
to
On Sat, 21 Jun 2008 21:12:53 +1000, foxidrive <got...@woohoo.invalid>
wrote:

I take that back, it doesn't work Timo, unless the line has been run twice
in which case the %IDATE% variable is already set and then it works.

This does work from scratch:

ten.n...@virgin.net

unread,
Jun 21, 2008, 7:35:37 AM6/21/08
to

Why use a command? (i.e. type)

::----- START -----
For /f %# in (C:\temp\test.txt) Do @Set "IDate=%#"&Call Echo/%IDate:~0,4%
::------ END ------

Don't forget if you're wanting to run this from a program you'd likely need
to prefix that line with `%comspec% /c ` or `cmd /c ` too!

Pegas...@yahoo.com

unread,
Jun 21, 2008, 8:16:42 AM6/21/08
to

Thanks for the various helpful replies. The command


for /F %a in ('type "c:\temp\test.txt"') do @set I=%a&@call echo
%I:~0,4%

appears to work nicely.
Third-party tools or batch files are not acceptable in this case
because
of distribution issues.
Am I right in assuming that "call echo" is equivalent to invoking
a secondary command processor?

ten.n...@virgin.net

unread,
Jun 21, 2008, 9:14:28 AM6/21/08
to
On Sat, 21 Jun 2008 12:35:37 +0100, ten.n...@virgin.net wrote:

>
> Why use a command? (i.e. type)
>
>::----- START -----
> For /f %# in (C:\temp\test.txt) Do @Set "IDate=%#"&Call Echo/%IDate:~0,4%
>::------ END ------
>
> Don't forget if you're wanting to run this from a program you'd likely need
> to prefix that line with `%comspec% /c ` or `cmd /c ` too!

Correction, if you need to see the console output, that'd have to be
`%comspec% /k ` or `cmd /k `

ten.n...@virgin.net

unread,
Jun 21, 2008, 9:20:13 AM6/21/08
to
On Sat, 21 Jun 2008 05:16:42 -0700 (PDT), Pegas...@yahoo.com wrote:

>
> Thanks for the various helpful replies. The command
> for /F %a in ('type "c:\temp\test.txt"') do @set I=%a&@call echo
> %I:~0,4%
> appears to work nicely.
> Third-party tools or batch files are not acceptable in this case
> because
> of distribution issues.
> Am I right in assuming that "call echo" is equivalent to invoking
> a secondary command processor?

The `call` bit does, (sort of)!

Don't forget, from my original post in this thread, that the type command
is unnecessary too!

foxidrive

unread,
Jun 21, 2008, 1:41:19 PM6/21/08
to
On Sat, 21 Jun 2008 14:20:13 +0100, ten.n...@virgin.net wrote:

>> Am I right in assuming that "call echo" is equivalent to invoking
>> a secondary command processor?

Yes, it does perform a similar function.

>Don't forget, from my original post in this thread, that the type command
>is unnecessary too!

Actually 'type' is required if the folder or filename has long filename
elements.

ten.n...@virgin.net

unread,
Jun 21, 2008, 1:51:47 PM6/21/08
to

The file name given did not require that, however if such an example had
been supplied, type still wasn't needed.

::----- START -----
For /f usebackq %# in ("C:\temp folder\test file.txt") Do @Set

Norman Bullen

unread,
Jun 21, 2008, 2:34:00 PM6/21/08
to
I think a more precise statement is: type is required if the folder or
filename requires quote marks because of embedded spaces.

And, as ten.nigriv points out, use of "usebackq" solves this problem.

Norm

--
Norm

To reply, change domain to an adult feline.

foxidrive

unread,
Jun 21, 2008, 3:04:04 PM6/21/08
to
On Sat, 21 Jun 2008 18:51:47 +0100, ten.n...@virgin.net wrote:

>>>Don't forget, from my original post in this thread, that the type command
>>>is unnecessary too!
>>
>> Actually 'type' is required if the folder or filename has long filename
>> elements.
>
>The file name given did not require that, however if such an example had
>been supplied, type still wasn't needed.

It required extra syntax than was in your example.

>::----- START -----
> For /f usebackq %# in ("C:\temp folder\test file.txt") Do @Set "IDate=%#"&Call Echo/%IDate:~0,4%
>::------ END ------

That is useful though.

0 new messages