Reputation: 15
I have application number along with associated images for that application on every row.
58:58123 image.jpg,image2.jpg
58:58456 image4.jpg
I want to have csv which will contain unique number and one associated image on every row like
123 image.jpg
123 image2.jpg
456 image4.jpg
I have already created bat file but it is showing me output as
58:58123 image.jpg
58:58123 image2.jpg
58:58456 image4.jpg
I want to remove 58:58 from every row.
@Echo off&(for /f "usebackq tokens=1* delims=:" %%A in ("transact.txt") do For %%C in (%%B) do Echo(%%A,%%C)>Output.csv
Actual Output:
58:58123 image.jpg
58:58123 image2.jpg
58:58456 image4.jpg
Expected Output:
123 image.jpg
123 image2.jpg
456 image4.jpg
Upvotes: 1
Views: 273
Reputation: 34899
It is a bad practice to put all code into a single line as it is hardly maintainable.
Anyway, your code seem to make an attempt to remove the prefix (like 58
), and it does for sure not return the actual output you describe.
But here is a possible way to do what you want (most probably):
@echo off
rem // Write to the output file:
> "Output.csv" (
rem // Read from the input file and split at the first `:`:
for /F "usebackq tokens=1-2* delims=: " %%A in ("transact.txt") do (
rem // Assign parts behind the first `:` to variables:
set "NUM=%%B" & set "LIST=%%C"
rem /* Enable delayed expansion to be able to read a variable
rem that is has been written in the same block of code: */
setlocal EnableDelayedExpansion
rem /* Remove prefix from number before the file names and then
rem provide it in a `for` variable (one loop iteration): */
for %%D in ("!NUM:*%%A=!") do (
rem /* Modify list so that spaces in file names are preserved,
rem loop through the file names (none must contain `,`!): */
for %%E in ("!LIST:,=" "!") do (
rem /* Return one file name after another preceded by the
rem pruned number; toggle delayed expansion in order to
rem avoid trouble with `!` or `^` in the file names: */
endlocal
echo(%%~D %%~E
setlocal EnableDelayedExpansion
)
)
endlocal
)
)
This script makes use of sub-string substitution as well as delayed expansion.
Upvotes: 1