How to combine .txt and create a new folder

Posted on 2016-09-23
Last Modified: 2016-10-12
When I run this batch file:

@echo off
setlocal enabledelayedexpansion

set FNSrcPatt=c:\folder\*.txt
set FNOut=c:\TRA_Files\TR2016.tra
for %%a in ("%FNSrcPatt%") do (type %%a>>"%FNOut%")

the old .txt files are left behind in c:\folder\. Is there a way, keeping the script as is, just adding a way for me to specify a folder that those .txt files are put...and have a folder created with it date and time stamped? So, using what you suggested, but then adding code where I specify where to create a new folder, the title of the folder is the current date and time, then the .txt files are moved to the newly created folder. Does that make sense? Thanks!
Question by:brasiman
  • 3
LVL 24

Expert Comment

ID: 41813159
Do you mean like this?

combine.bat c:\oldtxtfolder

Open in new window

...moves the original txt files to


Open in new window


Author Comment

ID: 41813163
I think so. So, your code will combine the .txt files into a .tra file. It will keep the .txt files in the c:\folder\ and move the .tra file to c:\TRA_Files\TR2016.tra. The .txt files that are in the c:\folder\ are the ones i want to move after the .tra file is created. When they are moved, I'd like them moved to a newly created folder with the date/time as the folder name. Steps when the batch file is ran:
1. Combines .txt files in c:\folder\ into one .tra file
2. .tra file is moved to c:\TRA_Files\TR2016.tra
3. .txt files currently in c:\folder\ are moved to a new folder c:\history\20160923_115400

This means at this point, the c:\folder\ is empty. On step 3, the .txt's don't need to be combined, they just need to be moved to the newly created folder.
LVL 24

Accepted Solution

NVIT earned 500 total points (awarded by participants)
ID: 41813285
@echo off
REM Combines multiple txt files to one file.
REM Moves original txt files to dated folder
setlocal enabledelayedexpansion

set FNSrcPatt=%1
set FNOut=%2
set DirMoveSrcTo=%3

if not defined Temp echo *** Critical error: TEMP environment variable not defined& goto :eof
if not defined FNSrcPatt echo *** Can't find source .txt files& goto :ErrSyntax
call :CfgCombinedFile
call :CfgMoveToDir

for %%a in ("%FNSrcPatt%") do (type %%a>>"%FNOut%")
if defined DirMoveSrcTo move "%FNSrcPatt%" "!DirMoveSrcTo!">nul
exit /b

REM Subs below this line only!
REM -------------------------------------------------------------------------------
if not defined FNOut set FNOut=%temp%
call :SetTimestamp
set FNOutDir=%FNOut%\TRA_Files
md "%FNOutDir%"
set FNOut=%FNOutDir%\Tr%DT_Year%.tra
REM echo FNOut=%FNOut%
goto :eof

if not defined DirMoveSrcTo set DirMoveSrcTo=%temp%
call :SetTimestamp
set DirMoveSrcTo=%DirMoveSrcTo%\%Timestamp%
md "%DirMoveSrcTo%"
REM echo DirMoveSrcTo=%DirMoveSrcTo%
goto :eof

REM Sets variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Year%%DT_Month%%DT_Day%_%DT_Hour%%DT_Minute%%DT_Second%
goto :eof

echo Syntax: %0 {source txt file pattern} [combined output filename] [folder to move original txt files to]
echo If [combined output filename] or [folder to move original txt files to] is
echo not passed, the %TEMP% folder is used.
echo e.g.
echo %0 "C:\local\test\combine\*.txt" "C:\local\test\combine" "C:\local\test\originals"
echo  Moves originals to "C:\local\test\originals\YYYYMMDD_HHMMSS"
echo  Creates "C:\local\test\combine\TRA_Files\TR2016.tra"
echo %0 "C:\local\test\combine\*.txt"
echo  Moves originals to "%temp%\YYYYMMDD_HHMMSS"
echo  Creates "%temp%\TRA_Files\TR2016.tra"

Open in new window

combine.bat c:\folder\*.txt c:\folder\combine c:\folder\originals

Open in new window

LVL 24

Expert Comment

ID: 41839775
Question answered.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question