Solved

How to combine .txt and create a new folder

Posted on 2016-09-23
4
34 Views
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!
0
Comment
Question by:brasiman
  • 3
4 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 41813159
Do you mean like this?

combine.bat c:\oldtxtfolder

Open in new window


...moves the original txt files to

c:\oldtxtfolder_20060923_113900

Open in new window

0
 

Author Comment

by:brasiman
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.
0
 
LVL 24

Accepted Solution

by:
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 -------------------------------------------------------------------------------
:CfgCombinedFile
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

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

:SetTimestamp
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

:ErrSyntax
echo.
echo Syntax: %0 {source txt file pattern} [combined output filename] [folder to move original txt files to]
echo.
echo If [combined output filename] or [folder to move original txt files to] is
echo not passed, the %TEMP% folder is used.
echo.
echo e.g.
echo.
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.
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

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

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 41839775
Question answered.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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