How to use for loop in the file name with space

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
komathi
Posts: 14
Joined: 18 May 2014 04:48

How to use for loop in the file name with space

#1 Post by komathi » 22 Jun 2014 12:38

Hi
I'm using the below code to get the count of the lines in csv file, It works fine if the folder name is without space,
but if space available in the folder name then not getting the count.

Code: Select all

SET rootpath=%~dp0
set rootpath=%rootpath:~0,-4%
set /a CustomerCodeUsageCounter=0
FOR /F "DELIMS=, TOKENS=1,2" %%p IN ( %rootpath%InputCSV\Code.csv) Do (
    set "line=%%a"
    set "line="!line:,=","!""
  set /a CustomerCodeUsageCounter+=1
    )

what modification need on this code?,

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to use for loop in the file name with space

#2 Post by penpen » 22 Jun 2014 13:11

if there are spaces in the path you should use "usebackq" option (and add doublequotes to the path):

Code: Select all

SET rootpath=%~dp0
set rootpath=%rootpath:~0,-4%
set /a CustomerCodeUsageCounter=0
FOR /F "TOKENS=1,2 usebackq DELIMS=," %%p IN ("%rootpath%InputCSV\Code.csv") Do (
   set "line=%%a"
   set "line="!line:,=","!""
   set /a CustomerCodeUsageCounter+=1
)

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to use for loop in the file name with space

#3 Post by foxidrive » 23 Jun 2014 06:28

komathi wrote:I'm using the below code to get the count of the lines in csv file


This should get the number of lines in a CSV file.

Code: Select all

@echo off
for /f %%a in (' find /c /v "" ^< "file.csv" ') do echo %%a


Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: How to use for loop in the file name with space

#4 Post by Yury » 23 Jun 2014 09:44

With blank lines:

foxidrive wrote:

Code: Select all

@echo off
for /f %%a in (' find /c /v "" ^< "file.csv" ') do echo %%a



.


Without blank lines:

Code: Select all

@echo off
(for /f "usebackq eol= delims=" %%a in ("file.csv") do set /a n+=1)& call echo %%n%%


or

Code: Select all

@echo off
for /f "eol=" %%a in ('findstr /v "^$" "file.csv"^| find /c /v ""') do echo %%a


.

Post Reply