Solved

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

Posted on 2011-03-09
20
568 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 68

Expert Comment

by:Qlemo
Comment Utility
@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
Comment Utility
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
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I tried the second script with your correction applied.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:John-S Pretorius
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That did it.

Awesome work, have a great day.
0
 

Author Closing Comment

by:John-S Pretorius
Comment Utility
Absolutely Awesome result for me.
0
 
LVL 43

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

11 Experts available now in Live!

Get 1:1 Help Now