Solved Read .csv and use variables

November 4, 2011 at 08:26:36
Specs: Windows Server 2008
I am trying to read a .csv file and using the 3 inputs per line create a directory and move a file into that directory. I had a script that would do this from a previous project but for this project I need it to work in batch script. I need help figuring out the correct syntax and conversion to get this to work in batch.

Here is the script that works but needs to be converted to batch:
sed 's/$/\r/' < u:/MassMedia/MediaMatch.txt > u:/MassMedia/MediaMatchTemp.txt
LINE=`cat u:/MassMedia/MediaMatchTemp.txt`
for LINENUM in $LINE ; do
echo "$LINENUM"
ISSUER_ID=`echo $LINENUM | cut -f1 -d ","`
PACKAGE_ID=`echo $LINENUM | cut -f3 -d ","`
ACCOUNT_ID=`echo $LINENUM | cut -f2 -d ","`

mkdir "o:/Documents_and_Media/$PACKAGE_ID"

mkdir "o:/Documents_and_Media/$PACKAGE_ID/$ACCOUNT_ID"

cp $FOO "o:/Documents_and_Media/$PACKAGE_ID/$ACCOUNT_ID/"


Here is my terrible attempt at trying to convert it to batch that isn't working:
for/F "tokens=1,2,3 delims=," %%G in (MoveFiles.txt) ; do
echo %%G %%H %%I
SET FILE_NAME=`echo %%I`

mkdir "o:\Documents and Media\%PACKAGE_ID%"

mkdir "o:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%"

COPY %FOO% "O:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%\%FILE_NAME%"


Can anyone please help guide me in the correct syntax for what i am trying to do?
Thank you so much,

See More: Read .csv and use variables

Report •

November 4, 2011 at 11:25:45
I have the below script working- doing exactly what I want it to do...
EXCEPT it only creates a directory and moves a file for the very last line of the .csv file. It seems to ignore any lines but the last one.
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2,3 delims=," %%G in (MoveFiles.txt) do (
@set "Account_ID=%%G"
@set "PACKAGE_ID=%%H"
@set "FILE_NAME=%%I")

mkdir -p "O:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%"

SET FOO="O:\MediaAttachments\%FILE_NAME%"
COPY %FOO% "O:\Documents and Media\%PACKAGE_ID%\%ACCOUNT_ID%\%FILE_NAME%"

Report •

November 4, 2011 at 14:11:57
✔ Best Answer
@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2,3 delims=," %%G in (MoveFiles.txt) do (
  set Account_ID=%%G 
  set PACKAGE_ID=%%H 
  set FILE_NAME=%%I
  if not exist "O:\Documents and Media\!PACKAGE_ID!\!ACCOUNT_ID!" ( 
    mkdir "O:\Documents and Media\!PACKAGE_ID!\!ACCOUNT_ID!"
  set FOO="O:\MediaAttachments\!FILE_NAME!"
  copy !FOO! "O:\Documents and Media\!PACKAGE_ID!\!ACCOUNT_ID!\!FILE_NAME!"

Report •

November 4, 2011 at 19:06:12
That worked perfectly. Thank you so much!

Report •

Related Solutions

Ask Question