Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to combine .txt and create a new folder

Posted on 2016-09-23
4
Medium Priority
?
59 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 25

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 25

Accepted Solution

by:
NVIT earned 2000 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 25

Expert Comment

by:NVIT
ID: 41839775
Question answered.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

886 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