批量连接文本

时间:2017-04-04 16:49:07

标签: batch-file

我在文件夹结构中有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:\ Users \ Peter \ Desktop \ 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 :(得分: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"