how to write content separated by comma in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
babhuko
Posts: 4
Joined: 17 Nov 2012 07:44

how to write content separated by comma in a text file

#1 Post by babhuko » 17 Nov 2012 08:01

Hi all,

I executed the command

DIR *.csv \b > sample.txt

The output is
file1.csv
file2.csv
file3.csv

But i need output like
file1.csv,file2.csv,file3.csv

Note : After file3.csv, comma should not be there. Please throw some light on this. Thank you..

Regards,
babhuko

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

Re: how to write content separated by comma in a text file

#2 Post by foxidrive » 17 Nov 2012 08:23

% and ^ and comma characters in the filenames will cause a problem.

Code: Select all

@echo off
for /f "delims=" %%a in ('DIR *.csv /b 2^>nul') do (
set /p ="%%a," <nul >>"sample.txt"
)
set /p "var=" <"sample.txt" >nul
set "var=%var:~0,-1%"
echo(%var%>sample.csv
del "sample.txt"

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

Re: how to write content separated by comma in a text file

#3 Post by Aacini » 17 Nov 2012 12:32

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set line=
for %%a in (*.csv) do set "line=!line!,%%a"
echo !line:~1!> sample.txt

babhuko
Posts: 4
Joined: 17 Nov 2012 07:44

Re: how to write content separated by comma in a text file

#4 Post by babhuko » 19 Nov 2012 06:23

Thanks for the reply. Its working.

I have another issue.

Code: Select all

@echo off
SET DATE_DIS=none
set DATE_DIS=%date%%time%
move %1%3 %2
cd %2
rename %3 %3_%DATE_DIS%
pause


In this error is in the rename command.
I dont know what it is. %3 is the filename am passing as a parameter from ETL tool. Before rename it works. but only rename throws some error. Please advice.


Thanks,
Dosuser

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

Re: how to write content separated by comma in a text file

#5 Post by foxidrive » 19 Nov 2012 07:05

add echo before the rename

ECHO rename %3 %3_%DATE_DIS%

And see what is on the screen when the pause prompt appears.

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

Re: how to write content separated by comma in a text file

#6 Post by foxidrive » 19 Nov 2012 07:06

The date might contain / and the time might contain : which are both illegal filename characters.

babhuko
Posts: 4
Joined: 17 Nov 2012 07:44

Re: how to write content separated by comma in a text file

#7 Post by babhuko » 19 Nov 2012 09:35

@foxidrive,

Thanks for your info. Could you please let me know how to avoid that error.

I mean l have given DATE_DIS = %date%%time%. Any other format there like only with numerals to avoid this issue.

For eg
yyyymmddhhmiss

Please advise.

Thanks

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

Re: how to write content separated by comma in a text file

#8 Post by Aacini » 19 Nov 2012 11:14

Code: Select all

set DATE_DIS=%date:/=%%time::=%

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

Re: how to write content separated by comma in a text file

#9 Post by foxidrive » 19 Nov 2012 15:46

Code: Select all

:: timestamp YYYYMMDD_HHMMSS
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set datetime=%dt:~0,8%_%dt:~8,6%


This is a good method and works in any region.

babhuko
Posts: 4
Joined: 17 Nov 2012 07:44

Re: how to write content separated by comma in a text file

#10 Post by babhuko » 21 Nov 2012 08:31

@foxidrive

I have used your datetime code. but still the same issue. Please advise

Code: Select all

@echo off
SET DATE_DIS=none
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set DATE_DIS=%dt:~0,8%_%dt:~8,6%
move %1%3 %2
cd %2
rename %3 %3_%DATE_DIS%
pause

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

Re: how to write content separated by comma in a text file

#11 Post by foxidrive » 21 Nov 2012 09:17

This will echo the rename command - see what it shows.

Code: Select all

@echo off
SET DATE_DIS=none
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set DATE_DIS=%dt:~0,8%_%dt:~8,6%
move %1%3 %2
cd %2
echo rename %3 %3_%DATE_DIS%
pause

Post Reply