BAT批处理:获取文件名(不含后缀)、执行SQL语句及数据导入

来源:互联网 发布:excel文件恢复软件 编辑:程序博客网 时间:2024/05/01 19:30

最近遇到需要按一目录下每个DMP文件的文件名(不包括后缀)创建数据库用户,并把同名DMP文件中的数据导入相应的各个用户中。

思前想后,决定用批处理来完成这工作。

多个DMP文件我这是放在C:\Users\Administrator\Desktop\数据\

批处理文件和相关文件则是放在了C:\Users\Administrator\Desktop\批处理\

下面是我的BAT文件内容:

@echo offsetlocal enabledelayedexpansionset dir=C:\Users\Administrator\Desktop\数据for /f %%j in ('dir /b C:\Users\Administrator\Desktop\数据\*.DMP') do (for /f "eol=* tokens=*" %%k in (create.sql) do (set a=%%kset "var=!a:123=%%~nj!"echo !var!>>$echo !a!>>#)move $ create.sqlsqlplus /@TEST AS SYSDBA @C:\Users\Administrator\Desktop\批处理\create.sqlset b=%%~nj&imp \"!b!/pass1009@TEST\" fromuser=!b! touser=!b! FILE=!dir!\!b!.DMP LOG=!dir!\!b!.LOGmove # create.sql)echo ---------------------------------------------pause

其中

for /f %%j in ('dir /b C:\Users\Administrator\Desktop\数据\*.DMP') do (......)

这里完成了对目录的整个遍历,并找出.DMP文件,获取文件名至变量J中(这里的j还是带后缀的)
如果想纯粹获取文件名:echo %%~nj(该操作输出不带后缀的文件名)

因为要动态创建用户,所以create.sql中的语句每次循环都需要替换用户名的

for /f "eol=* tokens=*" %%k in (create.sql) do (set a=%%kset "var=!a:123=%%~nj!"echo !var!>>$echo !a!>>#)move $ create.sql

这里我用了123,作为初始语句的用户名。
set”var=!a:123=%%~nj!”就是将存放sql文件内容的变量a中的的123替换为%%~nj中的内容(也就是文件名)后放入变量var
echo !var!>>$ 与 echo !a!>># 则是分别将得到的新数据var与原数据a分别放入临时文件$与#中
move $ create.sql 最终将替换后的内容替换sql文件原有内容

替换后下面该执行创建语句了

sqlplus /@TEST AS SYSDBA @C:\Users\Administrator\Desktop\批处理\create.sql

最后则是收尾导入数据了

set b=%%~nj&imp \"!b!/pass1009@TEST\" fromuser=!b! touser=!b! FILE=!dir!\!b!.DMP LOG=!dir!\!b!.LOGmove # create.sql

其中的&是表示执行完前者后立刻执行后者,而move # create.sql是将更改后的用户名重新替换为123,以便下次循环的使用

下面附上,create.sql内容:

set echo on;set define off;spool '批处理_创建用户.log';create user 123 identified by pass1009 default tablespace TEST temporary tablespace TEST_TEMP;grant unlimited tablespace to 123;alter user 123 quota unlimited on TEST;alter database datafile 'd:\oracletablespace\TEST.DBF'  autoextend on;grant connect to 123 with admin option;grant dba to 123 with admin option;grant exp_full_database to 123 with admin option;grant imp_full_database to 123 with admin option;grant resource to 123 with admin option;grant alter any index to 123 with admin option;grant alter any procedure to 123 with admin option;grant alter any rule set to 123 with admin option;grant alter any table to 123 with admin option;grant alter any type to 123 with admin option;grant debug any procedure to 123 with admin option;grant debug connect session to 123 with admin option;grant delete any table to 123 with admin option;grant drop any sequence to 123 with admin option;grant drop any table to 123 with admin option;grant export full database to 123 with admin option;grant import full database to 123 with admin option;grant insert any table to 123 with admin option;grant select any dictionary to 123 with admin option;grant select any table to 123 with admin option;grant select any transaction to 123 with admin option;grant under any table to 123 with admin option;grant unlimited tablespace to 123 with admin option;spool off;exit;
0 0
原创粉丝点击