1

My csv file contain 6 columns and I want to copy 6th column value inside first column path value.

Path,Firstname,Lastname,Name,Description,Images  
C:\Users\magoo\abc.jpg,alex,ross,pvs,data,123.jpg  
C:\Users\magoo\pqr.jpg,kris,ward,poi,data,784.jpg

Output I am looking for

Path,Firstname,Lastname,Name,Description,Images  
C:\Users\magoo\123.jpg,alex,ross,pvs,data,123.jpg  
C:\Users\magoo\784.jpg,kris,ward,poi,data,784.jpg  

I only want to only replace image name from first path column with 6th column value.
Kindly help as I am stuck here since long.I am using copy commands but it is replacing entire text from column one.

Code I tried:

@ECHO OFF
SETLOCAL 
( FOR /f "tokens=1-6delims=," %%a IN (ImportFile.csv) DO ( 
   ECHO(%%a%%f,%%b,%%c,%%d,%%e,%%f ) 
)>new.csv
GOTO :EOF
7
  • After having changed from Stack Overflow without commas, it is still no csv file. Did you take a look at the for /f command in the meantime? It is important to know what separates the columns in the real file - not a nice screen representation.
    – LotPings
    Commented Sep 12, 2019 at 21:30
  • @ECHO OFF SETLOCAL ( FOR /f "tokens=1-6delims=," %%a IN (ImportFile.csv) DO ( ECHO(%%a%%f,%%b,%%c,%%d,%%e,%%f ) )>new.csv GOTO :EOF
    – magoosh
    Commented Sep 12, 2019 at 21:32
  • With this code I am able to append new image name to path but I want to remove old image name from that path.
    – magoosh
    Commented Sep 12, 2019 at 21:33
  • For now I added your code to the question, please do that yourself in future instead of putting vital information in comments where it is hardly readable without line breaks. Hint the ~dp tilde modifiers allow to strip name extension information from a full qualified file name.
    – LotPings
    Commented Sep 12, 2019 at 21:46
  • In your echo line, instead of %%a%%f do %%~dpa%%f.
    – aschipfl
    Commented Sep 13, 2019 at 10:59

1 Answer 1

0

I just wrote this and it stripped my comments. .. then I fixed a bug :(

@echo off

Set Input_file=test.csv
Set Output_file=test_modified.csv

for /f "tokens=1-6 delims=," %%l in (%Input_file%) do call :process_tokens "%%l" "%%m" "%%n" "%%o" "%%p" "%%q"
goto :EOF

:process_tokens 
Set Col2=%~2
Set Col3=%~3
Set Col4=%~4
Set Col5=%~5
Set Col6=%~6

Set Col1=%~dp1%Col6%
echo %Col1%,%Col2%,%Col3%,%Col4%,%Col5%,%Col6% >>%Output_file%

goto :EOF

I am using label/functions to accomplish this.

In the ":process_tokens" function, look at the %~dp2%Col6% line.

%~dp1 means:

  • %~ = get the passed parameter and strip quotes from it.
  • dp = drive and path only (aka strip the filename)
  • 1 = passed param #1

%Col6% is where I tack on the filename you are looking for.

8
  • Also, @magoosh.. you posted while I was typing. I didn't copy you ;) Great minds think alike. Commented Sep 12, 2019 at 21:44
  • Thanks for your help!! Data is coming correctly but it is copying headers as well.
    – magoosh
    Commented Sep 12, 2019 at 21:45
  • @magoosh The call isn't necessary, in your code simply use echo %%~dpa%%f,...
    – LotPings
    Commented Sep 12, 2019 at 21:52
  • @LotPings, no, the call isn't needed. it just makes the code more readable and who doesn't love functions? Commented Sep 12, 2019 at 21:53
  • That's debatable, a call involves a 2nd hidden cmd, parameter passing etc, in sum it creates overhead and slows things down, when working with larger files this could be prohibitive.
    – LotPings
    Commented Sep 12, 2019 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .