2017-04-04 25 views
0

我有一个文件夹结构SQL文件类似如下:连接文本批量

C:\Users\Peter\Desktop\SQL_FILES\data_structure\customer1\test.sql 
C:\Users\Peter\Desktop\SQL_FILES\data_structure\customer2\test.sql 
C:\Users\Peter\Desktop\SQL_FILES\data_structure\customer3\test.sql 
C:\Users\Peter\Desktop\SQL_FILES\data_structure\customer4\test.sql 
........ 

我想打一个脚本读取路径(C:\用户\彼得\桌面\ SQL_FILES), 文件名(test.sql)和文本 ,然后连接每个test.sql文件末尾的文本。

请问您能帮我吗?

在此先感谢

:: Hide Command and Set Scope 
@echo off 
setlocal EnableExtensions 
mode 140,50 


set /p AbsolutePath="Enter the path of root folder :" 

echo. 
set /p FileName="Enter the filename with it's extension (ie. test.sql):" 

echo. 
echo Enter your inserts 
echo Press Enter twice when finished 
echo (text may not contain ^<, ^>, ^|, ^&, or un-closed quotes) 

ver > NUL 
set new_line="" 
:still_typing 
set /p new_line=">" 
if errorlevel 1 echo. >> temp.txt & set /p new_line=">" 
if errorlevel 1 echo Sending message. . . & goto done_typing 
echo  %new_line% >> temp.txt 
goto still_typing 

:done_typing 
echo done 

:End 
endlocal 
pause >nul 

==================================== =

例如: 例如文件TEST.SQL包含最初:

INSERT INTO TEST(COL1,COL2,COL3) VALUES(3,4,5); 

和批量的执行之后假设我添加一个空行,并在文本两个插入物:

INSERT INTO TEST(COL1,COL2,COL3) VALUES(3,4,5); 

INSERT INTO TEST(COL1,COL2,COL3) VALUES (1,2,3); 

INSERT INTO TEST(COL1,COL2,COL3) VALUES (2,3,4); 
+1

请提供您的起始数据示例,以及您希望得到结果的示例。 –

+0

更好的动词是'append'而不是'concatenate',但除了将行附加到'temp.txt'(不初始化文件)之外,没有循环来迭代文件夹/文件,并通过复制或'type test .txt >> test.sql' – LotPings

+0

我修改了最初的问题。谢谢 – prokopis

回答

0

下面的批处理文件使用不同的方法来做同样的事情,但以一种更简单的方式。此代码可能会在您希望的任何点进行修改;例如,如果您不希望文件名必须包含通配符。

@echo off 
setlocal 

set /p "AbsolutePath=Enter the path of root folder: " 
echo/ 
set /p "FileName=Enter the filename with a wild-card (ie. test*.sql): " 
echo/ 

echo Enter your inserts 
echo Press Ctrl-Z and Enter when finished 
copy CON temp.txt > NUL 
echo/ 
echo Typing done 
echo/ 

for /R "%AbsolutePath%" %%a in (%FileName%) do type temp.txt >> "%%a" 
+0

非常感谢您的回答,但如果我有像testFile.txt这样的文件,也可以添加文本。 我该如何避免这种情况?我试过test.sql *,它的工作原理.. – prokopis