Almost there - Filenames in quotes separated by commas

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Almost there - Filenames in quotes separated by commas

#1 Post by aisha » 16 Jan 2017 22:41

Hello group,
I am getting stuck with jrepl and am almost there...

We have a folder with many filenames:
c:\folder\file1.txt
c:\folder\file2.txt
c:\folder\file3.txt

And are trying to produce a TXT file with the file names listed in quotes, separated by a comma, and on the same line so:
"file1.txt","file2.txt","file3.txt"

What we have so far is:
:: Create a file, listing each filename in a directory
cd c:\folder\
for /f "delims=" %%A in ('dir /b /S') do echo %%~nxA >>c:\files\r\list.txt

:: Place quotes before and after each filename, insert a quote, and put all the entries on the same line
call jrepl.bat "[^\r\n]+|(\r?\n)+(?=[^\r\n])" "\q$&\q|," /x /m /t "|" /f "c:\files\r\list.txt" /o "c:\files\r\list2.txt"

BUT this is producing a file with the following results
"file1.txt ","file2.txt ","file3.txt "

AND I cannot figure out why the script is putting in an extra space after each filename.

Any advice (or a better way)?

Aisha

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Almost there - Filenames in quotes separated by commas

#2 Post by Aacini » 16 Jan 2017 23:16

This method do what you want, but it is limited to a list of up to 8190 characters:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "list="
cd c:\folder\
for /R %%A in (*.*) do set list=!list!,"%%~nxA"
echo %list:~1%> "c:\files\r\list.txt"

This method have not such a restriction:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "name="
cd c:\folder\
< NUL (
   for /R %%A in (*.*) do (
      if defined name set /P "=""!name!",""
      set "name=%%~nxA"
   )
   echo "!name!"
) > "c:\files\r\list.txt"

Both methods eliminate exclamation-marks and carets from file names; this behavior may be fixed, if needed.

Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Almost there - Filenames in quotes separated by commas

#3 Post by dbenham » 16 Jan 2017 23:21

The extra space is in your original file because of this line:

Code: Select all

echo %%~nxA >>c:\files\r\list.txt

The space before the redirection is included in the output. You could get rid of the trailing space and all would probably be good. but there is a chance it could fail if you ran across a file with a name like "file 1" because then the 1 would be interpreted as the file handle representing stdout, and so 1 would not be included in the output.

A safer option would be to use

Code: Select all

(echo %%~nxA) >>c:\files\r\list.txt
or

Code: Select all

>>c:\files\r\list.txt echo %%~nxA


But really, you can easily produce your desired output directly, with no need for JREPL.

Code: Select all

@echo off
setlocal disableDelayedExpansion
set first=1
<nul >c:\files\r\list.txt (
  for /r "c:\folder" %%F in (*) do (
    if defined first (
      set /p "=""%%~nxF"""
      set "first="
    ) else set /p "=","%%~nxF"""
  )
)

SET /P is used to print a value without a new line. The syntax is a bit weird to get the quotes. The outer most two sets are dropped.

The code is made much faster by enclosing the entire loop within an extra set of parentheses and redirecting just once.

EDIT: I just realized Aacini posted his answer as I was writing mine. They are very similar. However this answer does not have any limitation regarding exclamation points or line lengths.

Dave Benham

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Almost there - Filenames in quotes separated by commas

#4 Post by aisha » 17 Jan 2017 00:16

Thank you so much, both of you - I got it working and am very happy now

:)
Aisha

Post Reply