user1684922
user1684922

Reputation: 5

writing variables to a CSV file within a windows batch file/script

@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "drives=d e f g h i j k l m n o p q r s t u v w x y z"
SET "sourcedir=normal"
REM locate drive that has directory "?:\normal\f"
FOR %%d IN (%drives%) DO IF EXIST "%%d:\%sourcedir%\f\." SET "sourcedir=%%d:\%sourcedir%"&SET "destdir=%%d:\video"&GOTO founddrive
ECHO Could not find "%sourcedir%\f" on any drive
GOTO :eof

:founddrive
SET /a destnum=1
FOR /f "delims=" %%a IN (
    'dir /b /a-d "%sourcedir%\f\*.TS"'
) DO (
    MD "%destdir%!destnum!" 2>NUL
    SET "filename=%%~na"
    SET "filename=!filename:~0,-1!R%%~xa"
    MOVE /y "%sourcedir%\F\%%a" "%destdir%!destnum!\video.MP4"
    MOVE /y "%sourcedir%\R\!filename!" "%destdir%!destnum!\video2.MP4"
    For /f "Tokens=1,2 Delims= " %%A in ('dir /T:C "%destdir%!destnum!\video.MP4" ^| Find /I "%destdir%!destnum!\video.MP4"') Do Echo " Starting Date,%%A,,,,,,,,,,,,,,,,,,,,,,,,,, ">>%destdir%!destnum!\info.csv
    For /f "Tokens=1,2 Delims= " %%A in ('dir /T:C "%destdir%!destnum!\video.MP4" ^| Find /I "%destdir%!destnum!\video.MP4"') Do Echo " Starting Time,%%B,,,,,,,,,,,,,,,,,,,,,,,,,, ">>%destdir%!destnum!\info.csv
    SET /a destnum+=1
)

thanks to @Magoo for helping with the core of this script. i found i need to do 1 more task and generate a CSV file with the date and time. where i am stuck is with

this creates info.csv but gives me 2 lines in the correct directory but the inf.csv file is not formatted as a proper CSV. everything but the variables are in 1 cell comma's included.

Do Echo " Starting Date,%%A,,,,,,,,,,,,,,,,,,,,,,,,,, ">>%destdir%!destnum!\info.csv

if i remove the quotes the whole script bombs out

Do Echo Starting Date,%%A,,,,,,,,,,,,,,,,,,,,,,,,,,>>%destdir%!destnum!\info.csv

thanks in advance.

Upvotes: 0

Views: 435

Answers (1)

Magoo
Magoo

Reputation: 79982

Hmm. It shouldn't.

dir /T:C "%destdir%!destnum!\video.MP4" output should not contain the string "%destdir%!destnum!\video.MP4" so no matches should be found, hence neither echo should occur. Now video.mp4 is far more likely to generate a hit.

So, to generate the .csv, try

 For /f "Tokens=1,2 Delims= " %%A in ('dir /T:C "%destdir%!destnum!\video.MP4" ^| Find /I "video.MP4"') Do (
  Echo "Starting Date","%%A",,,,,,,,,,,,,,,,,,,,,,,,,,
  Echo "Starting Time","%%B",,,,,,,,,,,,,,,,,,,,,,,,,,
  )>"%destdir%!destnum!\info.csv"

Note that there's no point in running dir twice. The syntax (...)>filename will create a new file filename containing the output-to-console from any commands between the parentheses. Use >> to append to any existing filename if desired.

Personally, I'd establish a variable which I'll arbitrarily and for no particular reason call commas containing a long string of potatoes and then replace the comma-sequence in the echoes with %commas:~-x% where x is the number of commas required. Saves going cross-eyed counting the little perishers, especially if there's a need to change the count.

Upvotes: 1

Related Questions