Page 1 of 1

Append text in specific files of a folder (given a root path)

Posted: 04 Apr 2017 12:37
by bufoss
Hi all,
I am sorry I am newbie in batch scripting, but I need a script to append text to files.

I have sql files in a folder structure like the following :

C:\Users\George\Desktop\SQL_FILES\data\Folder1\test.sql
C:\Users\George\Desktop\SQL_FILES\data\Folder2\test.sql
C:\Users\George\Desktop\SQL_FILES\data\Folder3\test.sql
C:\Users\George\Desktop\SQL_FILES\data\Folder4\test.sql
........

I want to make a script which reads the path (C:\Users\George\Desktop\SQL_FILES), the name of the file(test.sql) and a text and then append the text in the end of each test.sql file.

Could you help me please ?

Thanks in advance

Code: Select all

:: Hide Command and Set Scope
@echo off
setlocal EnableExtensions

set /p AbsolutePath="Enter the path of root folder :"
set /p FileName="Enter the filename with it's extension (ie. test.sql):"

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 Text initiated. . . & goto append_text
echo      %new_line% >> temp.txt
goto still_typing

:append_text
for .......

:End
endlocal
pause >nul


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

For example : The file test.sql for example contains initially :
INSERT INTO TABLE_TEST(COL1,COL2,COL3) VALUES('a','b','c');

And after the execution of batch supposing I add two empty lines and two inserts in the text :
INSERT INTO TEST(COL1,COL2,COL3) VALUES('a','b','c');


INSERT INTO TEST(COL1,COL2,COL3) VALUES('f','b','c');
INSERT INTO TEST(COL1,COL2,COL3) VALUES('b','g','c');

Re: Append text in specific files of a folder (given a root path)

Posted: 05 Apr 2017 05:50
by SIMMS7400
This does the trick:

Code: Select all

SET PROC_PATH=C:\TEST\

for %%F IN ( %PROC_PATH%*.sql ) DO (

echo.>>%%F
echo.>>%%F
echo INSERT INTO TEST^(COL1,COL2,COL3^) VALUES^('f','b','c'^);>>%%F
echo INSERT INTO TEST^(COL1,COL2,COL3^) VALUES^('b','g','c'^);>>%%F

)

Re: Append text in specific files of a folder (given a root path)

Posted: 05 Apr 2017 06:48
by Aacini
Crossposted in SO. I already provided an answer there.

Antonio