concatenate multiple text files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
billrozster
Posts: 4
Joined: 29 Jan 2013 19:51

concatenate multiple text files

#1 Post by billrozster » 29 Jan 2013 20:17

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: concatenate multiple text files

#2 Post by foxidrive » 29 Jan 2013 23:18

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"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: concatenate multiple text files

#3 Post by abc0502 » 30 Jan 2013 16:40

Try This:

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
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.

billrozster
Posts: 4
Joined: 29 Jan 2013 19:51

Re: concatenate multiple text files

#4 Post by billrozster » 31 Jan 2013 10:43

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?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: concatenate multiple text files

#5 Post by abc0502 » 31 Jan 2013 10:51

you didin't provide the full path when using any of your commands,

Code: Select all

copy "C:\folder_of_txt_files\*.txt" "D:\temp.tmp"

The above locations is just as an example

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: concatenate multiple text files

#6 Post by foxidrive » 31 Jan 2013 13:09

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%

billrozster
Posts: 4
Joined: 29 Jan 2013 19:51

Re: concatenate multiple text files

#7 Post by billrozster » 31 Jan 2013 15:25

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: concatenate multiple text files

#8 Post by abc0502 » 31 Jan 2013 17:28

File names can't have characters such as "/", if you want the date like this : MMDDYY, use this:

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%"
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

billrozster
Posts: 4
Joined: 29 Jan 2013 19:51

Re: concatenate multiple text files

#9 Post by billrozster » 04 Feb 2013 19:40

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.

Post Reply