batch script to match header in CSV file

January 15, 2012 at 22:33:53
Specs: Windows XP
Hi guys, currently i have a problem with my script, i am unable to match the output with the header.
setlocal enabledelayedexpansion
@echo off
set header=Server,Mail,Store

echo %header%>>output.CSV

for /f "tokens=*" %%1 in ('dir/b sg*') do (

for /f %%a in ('find /c /v "" ^< "%%1"') do (

for /l %%b in (1,1,%%a) do (
set filein=%%1
set server=!filein:~2,2!
set mail=!filein:~4,2!
set store=!filein:~6,3!
if !server!==!header!
if !mail!==!header!
if !store!==!header!
echo !server!>>output.CSV
echo !mail!>>output.CSV
echo !store!>>output.CSV
)
)
)
how do i match the server,mail and store with the header that i set in the script?
Thanks in advance
Wei


See More: batch script to match header in CSV file

Report •


#1
January 16, 2012 at 00:10:16
Your script doesn't make a lot of sense to me. Consider these lines:

set header=Server,Mail,Store
set server=!filein:~2,2! 
if !server!==!header! 

The variable Server will only contain 2 bytes, how can you compare it to the variable Header which contains a 17 byte string and ever expect True to be returned? Same consideration for variable Mail and Store.

It's not possible to debug your script without knowing the format of the filenames SG* or the expected contents of the files or what you are trying to achieve.

Please use the PRE facility when posting code.



Report •

#2
January 16, 2012 at 01:27:20
Hi, T.C.
Regarding to your message, what is PRE facility? and regarding my posted question, fyi, my script here reads the filename that starts with 'SG' and count the number of lines within the file. it then return the filename in 3 parts, repeating the 3 parts according to the number of lines in the file.
for example; Filename : sg12hm45a; number of lines : 3
the script will output :server mail store
...............................12(server)
...............................hm(mail)
...............................45a(store)
...............................12(server)
...............................hm(mail)
...............................45a(store)
...............................12(server)
...............................hm(mail)
...............................45a(store)
given that the filename and the number of lines are both variable.
What i am achieve is assign each variable to its own header. for example, server should go to server column which currently the script is unable to do so.
Thanks in advance
Wei

Report •

#3
January 16, 2012 at 14:38:49
Re: Pre - When creating a post, above the dialog box you will see selections for Bold, Italics, Strike-Through, Hyperlink and Pre. Using pre allows code to be posted retaining formatting, indenting etc.

I still have doubts about what you are trying to achieve, your first post indicates a CSV file but your last post (#2) indicates a space delimited file.

Try the following script and get back if it's not what you want. It should create a CSV output file.

@echo off
cls
setlocal enabledelayedexpansion

echo Server,Mail,Store>output.csv

for /f "tokens=*" %%1 in ('dir/b sg*') do (
    set filein=%%1
    set outline=!filein:~2,2!,!filein:~4,2!,!filein:~6,3!

    for /f %%a in ('find /c /v "" ^< "%%1"') do (

    for /l %%b in (1,1,%%a) do (
    echo !outline!>>output.csv
    )
  )
)

type output.csv


Report •

Related Solutions

#4
January 16, 2012 at 18:49:42
Hi T.C.
The script you sent me is the script i had, before i modified to this current script. What i need is that the filename parts are assigned to the <server, mail, store>
as the header has other information in it <set header=Mailbox,Last Logged on By,Size (KB),Total Items,Last Logon Time,Last Logoff Time,Server,Mail,Store>
The information from the filename splits into 3 parts and are needed to be assigned to server, mail and store in the given header. The output file is in CSV.
Thanks in advance.
Wei

Report •

#5
January 17, 2012 at 00:12:15
From that I now take it that the existing file already consists of six columns and you want to add a further three columns.

Here's another script to try:

@echo off
cls
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in ('dir/b sg*') do (
    set /p header=<%%1
)
echo %header%,Server,Mail,Store>output.csv


for /f "tokens=*" %%1 in ('dir /b sg*') do (

    set filein=%%1
    set outline=,!filein:~2,2!,!filein:~4,2!,!filein:~6,3!

        for /f "skip=1 tokens=*" %%A in (%%1) do (
            echo %%A!outline!>>output.csv

  )
)

type output.csv

Or perhaps you want the output to be in individual files?????


Report •

#6
January 18, 2012 at 00:35:06
Hi T.C.
Thanks for the script, with your script information, i was able to edit and add some features from your script to my script, thus enabling me to get the result i desire. Thanks for your help,
Cheers
Wei

Report •


Ask Question