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
Almost there - Filenames in quotes separated by commas
Moderator: DosItHelp
Re: Almost there - Filenames in quotes separated by commas
This method do what you want, but it is limited to a list of up to 8190 characters:
This method have not such a restriction:
Both methods eliminate exclamation-marks and carets from file names; this behavior may be fixed, if needed.
Antonio
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
Re: Almost there - Filenames in quotes separated by commas
The extra space is in your original file because of this line:
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
or
But really, you can easily produce your desired output directly, with no need for JREPL.
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
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
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
Re: Almost there - Filenames in quotes separated by commas
Thank you so much, both of you - I got it working and am very happy now
Aisha
Aisha