Acy Forsythe wrote:Code: Select all
FOR /F "tokens=1-26 delims=," %%A IN ('%CMD%') DO (
SET Result=
SET "Text=%%~C"
%macro_Call% ("Text Result") %macro.StrLen%
IF "!Result!" LSS "2" (
SET "Text=000!Text!"
) ELSE (
IF "!Result!" LSS "3" (
SET "Text=00!Text!"
) ELSE (
IF "!Result!" LSS "4" (
SET "Text=0!Text!")
)
)
ECHO %%~A,%%~B,!Text!,%%~D,%%~E,%%~F,%%~G,%%~H,%%~I,%%~J
) >> %WORKDIR%\temp.csv
Ed Dyreen wrote:Code: Select all
set "$Fetch=00000"
::
set "$Fetch=!$Fetch:~%Result%!"
::
set "Text=!$Fetch!!Text!"
Remember that set is way faster than if so if you can avoid it...
I don't follow Ed's logic, but I think he might have been flirting with a good solution
There is no need for StrLen to left pad a number -
Align RightCode: Select all
FOR /F "tokens=1-26 delims=," %%A IN ('%CMD%') DO (
SET "Text=000%%~C"
ECHO %%~A,%%~B,!Text:~-4!,%%~D,%%~E,%%~F,%%~G,%%~H,%%~I,%%~J
) >> %WORKDIR%\temp.csv
If %%C has the potential of exceeding 4 digits, then:
Code: Select all
FOR /F "tokens=1-26 delims=," %%A IN ('%CMD%') DO (
SET "Text=%%~C"
IF "!Text:~3!" == "" (
SET "Text=000!Text!"
SET "Text=!Text:~-4!"
)
ECHO %%~A,%%~B,!Text!,%%~D,%%~E,%%~F,%%~G,%%~H,%%~I,%%~J
) >> %WORKDIR%\temp.csv
Obviously you can incorporate a %Prefix% into either solution above.
WARNING - FOR /F is very convenient for working with CSV as long as there are no empty fields. But be careful - Empty fields will shift subsequent columns to the left:
Code: Select all
for /f "tokens=1-26 delims=," %a in ("A,B,,D,E") do echo %a,%b,%c,%d,%e,
produces:
Can CSVFix not left pad a number
I've never used it, but I see that it has a Pad function.
I understand not wanting to break out the C compiler. But having to process a file multiple times can definitely slow the process down.
Years ago I did some serious CSV manipulation using Perl on Windows. It is the only time I used that free language, but it was successful and efficient.
Another free language that people seem to like is Ruby - I've not tried it.
Acy - I'm glad you've gotten some useful tools from this site. Until I discovered this site 6 months ago I was clueless to the level of sophistication that is possible with musty old Batch programming. Since then, I've had a blast exploring what is possible. Mostly it has been "mental masturbation' during my free time. But like you I have a set of batch tools that are real time savers in real world work situations.
Dave Benham