Computer Hope

Microsoft => Microsoft DOS => Topic started by: trig on May 01, 2020, 03:09:07 PM

Title: Assign Variable for use by tokens statement in Windows 7 batch file
Post by: trig on May 01, 2020, 03:09:07 PM
Hello,
hoping this is a simple question. I am working on a process for work...the overall path to a solution may not be the road I am currently on, but this is a learning experience regardless and so far I like where it's going. Quick explanation..

Want to run a batch file on .csv file(s) that will have varying column counts. Is there a way to read the header to determine how many columns are in this particular .csv file and assign that to a variable to be used by the tokens statement later in process? This is in Windows 7.

Here is what I have so far...

@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\trig\Desktop"
SET "destdir=C:\Users\trig\Desktop"
SET "filename1=%sourcedir%\Before.csv"
SET "outfile=%destdir%\After.csv"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
 FOR /f "tokens=4delims=," %%x IN ("%%a") DO ECHO %%a
)
)>"%outfile%"

GOTO :EOF

FOR /f "tokens=4 is the piece I want to put a variable in..ie FOR /f "tokens=%numofcolums%

any help is much appreciated
Title: Re: Assign Variable for use by tokens statement in Windows 7 batch file
Post by: Hackoo on May 02, 2020, 07:49:24 AM
Hi  ;)
Give a try for this example and tell me if this what you want or not ?
Code: [Select]
@echo off
Title Assign Variable for use by tokens statement
If /I [%~x1] NEQ [.CSV] (
Color 0C & echo(
echo      You should drag and drop a [".csv"] file over this script "%~nx0"
Timeout /T 5 /NoBreak>nul
Exit
)
setlocal EnableDelayedExpansion
set _comma=,
set count=0
set /p _line=<%1
Call :Count_Commas
echo( & color 0A
echo The file "%~nx1" contains !count! commas
set /a Tokens= !count! + 1
echo(
echo The num_of_colums in this case will be !Tokens!
echo(
pause
exit
::---------------------------------------------------
:Count_Commas
if [!_line:~0^,1!] equ [!_comma!] (
set /a count+=1
)
if [!_line:~1!] neq [] (
set _line=!_line:~1!
goto :Count_Commas
)
Exit /b
::---------------------------------------------------
Title: Re: Assign Variable for use by tokens statement in Windows 7 batch file
Post by: trig on May 05, 2020, 02:48:21 PM
Thank you for the reply Hackoo and sorry for the late response.

If I understand your code correctly, the top section is basically calling the bottom and resetting the "count" variable to 0 for every file...the bottom section is doing the actual counting of the commas (counting how many columns)? Is this correct?

Also, what does Line 5 - echo      You should drag and drop a [".csv"] file over this script "%~nx0" mean? drag and drop a .csv file?

For what it's worth, right now my Before.csv file looks like this -
Col1,Col2,Col3,Col4,Col5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
,,,,

1,2,3,4,5
1,2,3,4,5
and with the code I have in my original post, the After.csv file looks like this -
Col1,Col2,Col3,Col4,Col5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5

Which is perfect, but like I said, I want to count the number of columns on the fly, for as many .csv files as there may be in this folder at the time the batch file is run.

Kris