Help arrange with a batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
babaegun
Posts: 4
Joined: 12 Jun 2017 20:46

Help arrange with a batch script

#1 Post by babaegun » 12 Jun 2017 21:04

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

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Help arrange with a batch script

#2 Post by elzooilogico » 13 Jun 2017 02:30

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.

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.

babaegun
Posts: 4
Joined: 12 Jun 2017 20:46

Re: Help arrange with a batch script

#3 Post by babaegun » 13 Jun 2017 06:16

@elzooilogico

Wow!! It works perfectly. Thanks a Billion.

I think I need to learn this scripting immediately.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Help arrange with a batch script

#4 Post by elzooilogico » 13 Jun 2017 09:34

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! :mrgreen:

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)

babaegun
Posts: 4
Joined: 12 Jun 2017 20:46

Re: Help arrange with a batch script

#5 Post by babaegun » 14 Jun 2017 01:23

Yeah. Automation is the way to go. It makes life easy. Thanks. :D

So how do i learn this scripting? Any recommendation from you?

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Help arrange with a batch script

#6 Post by SIMMS7400 » 14 Jun 2017 01:44

^ Repetition, man! Stick with it and you'll be able to hold your own in no time.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Help arrange with a batch script

#7 Post by elzooilogico » 14 Jun 2017 03:25

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.

babaegun
Posts: 4
Joined: 12 Jun 2017 20:46

Re: Help arrange with a batch script

#8 Post by babaegun » 14 Jun 2017 08:42

@elzooilogico

Thanks. I will give it a shot!

Post Reply