Batch script to read a .DAT file & rename it
Moderator: DosItHelp
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Batch script to read a .DAT file & rename it
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
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
Re: Batch script to read a .DAT file & rename it
Will there ever be spaces in the Name or user?
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
Yes, there is a space between User & Name
Re: Batch script to read a .DAT file & rename it
The question is: will there be spaces in these two parts:
ABCDEFGH
mfuser
ABCDEFGH
mfuser
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
Yes, there will be a space between ABCDEFGH & mfuser, but we dont need mfuser anywhere.
Re: Batch script to read a .DAT file & rename it
Take 3 - will there be any spaces like these
ABC DEF GH
mf user
ABC DEF GH
mf user
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
No, there will not be any spaces like this.
ABC DEF GH
mf user
ABC DEF GH
mf user
Re: Batch script to read a .DAT file & rename it
Ok, thanks. One more question: does the line look like this:
or 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
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
Line looks like this:
*-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*
*-*-* Job: 11676 Name: ABCDEFGH User: mfuser Date: 03/13/13 Time: 10:47:46 *-*-*
Re: Batch script to read a .DAT file & rename it
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
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
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.
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.
Re: Batch script to read a .DAT file & rename it
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!
Re: Batch script to read a .DAT file & rename it
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
-
- Posts: 7
- Joined: 07 Mar 2014 02:31
Re: Batch script to read a .DAT file & rename it
Thanks alot
.
Will this batch script work on the Linux machine as well?
![Very Happy :D](./images/smilies/icon_biggrin.gif)
Will this batch script work on the Linux machine as well?
Re: Batch script to read a .DAT file & rename it
yadavmanoj78 wrote:Thanks alot.
Will this batch script work on the Linux machine as well?
No.