Solved How to write for /f to make a field DDDDDDDDCC

August 25, 2020 at 10:12:38
Specs: Windows 10
I have file with lines reading like this:
15 812552 163774 680.93 20200814 C vendor name
and I'm using batch script below to get column b, column c, column e and column d. I need column d or 680.93 to be formatted in DDDDDDDDCC. Is there a way to get this output for this field.
@echo off
if exist %~dp0\TEST2.csv del %~dp0\TEST2.csv > nul
for /f "tokens=1,2,3,4,5,6,7 delims=," %%a in (test11.csv) do echo %%b %%c %%e %%d>> TEST2.csv
copy TEST2.csv TEST2.txt > nul
exit

See More: How to write for /f to make a field DDDDDDDDCC


✔ Best Answer
September 1, 2020 at 03:00:55
input text.txt:

15 812552 163774 680.93 20200814 C vendor name
11 123445 162424 1679.90 20100814 C test bat

output.txt:

812552 163774 20200814 0000068093
123445 162424 20100814 0000167990


test.bat:

@echo off&setlocal enabledelayedexpansion
for /f "tokens=2,3,4,5,6 delims=. " %%a in (text.txt) do (
	set "string=%%c%%d"
	call :strlen ans string
	set/a addzero=10-!ans!
	call :zeroloop
	echo %%a %%b %%e !this! >> output.txt
)
exit/b
:strlen
set l=0
set "s=!%~2!#"
for %%P in (16 8 4 2 1) do (
	if "!s:~%%P,1!" NEQ "" (
		set/a "l+=%%P"
		set "s=!s:~%%P!"
	)
)
set "%~1=%l%"
exit /b
:zeroloop
set zeros=
for /l %%1 in (1,1,%addzero%) do (
	set "zeros=!zeros!0"
)
set this=!zeros!!string!
exit/b

my system specs: https://www.userbenchmark.com/UserR...

message edited by hidde663



#1
August 26, 2020 at 01:29:46
What did you mean by to be formatted in DDDDDDDDCC ???
Please explain with an example ! it's unclear for me !

Reply ↓  Report •

#2
August 26, 2020 at 06:54:21
Using my example I gave above. The 680.93 should output 0000068093. On every line in the file, there will be a different dollar amount in column d. I need to output the line with that field to be right justified, zero filled, 10 characters and decimal point dropped.
example output:
812552 163774 20200814 0000068093
I hope this makes more sense.
Thanks!

message edited by friedkath


Reply ↓  Report •

#3
August 27, 2020 at 21:58:17
Well, since no further answers, might try:
::-------- begin script
@echo off & setlocal
for /f "tokens=1,2,3,4,5,6,7 delims=," %%a in (test11.csv) do call :xx %%b %%c %%e %%d
goto :eof

:xx
set x=%4
set x=%x:.=%
set x=00000000%x%
set x=%x:~-10%
echo %1 %2 %3 %x%


Reply ↓  Report •

Related Solutions

#4
September 1, 2020 at 03:00:55
✔ Best Answer
input text.txt:

15 812552 163774 680.93 20200814 C vendor name
11 123445 162424 1679.90 20100814 C test bat

output.txt:

812552 163774 20200814 0000068093
123445 162424 20100814 0000167990


test.bat:

@echo off&setlocal enabledelayedexpansion
for /f "tokens=2,3,4,5,6 delims=. " %%a in (text.txt) do (
	set "string=%%c%%d"
	call :strlen ans string
	set/a addzero=10-!ans!
	call :zeroloop
	echo %%a %%b %%e !this! >> output.txt
)
exit/b
:strlen
set l=0
set "s=!%~2!#"
for %%P in (16 8 4 2 1) do (
	if "!s:~%%P,1!" NEQ "" (
		set/a "l+=%%P"
		set "s=!s:~%%P!"
	)
)
set "%~1=%l%"
exit /b
:zeroloop
set zeros=
for /l %%1 in (1,1,%addzero%) do (
	set "zeros=!zeros!0"
)
set this=!zeros!!string!
exit/b

my system specs: https://www.userbenchmark.com/UserR...

message edited by hidde663


Reply ↓  Report •

#5
September 15, 2020 at 07:08:57
Thanks so much hidde663 and nbrane!
Can I also get your help with this same batch script. I just learned that the date in the input file is in MMDDYYYY format and I need it to be YYYYMMDD format. Also in the output, I need the first field to be 17 characters, zero filled, second field to be 10 characters, zero filled. the output needs to be a 47 character record. Can you help me with this?

input:
15 812552 163774 680.93 08142020 C vendor name
output:
000000000008125520000163774 20200814 0000068093


Reply ↓  Report •

#6
September 15, 2020 at 22:35:43
ok. Left a lot of debugging in for your benefit (and mine!). Take out the dbg and go from there, IF it works. worked in my test on sample data.
@echo off & setlocal
set c=0
set out=
set max=6
for /f "tokens=*" %%a in (test11.csv) do call :xx %%a
goto :eof

:xx
set /a c+=1
:: skip after max
if %c% geq %max% (
echo OUT1:--------------  %out%
call :arrange %out%
set out=
set c=0
goto :eof
)
call :%c% %1
shift
if "%1" neq "" goto :xx
ECHO %1 NULL
goto :eof

:1
ECHO %0 [%1 ignored
goto :eof

:2
set x=000000000000000000%1
(set out=%out%%x:~-17% )
echo %0: %out%
goto :eof

:3
set x=00000000000000000%1
(set out=%out%%x:~-10% )
echo %0: %out%
goto :eof


:4
:: decimal normalized and padded
set x=%1
set x=00000000000%x:.=%
(set out=%out%%x:~-10% )
echo %0: %out%
goto :eof

:5
::-- date
set x=%1
(set out=%out%%x:~0,4%%x:~-4% )
echo %0: %out% 
goto :eof

:arrange
set out=%1%2 %4 %3
echo final output: %out%
goto :eof


Reply ↓  Report •

Ask Question