2

I want to loop through a CSV file and create a full path variable out of two columns - and only if one specific column has the value "Folder".

This actually works right now, but the not working part is the creation of the path variable.

two points:

the two columns I look at are the deepest folder and the root folders aboth. Like this: first/first1/ + second = first/first1/second

So, the deepest folder will always be filled and I can just take it like it comes.

The folders above will either be filled or it will be a 0 standing in the column. Also the string will have a "/" which I have to CUT and replace with a backslash ..

Here's my code:

For /F "tokens=1-7 delims=;" %%a in (L:/Mappe1.csv) Do (

if "%%b"=="Folder" (
   set folder=%%c
   if NOT %%f==0 (
      set rootFolder ==f
      rem check for slash..
      if %rootFolder=~-1%==/ (
          rem cut it out
          SET rootFolder=%rootFolder:~0,-1%
      )
      rem both variables set
      set finalPath=%rootFolder%%folder%
   ) else (
      rem just the folder
      set finalpath=%folder%
   )
echo %finalpath%
)
) 

Sampe of the CSV:

ID;PROGID;NAME; PARENTID; PARENT_FOLDER;PATH; ISNTANCE; DESC; ORDER; 23;Folder;Stammordner;4;4;PRODUKTION/;0;;;

What's not working:

  • right now I get the "Echo is off" output everytime I run. I think that somehow the finalpath is not set, but I don't get where.

  • also I don't know if the way I do it is the common way - I'm so much new to batch - even if I'm a developer its somehow very complicated for me ..

Regards

7
  • If you provide a sample of the csv file and describe the task then we may find a more straight forward way to do it. Commented Dec 11, 2013 at 10:23
  • Hey, thanks for your answer.. I thought that I have a syntax error thats why I didn't provide a sample. I edit the main post with a sample and the for loop which I use Commented Dec 11, 2013 at 10:26
  • Your code has errors: at the moment you are checking the 6th token and if it is not "0" you are setting a variable to "token6/token2" otherwise set the variable to "token2" alone. Is that it? is the folder delimiter really / or \ ? Commented Dec 11, 2013 at 10:39
  • The 7th token can either be the path or 0, if it is NOT 0 then its the path and I assign it to the variable. Yes thats it.. where is the error? I don't see it :/ I tried it with / and it didn't work - with \ it worked so I think its \.. thats why i change the / in the paths with \ Commented Dec 11, 2013 at 10:41
  • set rootFolder ==f is a syntax error. You also need to use delayed expansion to change a variable within a loop, and use !variable! syntax. Commented Dec 11, 2013 at 10:43

1 Answer 1

2

I think this should be functionally the same, given the sample data, and the fact that the 6th token only has a single slash.

@echo off
setlocal enabledelayedexpansion
For /F "tokens=1-7 delims=;" %%a in (L:\Mappe1.csv) Do (
   if "%%b"=="Folder" (
      set "finalpath=%%c"
      if NOT "%%f"=="0" set "finalpath=%%f%%c"
   )
set "finalpath=!finalpath:/=!"
echo "!finalpath!"
)
pause
Sign up to request clarification or add additional context in comments.

6 Comments

Your code is working like a charm. Thanks for that. I'll mark your answer but I have another question at this point: what is this line doing? set "finalpath=!finalpath:/=!" you are replacing every slash with nothing right? How can I replace every slash with a backslash at this point?
set "finalpath=!finalpath:/=\!" <--- this will replace forward slashes with backslashes. And yes, I was replacing every forward slash with nothing, as the example just had one slash.
Perfect, I undestand now much more how the syntax works. Thank you very much!
Hey, its me again.. I have one problem now. My CSV sometimes has empty columns, so its displayed like this ;;text;0;;1;. Now, if I ask for token %%f it skips empty columns and graps the next column with text in it. Sometimes it is a 0. The problem is now that the code above with the 0 check doesn't work at this point.. any idea? I tried to replace every 0 with an emptystring - but sometimes I have a string with a foldername with a 0 in it so this will not do it...
Can you zip up some sample CSV files and put them on dropbox? It would help to see the actual data and see if there is a pattern than can be used.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.