Windows batch file to rename to .txt and then remove the first header line.

I have an automatically generated file saved automatically in a folder every night : C:/Reports/Hotelexternaldata.20110308000000.20110308235959.EXP that I firstly want to rename to C:/Reports/PARC.txt and then remove/delete just the first line :

210004,1704683,119,08.03.2011,'USD'
'Time','SerialNo.','MovementType','DeviceNo.','Rejection','SystemDeviceDesignation',
07.03.2011 01:12:04,'2398=1103052006',0,22,'No Rejection','4th Street Ent Right

and then save it again as PARC.txt in a new folder C:/Reports/Data
John-S PretoriusTechnical Service Manager, Mid-AtlanticAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
Or if you aren't bothered about the date format then this will give you mmddyyyy without the VBScript bit:

@echo off

cd /d c:\reports
md c:\reports\archive 2>NUL

REM Remove existing parc.txt
del parc.txt 2>NUL

REM Skip first line of any .ext files into parc.txt
more +1 Hotel*.exp > parc.txt

REM Gate ddmmyyyy into shortdate variable
set shortdate=%date:~4%
set shortdate=%shortdate:/=%

REM Copy the parc.txt to archive as parcddmmyyyy.txt
copy parc.txt C:\reports\archive\parc%shortdate%.txt 2>NUL

REM Delete the Hotel*.exp files
del Hotel*.exp 2>NUL

echo You now have park.txt looking like this:
more parc.txt

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
@echo off
pushd C:\Reports
ren Hotelexternadata.*.exp PARC.txt
(for /F "skip=1 delims=" %%L in (PARC.txt) do echo,%%L) > Data\PARC.txt
popd

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
I would suggest using a more +1 or find /v command with a unique string on line 1 as this would means less likely to be affected by any special characters in the file, e.g.

@echo off
cd /d c:\reports

REM Skip first line of any .ext files into parc.txt
more +1 *Hotel.ext > parc.txt

REM remove the .ext files (or perhaps they should be renamed or moved elsewhere for keeping?)
del Hotel*.ext 2>NUL

echo You now have parc.txt looking like this:
more parc.txt

Steve
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Steve KnightIT ConsultancyCommented:
Sorry, copy/paste error, the line:

more +1 *Hotel.ext > parc.txt

should be

more +1 Hotel*.ext > parc.txt

or whatever file string matches your files actual names if different to example shown.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Good point, Steve. That is indeed much better than processing with FOR /F. I totally forgot about the features of MORE.
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Where did the data go ?
When executed, it actually renames perfect but then cleans the file out completely : empty .txt file.

Keep in mind the first line within the .EXP that I want completely removed looks like below :
210004,1704683,119,08.03.2011,'USD'

I want the first line deleted completely leaving no space. I am not really interested in the original  .EXP file anymore but would want to create another copy of that renamed file with a new date added (PARC03092011.txt) and moved to C:/Parking/Archive

Tall order guys ?
0
 
Steve KnightIT ConsultancyCommented:
I always forget about MORE + option too Qlemo, was thinking of doing a find /v then it came to me :-)

Which script did you try?  Can you give us an example script of 4-5 lines + the header perhaps (made up data if needed)

I tried mine on a test file called hotel....ext and saw it remove the first line and leave it in parc.txt

Steve
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I just created a .EXP file in notepad and put4 lines of garbage in but here is an exact example :

210004,1704683,801,06.03.2011,'USD'
'Time','SerialNo.','MovementType','DeviceNo.','Rejection','SystemDeviceDesignation','CarParkName','CarParkNo','Amount','AdditionalInformation'
08.03.2011 00:01:19,'5797=1103061554',4,42,'No Rejection','B Level Exit','Skywalk Garage',0,0.00,''
08.03.2011 00:06:13,'3478=1103051844',0,22,'No Rejection','4th Street Ent Right Main','Skywalk Garage',0,0.00,''
08.03.2011 00:15:36,'5797=1103061554',0,86,'No Rejection','3rd Street B Ent Nesting','Nested Garage (A)',2,0.00,''
08.03.2011 00:25:54,'2987=1103080028',11,51,'No Rejection','Parc ENT 1','PARC Garage',3,0.00,''

Much appreciated.
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I tried the second script with your correction applied.
0
 
Steve KnightIT ConsultancyCommented:
OK I saved your file as hoteldata.exp in c:\reports

I used this script

@echo off
cd /d c:\reports
md c:\reports\archive 2>NUL

REM Skip first line of any .ext files into parc.txt
more +1 Hotel*.ext > parc.txt

REM remove the .ext files (or perhaps they should be renamed or moved elsewhere for keeping?)
move Hotel*.ext C:\reports\archive 2>NUL

echo You now have parc.txt looking like this:
more parc.txt

and the output was:

