Solved

How to combine .txt and create a new folder

Posted on 2016-09-23
4
26 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 23

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 23

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 23

Expert Comment

by:NVIT
ID: 41839775
Question answered.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now