Solved

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

Posted on 2011-03-09
20
575 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
[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
  • 10
  • 7
  • 3
20 Comments
 
LVL 70

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 70

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 70

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Suggested Courses

630 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