Batch script to read a .DAT file & rename it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Batch script to read a .DAT file & rename it

#1 Post by yadavmanoj78 » 07 Mar 2014 03:19

Dear All,

I need a batch script which can read a file *.D00000.DAT and find the specific line as below

*-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*

and store Job, Name, Date & Time in different variables.

Then do the following:

1. close the file and rename it with "Name".
2. Create a folder and name it with Job-Name-Date-Time
3. Move the renamed file into the folder
4. repeat the process for next *.D00000.DAT file

Squashman
Expert
Posts: 4477
Joined: 23 Dec 2011 13:59

Re: Batch script to read a .DAT file & rename it

#2 Post by Squashman » 07 Mar 2014 04:43

Will there ever be spaces in the Name or user?

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#3 Post by yadavmanoj78 » 07 Mar 2014 21:50

Yes, there is a space between User & Name

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .DAT file & rename it

#4 Post by foxidrive » 07 Mar 2014 21:56

The question is: will there be spaces in these two parts:

ABCDEFGH
mfuser

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#5 Post by yadavmanoj78 » 07 Mar 2014 22:12

Yes, there will be a space between ABCDEFGH & mfuser, but we dont need mfuser anywhere.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .DAT file & rename it

#6 Post by foxidrive » 07 Mar 2014 22:55

Take 3 - will there be any spaces like these

ABC DEF GH
mf user

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#7 Post by yadavmanoj78 » 07 Mar 2014 23:11

No, there will not be any spaces like this.

ABC DEF GH
mf user

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .DAT file & rename it

#8 Post by foxidrive » 07 Mar 2014 23:35

Ok, thanks. One more question: does the line look like this:

Code: Select all

*-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*


or like this:

Code: Select all

Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#9 Post by yadavmanoj78 » 07 Mar 2014 23:47

Line looks like this:

*-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch script to read a .DAT file & rename it

#10 Post by foxidrive » 08 Mar 2014 00:29

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%a in (*.D00000.DAT) do (
for /f "tokens=3,5,9,11" %%b in ('type "%%a" ^|find " Job: "^|find " Name: "^|find " User: "^|find " Date: "^|find " Time: " ') do (
set d=%%d
set d=!d:/=_!
set t=%%e
set t=!t::=_!
md "%%b-%%c-!d!-!t!"
move "%%a" "%%b-%%c-!d!-!t!\%%b"
)
)
pause

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#11 Post by yadavmanoj78 » 08 Mar 2014 01:30

Thanks it works.

I need 2 modifications in it.

While renaming keep the original file name & add the Name into it. Like below:

1. ABC2013.C0000.D133136.J11341.D00000.DAT after renaming it should be
ABCDEFGH.ABC2013.C0000.D133136.J11341.D00000.DAT

2. Look for other files which has same Job and add the Name in that file name as well ABC2013.C0000.D133136.J11341.D00001.SYSPRINT.DAT will look like ABCDEFGH.ABC2013.C0000.D133136.J11341.D00001.SYSPRINT.DAT

3. Move the renamed files into respective folder and rest of the steps are same.

Squashman
Expert
Posts: 4477
Joined: 23 Dec 2011 13:59

Re: Batch script to read a .DAT file & rename it

#12 Post by Squashman » 08 Mar 2014 07:44

yadavmanoj78 wrote:Yes, there will be a space between ABCDEFGH & mfuser, but we dont need mfuser anywhere.

That was worse then going to the dentist!
Doesn't matter that you don't need user. You need the data after that and how the user is formatted makes a difference has to how to capture the data after it.

Not sure why you kept thinking we were asking if there were spaces between your data. We could obviously see there was a space between each variable. I was asking if there was any spaces IN the Name or user. Keyword is IN!

Aacini
Expert
Posts: 1888
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch script to read a .DAT file & rename it

#13 Post by Aacini » 08 Mar 2014 10:21

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem "I need a batch script which can read all files *.D00000.DAT and find the specific line as below"
rem *-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*

for /F "tokens=1,4,6,10-12,14-16 delims=:/ " %%a in (
       'findstr /R /C:" Job: .* Name: .* User: .* Date: .* Time: .*" *.D00000.DAT' ) do (

   rem "and store Job, Name, Date and Time in different variables."
   set fileName=%%a
   set Job=%%b
   set Name=%%c
   set DDate=%%d_%%e_%%f
   set TTime=%%g_%%h_%%i

   rem "Then do the following:"

   rem 1. close the file and rename it with "Name".
   rem    Mod 1- While renaming keep the original file name and add the Name into it.
   REM APA - Below, at same time of MOVE

   rem 2. Create a folder and name it with Job-Name-Date-Time
   if not exist "!Job!-!Name!-!DDate!-!TTime!" ECHO md "!Job!-!Name!-!DDate!-!TTime!"

   rem 3. Move the renamed file into the folder
   ECHO move "!fileName!" "!Job!-!Name!-!DDate!-!TTime!\!Name!.!fileName!"

   rem Mod 2- Look for other files which has same Job and add the Name in that file name as well:
   rem                 ABC2013.C0000.D133136.J11341.D00001.SYSPRINT.DAT will look like
   rem        ABCDEFGH.ABC2013.C0000.D133136.J11341.D00001.SYSPRINT.DAT
   rem Mod 3- Move the renamed files into respective folder and rest of the steps are same.
   for %%A in (*.*.*.J!Job!.*) do (
      ECHO move "%%A" "!Job!-!Name!-!DDate!-!TTime!\!Name!.%%A"
   )

   rem 4. repeat the process for next *.D00000.DAT file
)

Note that this Batch file just display the commands! If you want the commands be executed, remove the ECHO from all commands.

Antonio

yadavmanoj78
Posts: 7
Joined: 07 Mar 2014 02:31

Re: Batch script to read a .DAT file & rename it

#14 Post by yadavmanoj78 » 09 Mar 2014 00:38

Thanks alot :D .

Will this batch script work on the Linux machine as well?

Squashman
Expert
Posts: 4477
Joined: 23 Dec 2011 13:59

Re: Batch script to read a .DAT file & rename it

#15 Post by Squashman » 09 Mar 2014 09:35

yadavmanoj78 wrote:Thanks alot :D .

Will this batch script work on the Linux machine as well?

No.

Post Reply