assign a value and manipulate a variable

February 5, 2010 at 03:21:59
Specs: Windows XP, Core2Duo/2GB
Hello everybody.

I am facing some difficulties while working with a batch script:

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=1 delims= " %%a in (Dir1\lastFileName.txt) do ( sqlldr user/pas@dbpath control=Dir2\Control.ctl log=Dir2\%%a.log data=%%a
)

lastFileName.txt contains a single line, which shows the path to a certain file. (e.g. c:\path\file.type)

No, I want to assign to a variable this path (e.g. var_path==%%a), in order to use it, everywhere in the script.
Moreover, I want to modify this variable a little
(e.g. I want just the last 10 characters)

Please give me some help,

greatfully,
Pupli


See More: assign a value and manipulate a variable

Report •

#1
February 5, 2010 at 03:54:23
Untested:

for /f "tokens=1 delims= " %%a in (Dir1\lastFileName.txt) do (
set var_path_temp=%%a
set var_path=%var_path_temp:~-10%
REM The use %var_path% where-ever needed
sqlldr user/pas@dbpath control=Dir2\Control.ctl log=Dir2\%%a.log data=%%a
)


See "set /?" for more details on the substring part.

Also note my advice to change variables called %%a (or %1 for that matter) to a name that is more indicative for the content of the value. In this case you would need it anyway, because you are performing substrings, but otherwise ...


Report •

#2
February 5, 2010 at 04:07:40
Inside a For loop you can't assign then access a variable without enabling the Delayed Expansion.

so Instead of

set var_path_temp=%%a
set var_path=%var_path_temp:~-10%

use

set var_path_temp=%%a
set var_path=!var_path_temp:~-10!

and at the top of your code don't miss

setlocal EnableDelayedExpansion

@tvc

Precision is the spice of software where "to be closer" has no meaning.


Report •

#3
February 5, 2010 at 04:37:48
> Precision is the spice of software where "to be
> closer" has no meaning.
>

Indeed ... but I said it wasn't tested ... can't test everything ... I know that untested=buggy

Anyway, I was also looking at this ... the substring stuff (the -10 in this example), doesn't work straight on %%a ?!


Report •

Related Solutions

#4
February 5, 2010 at 04:48:04
Thank you to both of you guys,

You really helped me!

thnx again


Report •

#5
February 5, 2010 at 05:54:36
@tvc

Substring manipulation applies to environmental variables only, so to perform it you need always to transform an internal (e.g. %%a) or external (e.g. %1) variable to an environmental one by coding a SET statement.

A further note.

As the file posted in the example is reported to hold just ONE line, the script may be sempllified by coding

set /P var_path=< Dir\LastFileName.txt
set var_path=%var_path:~-10%

avoiding to enable delayed expansion.


Report •

#6
February 5, 2010 at 12:41:47
its a bummer you can't do certain things with primaries that you can do with artificials, and vice versa! you can do %xx:~-1% but not %%a:~-1, and you can do %%~na but not %~nxx%. especially since SET won't allow <,>, or = unless quoted, so if you have incoming xml or html, for example, you have to do a lot of extra work. oh, well.

Report •

Ask Question