Solved

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

Posted on 2011-03-09
20
572 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:John-S Pretorius
  • 10
  • 7
  • 3
20 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 35080508
@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35080569
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35080582
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
Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

 
LVL 69

Expert Comment

by:Qlemo
ID: 35080596
Good point, Steve. That is indeed much better than processing with FOR /F. I totally forgot about the features of MORE.
0
 

Author Comment

by:John-S Pretorius
ID: 35080858
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35081103
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
 

Author Comment

by:John-S Pretorius
ID: 35081192
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
 

Author Comment

by:John-S Pretorius
ID: 35081203
I tried the second script with your correction applied.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35081276
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35081280
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
 

Author Comment

by:John-S Pretorius
ID: 35081400
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 35081495
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35081498
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35081531
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
 

Author Comment

by:John-S Pretorius
ID: 35081812
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35082095
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 35082105
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
 

Author Comment

by:John-S Pretorius
ID: 35082330
That did it.

Awesome work, have a great day.
0
 

Author Closing Comment

by:John-S Pretorius
ID: 35082335
Absolutely Awesome result for me.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35082536
No problem, maybe there is problem vbscripts then.  As long as it does what you want that;s fine by me.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

828 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