Page 1 of 1

Dir output into a file in one row with comma seperation

Posted: 09 Nov 2010 10:42
by bob_carswell
Hi, I am taking the output of a dir command being done as shown below

Code: Select all

rem ***********************************************
rem * Estabalish the available manifest file names
rem * Extract the first four characters of file name
rem ***********************************************

cd D:\Adsoft\SIMS\ManifestFiles
for /f  %%a in ('dir /o-d /b *.mfs') do (
   set FileName=%%a
   set str=%%a
   set str=!str:~0,4!

rem *************************************
rem * Remove the leading two zero digits
rem *************************************
   set str=!str:~-2!

rem ******************************************************
rem * Remove the leading zero if file number less that 10
rem ******************************************************
   if "!str:~0,1!" == "0" (
   set str=!str:~-1!
   )
   echo !str! >> %~dp0FileNameA.csv
   echo !FileName! >> %~dp0FileNameB.csv
)
set /p ManifestFileNumber=<%~dp0FileNameA.csv



The result is going into the csvfile in one column as shown in example below

50
40
30
20
10

I want to extract the output instead into a csv file that is one row only with comma separation between groups as shown in example below

50,40,30,20,10,

I need to use the numbers one at a time from the csv in another 'for' loop extracting the numbers with something like
the code below to be used with an application that is running the original file names from D:\Adsoft\SIMS\ManifestFiles directory.

Code: Select all

set Count=1
set EndCount=<number of original files in directory>

if !Count! gtr !EndCount! goto endloop
set CSV=%~dp0FileNameA.csv
for /F "tokens=%Count% delims=," %%a in (%CSV%) do (

etc




I am having trouble getting the extracted numbers into a comma delimited csv file in one row only, can anyone help please.

Tks

Re: Dir output into a file in one row with comma seperation

Posted: 09 Nov 2010 18:30
by amel27
bob_carswell wrote:I am having trouble getting the extracted numbers into a comma delimited csv file in one row only, can anyone help please.
sample via "set /p" triks:

Code: Select all

@echo off
SetLocal EnableDelayedExpansion

cd /D D:\Adsoft\SIMS\ManifestFiles

for /f "delims=" %%a in ('dir /o-d/b "*.mfs" 2^>nul') do (set "str=%%a"
  <nul set /p $="%%a,">>"%~dp0FileNameA.csv"
  for /f "tokens=* delims=0" %%b in ("!str:~,4!") do (
  <nul set /p $="%%b,">>"%~dp0FileNameB.csv"
))

Re: Dir output into a file in one row with comma seperation

Posted: 10 Nov 2010 03:43
by bob_carswell
Hi amel27

Many thanks for your code it does the job really well, appreciate your help. :D