C:\>1
You now have parc.txt looking like this:
'Time','SerialNo.','MovementType','DeviceNo.','Rejection','SystemDeviceDesignati
on','CarParkName','CarParkNo','Amount','AdditionalInformation'
08.03.2011 00:01:19,'5797=1103061554',4,42,'No Rejection','B Level Exit','Skywal
k Garage',0,0.00,''
08.03.2011 00:06:13,'3478=1103051844',0,22,'No Rejection','4th Street Ent Right
Main','Skywalk Garage',0,0.00,''
08.03.2011 00:15:36,'5797=1103061554',0,86,'No Rejection','3rd Street B Ent Nest
ing','Nested Garage (A)',2,0.00,''
08.03.2011 00:25:54,'2987=1103080028',11,51,'No Rejection','Parc ENT 1','PARC Ga
rage',3,0.00,''

Steve
0
 
Steve KnightIT ConsultancyCommented:
Sorry I realised there I am using .ext not .exp so it should be

@echo off
cd /d c:\reports
md c:\reports\archive 2>NUL

REM Skip first line of any .ext files into parc.txt
more +1 Hotel*.exp > parc.txt

REM remove the .exp files (or perhaps they should be renamed or moved elsewhere for keeping?)
move Hotel*.exp C:\reports\archive 2>NUL

echo You now have parc.txt looking like this:
more parc.txt
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Works Great, I just have one last request if possible please .

When I move the file to the Archive, I actually want the parc.txt copied and renamed (park03092011) and then moved to the Archive. Remember that I still need the park.txt in it's original folder.

You did great,

Thanks.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry, but having a date format of mmddyyyy is a bad idea. You can't sort for that. It is much better to retain the yyyymmdd format of the original file.
Would you need to have the original date of the file, or the execution date?
Does    echo %date%    give you     Wed, 03/09/2011 ?
0
 
Steve KnightIT ConsultancyCommented:
Ok so you don't want the one with the header then.  In which case we need to get the date into a string.  We can either use your current date format which may or not change usign %date:/=% to remove any / characters from the date but better is to use a small VBScript:



http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

@echo off

cd /d c:\reports
md c:\reports\archive 2>NUL

REM Remove existing parc.txt
del parc.txt 2>NUL

REM Skip first line of any .ext files into parc.txt
more +1 Hotel*.exp > parc.txt

REM Gate ddmmyyyy into shortdate variable
echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& year(date) > "%temp%\dateparts.vbs"
for /f "tokens=1-8 delims=," %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set shortdate=%%a

REM Copy the parc.txt to archive as parcddmmyyyy.txt
copy parc.txt C:\reports\archive\parc%shortdate%.txt 2>NUL

REM Delete the Hotel*.exp files
del Hotel*.exp 2>NUL

echo You now have park.txt looking like this:
more parc.txt
0
 
Steve KnightIT ConsultancyCommented:
You can change the order of the date if you wish:

echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& year(date) > "%temp%\dateparts.vbs"
to
ddmmyyyy
echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& year(date) > "%temp%\dateparts.vbs"

mmddyyyy
echo wscript.echo right(100+month(date),2) ^& right(100+day(date),2) ^& year(date) > "%temp%\dateparts.vbs"

yyyymmdd

echo wscript.echo year(date) ^& right(100+month(date),2) ^& right(100+day(date),2)  > "%temp%\dateparts.vbs"
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I'm OK with any date format and echo does return as indicated in your prevuous question.

At this stage when I execute it does not copy to the Archive Folder at all.
0
 
Steve KnightIT ConsultancyCommented:
With the script I had above it does this for me.  Try removing the @echo off line and seeing what output you get.  Any errors?

C:\reports>1
        1 file(s) copied.
You now have park.txt looking like this:
'Time','SerialNo.','MovementType','DeviceNo.','Rejection','SystemDeviceDesignation','CarParkName','CarParkNo','Amount','AdditionalInformation'
08.03.2011 00:01:19,'5797=1103061554',4,42,'No Rejection','B Level Exit','Skywalk Garage',0,0.00,''
08.03.2011 00:06:13,'3478=1103051844',0,22,'No Rejection','4th Street Ent RightMain','Skywalk Garage',0,0.00,''
08.03.2011 00:15:36,'5797=1103061554',0,86,'No Rejection','3rd Street B Ent Nesting','Nested Garage (A)',2,0.00,''
08.03.2011 00:25:54,'2987=1103080028',11,51,'No Rejection','Parc ENT 1','PARC Garage',3,0.00,''

C:\reports>dir
<snip>
09/03/2011  09:48       <DIR>          archive
09/03/2011  12:32                  572 parc.txt
<snip>

C:\reports>dir archive
<snip>
09/03/2011  12:32                  572 parc09032011.txt
<snip>

Steve
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
That did it.

Awesome work, have a great day.
0
 
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Absolutely Awesome result for me.
0
 
Steve KnightIT ConsultancyCommented:
No problem, maybe there is problem vbscripts then.  As long as it does what you want that;s fine by me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.