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?
for /F %a in ('type "c:\temp\test.txt"') do @set I=%a&@call echo %I:~0,4%
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
>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. :)
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:
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!
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?
>
> 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 `
>
> 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!
>> 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.
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
And, as ten.nigriv points out, use of "usebackq" solves this problem.
Norm
--
Norm
To reply, change domain to an adult feline.
>>>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.