如何在Windows批处理作业中访问环境变量?

时间:2012-02-09 19:57:05

标签: batch-file cmd

我正在尝试从命令提示符中读取登录cridentials以执行一系列SQL * Loader脚本。
我能够看到使用 set v _ 设置变量。但是,我无法访问这些值。

测试文件:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

实际结果结果:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: ""  
Password: ""  
Database: ""  
v_db   =3  
v_pass =2  
v_user =1  

预期结果结果:

Username: 1    <-- Input
Password: 2    <-- Input
Database: 3    <-- Input
Username: "1"  
Password: "2"  
Database: "3"  
v_db   =3  
v_pass =2  
v_user =1  

预期用途:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T1.ctl' log='logs/TABLE_T1.log' bad='bad/TABLE_T1.bad' skip=1
sqlldr %v_user%/%v_pass%@%v_db% CONTROL='TABLE_T2.ctl' log='logs/TABLE_T2.log' bad='bad/TABLE_T2.bad' skip=1

pause

1 个答案:

答案 0 :(得分:6)

删除set语句中=周围的空格:

echo off

set /P v_user=Username: 
set /P v_pass=Password: 
set /P v_db=Database: 

ECHO Username: "%V_USER%"
ECHO Password: "%V_PASS%"
ECHO Database: "%V_DB%"

set v_

pause

另一种方法是将ECHO语句更改为包含与变量名称和=符号之间使用的SET语句相同数量的尾随空格:

@echo off

set /P v_user = Username: 
set /P v_pass = Password: 
set /P v_db   = Database: 

ECHO Username: "%V_USER %"
ECHO Password: "%V_PASS %"
ECHO Database: "%V_DB   %"

set v_

pause