Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to combine .txt and create a new folder

Posted on 2016-09-23
4
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

715 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