I have a file that gets generated automatically as in A below. In order to use the file, I have to re-arrange it in the form B below and also remove the ' . It is time consuming and tasking having to handle several of such files. I need somebody to help with a batch file that can, on a click re-arrange the file automatically in the B.txt format.
Thanks.
A
===
DATE,ENERGY,POWER,TIME
20170613,1000.0,100.0,'03:47'
B
===
DATE,TIME,POWER,ENERGY
20170613,03:47,100,1000
Help arrange with a batch script
Moderator: DosItHelp
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Help arrange with a batch script
Try these. Just change "your_actual_path" and *_A_files.txt to whatever path and wildcard you need (i.e "%userprofile%\some_folder" and perf_logs_*.txt)
Outputs all matching input files data to a single file.
Outputs each input file data to a new file with the same name, but changing ext to .log
Side note Works only in A files have two lines, like example you gave. Need changes, if they have other format.
Outputs all matching input files data to a single file.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
>"B_Result.txt" (
for /R "your_actual_path" %%i in (*_A_files.txt) do (
<"%%~nxi" (
set /p header=
set /p data=
)
for /F "tokens=1-4 delims=," %%a in ("!header!") do echo/%%a,%%d,%%c,%%b
for /F "tokens=1,2,4,6 delims=,.'" %%a in ("!data!") do echo/%%a,%%d,%%c,%%b
)
)
endlocal
exit/B
Outputs each input file data to a new file with the same name, but changing ext to .log
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /R "your_actual_path" %%i in (*_A_files.txt) do (
<"%%~nxi" (
set /p header=
set /p data=
)
>"%%~ni.log" (
for /F "tokens=1-4 delims=," %%a in ("!header!") do echo/%%a,%%d,%%c,%%b
for /F "tokens=1,2,4,6 delims=,.'" %%a in ("!data!") do echo/%%a,%%d,%%c,%%b
)
)
endlocal
exit/B
Side note Works only in A files have two lines, like example you gave. Need changes, if they have other format.
Re: Help arrange with a batch script
@elzooilogico
Wow!! It works perfectly. Thanks a Billion.
I think I need to learn this scripting immediately.
Wow!! It works perfectly. Thanks a Billion.
I think I need to learn this scripting immediately.
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Help arrange with a batch script
babaegun wrote:@elzooilogico
Wow!! It works perfectly. Thanks a Billion.
I think I need to learn this scripting immediately.
yes, you will enjoy shooting at your feet!
now, seriously, it's worth the effort when you can automate jobs. knowing the tools modern systems rely on, surely will make your life easier... (and more time to share with your family)
Re: Help arrange with a batch script
Yeah. Automation is the way to go. It makes life easy. Thanks.
So how do i learn this scripting? Any recommendation from you?
So how do i learn this scripting? Any recommendation from you?
Re: Help arrange with a batch script
^ Repetition, man! Stick with it and you'll be able to hold your own in no time.
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Help arrange with a batch script
Welcome to DosTips.
may be not the best, but a good starting point...
of course here
http://www.dostips.com/
and
http://www.robvanderwoude.com/batchstart.php
https://www.tutorialspoint.com/batch_script/
https://ss64.com/nt/
Then, once you have done some efforts, and get stuck, come back here
http://www.dostips.com/forum/viewforum.php?f=3
or
https://stackoverflow.com/questions/tagged/batch-file
with a specific question, and you may get help from other users.
may be not the best, but a good starting point...
of course here
http://www.dostips.com/
and
http://www.robvanderwoude.com/batchstart.php
https://www.tutorialspoint.com/batch_script/
https://ss64.com/nt/
Then, once you have done some efforts, and get stuck, come back here
http://www.dostips.com/forum/viewforum.php?f=3
or
https://stackoverflow.com/questions/tagged/batch-file
with a specific question, and you may get help from other users.
Re: Help arrange with a batch script
@elzooilogico
Thanks. I will give it a shot!
Thanks. I will give it a shot!