concatenate multiple text files
Moderator: DosItHelp
-
- Posts: 4
- Joined: 29 Jan 2013 19:51
concatenate multiple text files
I have need to concatenate multiple text files, that have been downloaded from FTP site, into one new text file. Then I need to do another FTP to put the concatenated file to a new server and move everything to an archive folder. I am new to batch files and so far all I can find is this code "C:\>type *.txt >> merge.txt " and not sure how to continue. Can I call a concate batch from a FTP batch? Any help would be most appreciated.
Re: concatenate multiple text files
Something like this should function ok.
Code: Select all
@echo off
copy *.txt temp.tmp
ren temp.tmp "All-files.txt"
:: Do your ftp here
move "*.txt" "c:\archive"
Re: concatenate multiple text files
Try This:
You will be asked for a user name and password to the server where you will upload your merged file to when the batch start, then it will concatenate the files and upload it and then move your txt files to the archive folder.
Notes:
> when files moved to the archive folder if there is files with the same name it will ask you to confirm overwrite or it will not be moved.
> you will have to set these 3 variables:
"TXTfiles" --> the location to the FOLDER where your txt files exist.
"ArchiveFolder" --> Archive folder location where you will move your txt files to after you are done.
"server" --> FTP server where you will upload your files to.
It's Not Tested.
Code: Select all
@Echo OFF
Title FTP Upload
:: Settings
SET "TXTfiles=C:\TXTfolders"
SET "ArchiveFolder=C:\Archive"
SET "server=ftp.server.com"
:: Code Start From Here
Echo.
SET "UserName="
SET /P "UserName=Server Username>"
SET "Password="
SET /P "Password=Server Password>"
:: Concatenate TxT files
Copy "%TXTfiles%\*.txt" "%TEMP%\Merged.txt"
:: Upload to FTP Server
Call :FTPscript "%UserName%" "%Password%" "%TEMP%\Merged.txt"
:: Move Txt Files to Archive Folder
Move "%TXTfiles%\*.txt" "%ArchiveFolder%"
:: Remove Meged File
Del /F /Q "%TEMP%\Merged.txt"
Exit /B
:FTPscript
(
ECHO.@ftp -s:"%%~f0"^&GOTO:EOF
ECHO.open %server%
ECHO.%~1
ECHO.%~2
ECHO.verbose
ECHO.PUT "%~3"
ECHO.close
Echo.Bye
)>FTPscript.bat
"FTPscript.bat"
Goto :EOF
Notes:
> when files moved to the archive folder if there is files with the same name it will ask you to confirm overwrite or it will not be moved.
> you will have to set these 3 variables:
"TXTfiles" --> the location to the FOLDER where your txt files exist.
"ArchiveFolder" --> Archive folder location where you will move your txt files to after you are done.
"server" --> FTP server where you will upload your files to.
It's Not Tested.
-
- Posts: 4
- Joined: 29 Jan 2013 19:51
Re: concatenate multiple text files
Thank you abc0502 and foxidrive. I used foxidrive's code and added it to what I currently had for the first FTP I needed to do. With the FTP's I am using text files to pass the commands. Below is what I have and I tied to modify the concat to append a date. This is not working for me as I get error saying parameters are not valid.
rem have a text file with commands for below FTP.
ftp -i -s:C:\PuroFiles\BatchText\PCLusa.txt
@echo on
copy *.txt temp.tmp
set dd = %date%
Ren temp.tmp Puro%dd:~0,2%%dd:~4,2%%dd:~6,2%.txt
:: Do ftp here to AS400P
rem ftp -i -s:C:\PuroFiles\BatchText\AS400_UP.txt
move "*.txt" "\\server\VOL2\SHARE\Local Initiatives\Bartender\Archive_Puro"
move "*.png" "\\server\VOL2\SHARE\Local Initiatives\Bartender\Images"
I have not tested the FTPs yet as I want everything locally to work first. Do I need to strip something from the set date before it will work?
rem have a text file with commands for below FTP.
ftp -i -s:C:\PuroFiles\BatchText\PCLusa.txt
@echo on
copy *.txt temp.tmp
set dd = %date%
Ren temp.tmp Puro%dd:~0,2%%dd:~4,2%%dd:~6,2%.txt
:: Do ftp here to AS400P
rem ftp -i -s:C:\PuroFiles\BatchText\AS400_UP.txt
move "*.txt" "\\server\VOL2\SHARE\Local Initiatives\Bartender\Archive_Puro"
move "*.png" "\\server\VOL2\SHARE\Local Initiatives\Bartender\Images"
I have not tested the FTPs yet as I want everything locally to work first. Do I need to strip something from the set date before it will work?
Re: concatenate multiple text files
you didin't provide the full path when using any of your commands,
The above locations is just as an example
Code: Select all
copy "C:\folder_of_txt_files\*.txt" "D:\temp.tmp"
The above locations is just as an example
Re: concatenate multiple text files
billrozster wrote:set dd = %date%
That is creating a variable called "%dd %" and is adding a space to the start of the date.
Use this:
set dd=%date%
-
- Posts: 4
- Joined: 29 Jan 2013 19:51
Re: concatenate multiple text files
Does it matter when after I do the set dd=%date% that it shows up like this in echo: Thur 01/31/2013. I can only have a 10 character name for AS400 usage. I want the name to be Purommddyy. If I rem out the date part completely it renames the tmp to puro.txt.. One more thing, when the concat takes place it is making all one line with no spaces between each text file data. do I need to do some kind of for loop to get this to place each text file data on a new line? Is there a good tutorial website I can use that can help me understand more of what I am doing. The person that used to do these type of things was let go for downsizing reasons and I am nowheres near his level of expertise.
Re: concatenate multiple text files
File names can't have characters such as "/", if you want the date like this : MMDDYY, use this:
The Copy Command Should work fine, i tested on sample file, but if your text files contain empty lines that you need to preserve "Though the Copy command do that too" , then try this:
Set the Main and Merged Variables to match yours, the Final.txt file will be created you don't have top create it first.
The final.txt file must be in different location, not with other txt files, you can keep it's location as it is, and you are free to change it's name.
This combine function will take each txt file in the folder you set in the main variable and put it all one after the other in the final.txt file that you set in the merged variable.
If you need to add a separator between each merged file change the code above to be like this:
or replace "==End File===================================" with yours
and make sure that the combine function will be after your Exist command in your final batch, it can't be between your commands, always put the functions after the Exist command in your batch and end with Goto :EOF
Code: Select all
SET "DD=%date:~4,2%%date:~7,2%%date:~10,4%"
The Copy Command Should work fine, i tested on sample file, but if your text files contain empty lines that you need to preserve "Though the Copy command do that too" , then try this:
Set the Main and Merged Variables to match yours, the Final.txt file will be created you don't have top create it first.
The final.txt file must be in different location, not with other txt files, you can keep it's location as it is, and you are free to change it's name.
Code: Select all
@echo off
SET "Main=%userprofile%\desktop\a"
SET "Merged=%userprofile%\desktop\Final.txt"
For /F "delims=" %%A in ('DIR /B /A:-D "%main%\*.txt"') Do (
Call :combine "%Main%\%%A" "%Merged%"
)
pause
exit /b
:combine <in_file> <out_file>
:: set start line and end line and file name/location and out file
set "file=%~1"
set /a from=1
set /a till=10000
set "out=%~2"
:: if the end line number is bigger than the real one, it set to it's end number
for /f %%i in ('type "%file%"^|find /v /c ""') do if %till% gtr %%i set /a till=%%i
set /a skip=from-1
setlocal EnableDelayedExpansion
<"!file!" (
for /l %%i in (1 1 %skip%) do set /p "="
for /l %%i in (%from% 1 %till%) do (
set "ln="
set /p "ln="
echo(!ln!
)
)>>"%out%"
endlocal
goto :eof
This combine function will take each txt file in the folder you set in the main variable and put it all one after the other in the final.txt file that you set in the merged variable.
If you need to add a separator between each merged file change the code above to be like this:
Code: Select all
Call :combine "%%A" "%userprofile%\desktop\final.txt"
Echo ==End File===================================>>"%Merged%"
and make sure that the combine function will be after your Exist command in your final batch, it can't be between your commands, always put the functions after the Exist command in your batch and end with Goto :EOF
-
- Posts: 4
- Joined: 29 Jan 2013 19:51
Re: concatenate multiple text files
Awesome abc0502 This is working perfectly. I ha dto remove the pause so it would run without user intervention. It also took me a while to figure out where to put the rest of my code. Thanks again everyone for the help and advice. I really need to find a good tutorial to help me in the future.