2017-04-04 22 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