DOS脚本将一个文件读入另一个文件 [英] DOS script to read one file into another
问题描述
我发现无法在托管的sql服务器db上授予BULKADMIN或SYSADMIN角色,并尝试通过创建包含插入语句的.sql文件来绕过批量插入操作. insert语句是使用xls宏创建的,因此我现在正在做一些手动的bodyshopping工作.因此,让我在这里提出问题.
I discovered that I couldnt be granted BULKADMIN or SYSADMIN role on my hosted sql server db and trying to bypass the bulk insert operation by creating a .sql file containing insert statements. The insert statements are created using a xls macro, so theres some bit of manual bodyshopping work that I am doing now. So let me draw the problem here.
我有一个文本文件,其内容如下-
I have a text file with the following contents -
10/05/2011 01:21 PM 1-16332-1008261.psa
10/05/2011 01:21 PM 1-16332-1011698.psa
10/05/2011 01:21 PM 1-16332-1023151.psa
10/05/2011 01:21 PM 1-16332-1035695.psa
10/07/2011 03:36 PM 1-16332-1023193.psa
10/07/2011 03:36 PM 1-16332-1035694.psa
6 File(s) 8,933,754
2 Dir(s) 1,675,268,096 free
我要在最终输出文件中实现的是-
What I want to achieve in my final output file is this -
insert into xyz.abcd values('10/05/2011', '1-16332-1008261.psa');
insert into xyz.abcd values('10/05/2011', '1-16332-1011698.psa');
insert into xyz.abcd values('10/05/2011', '1-16332-1023151.psa');
insert into xyz.abcd values('10/05/2011', '1-16332-1035695.psa');
insert into xyz.abcd values('10/07/2011', '1-16332-1023193.psa');
insert into xyz.abcd values('10/07/2011', '1-16332-1035694.psa');
go
请注意,输入文本文件的最后两行将在我的最终输出sql文件中显示.我现在通过dos脚本自动执行我正在手动执行的xls宏,其中插入xyz.abcd值"被认为是一个常量字符串,该常量在读取文件的实际日期和文件名之前被写入到每个新行中. 我将最终的输出文件命名为.sql,然后从计算机上远程执行该文件. 如果可以在DOS编程中做到这一点,请有人帮助我.
Please take a note that the last two lines from my input text file are to be dinged in my final output sql file. Its kind of automating the xls macro I am manually doing now by dos scripting where the 'insert into xyz.abcd values' is considered to be a constant string which is written to each new line before the actual date and filename from the file been read. I will name my final output file as a .sql and execute it remotely from my computer. Can somebody please help me if this can be doable in DOS programming or not.
我试图在另一篇文章中提出一个类似的问题,但我尝试将其删除,但无法这样做.猜猜我没有正确解释另一个.
I have tried to present a similar problem in another post which I am trying to delete, but am unable to do so. Guess I didnt explain the other one properly.
我知道我可以通过echo命令将字符串作为文字重定向到文件,但是挑战在于如何从一个文件中读取一行的一部分并将其放入新文件中.
I know I can redirect the string as a literal to a file by the echo command but the challenge is how do I read fraction of a line from one file and put them into a new file.
谢谢!
推荐答案
请将以下代码放入记事本,并将其另存为CMD批处理文件(例如Convert2SQLfile.cmd).然后从命令行使用3个参数运行它,同时:
please put following code to notepad and save it as CMD batch file (e.g. Convert2SQLfile.cmd). Then run it from command line with 3 parameters, while:
- 第一个参数包含要从中读取数据的源文件的路径
- 第二个参数包含要将数据保存到的目标文件的路径
- 第3个参数包含应存储在SQL命令中的表名,而不是请求中存储的表名"xyz.abcd"
希望这会有所帮助.
关于,
标准
@echo off
SetLocal
SET App.SourceFile=%1
SET App.DestinationFile=%2
SET App.Table=%3
FOR /f "usebackq tokens=1,4" %%i IN (`type %%App.SourceFile%%`) DO (
IF #%%j# NEQ ## (
IF #%%j# NEQ #free# (
ECHO insert into %App.Table% values ^('%%i', '%%j'^); >> %App.DestinationFile%
)
)
)
ECHO go >> %App.DestinationFile%
这篇关于DOS脚本将一个文件读入另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!