Reputation: 1688
Im not entirely familiar with command line scripting. I am trying to create a batch file to read in a txt file, apply a format which converts the file from a report into table and then export the file as spreadsheet.
I can get the script to work for an individual file however if I try to run multiple files through a loop nothing happens. It launches the program goes through each script line but again doesn't do anything. It doesn't set the path. No crashes. Where as where I take the script out of the loop and apply it to a single file it works fine. Any suggestions?
SET monarch_bin="C:\Program Files (x86)\Datawatch\Monarch\Program\Monarch.exe"
for /f "delims=" %%i in ('dir /b "C:\Users\Me\Desktop\Sample Monarach Input\*.txt"') do (
echo File directory : %%i
SET infilename=/Rpt: "C:\Users\Me\Desktop\Sample Monarach Input\%%i.txt"
echo ####### In file name:%infilename%
SET model_file=/mod:"C:\Users\Me\Desktop\Sample Monarach Input\InvoiceSummary.xmod"
echo ####### Model: %model_file%
SET outfilename=/exp:"C:\Users\Me\Desktop\Sample Monarach Input\OUTPUT.xlsx"
echo ####### Out file name:%outfilename%
SET mode=/expfileopt:append /T
echo ####### File: %%i
%monarch_bin%%infilename%%model_file%%outfilename%%mode%
pause
)
Upvotes: 0
Views: 1397
Reputation: 70923
Delayed expansion problems (explanation here)
setlocal enabledelayedexpansion
SET monarch_bin="C:\Program Files (x86)\Datawatch\Monarch\Program\Monarch.exe"
for /f "delims=" %%i in ('dir /b "C:\Users\Me\Desktop\Sample Monarach Input\*.txt"') do (
echo File directory : %%i
SET infilename=/Rpt:"C:\Users\Me\Desktop\Sample Monarach Input\%%i.txt"
echo ####### In file name: !infilename!
SET model_file=/mod:"C:\Users\Me\Desktop\Sample Monarach Input\InvoiceSummary.xmod"
echo ####### Model: !model_file!
SET outfilename=/exp:"C:\Users\Me\Desktop\Sample Monarach Input\OUTPUT.xlsx"
echo ####### Out file name: !outfilename!
SET mode=/expfileopt:append /T
echo ####### File: %%i
%monarch_bin% !infilename! !model_file! !outfilename! !mode!
)
If a variable value is changed inside a block of code you need delayed expansion to retrieve the changed value inside the same block of code.
But, this can be written as
set "monarch_bin=C:\Program Files (x86)\Datawatch\Monarch\Program\Monarch.exe"
set "inputFolder=C:\Users\Me\Desktop\Sample Monarach Input"
for %%i in ("%inputFolder%\*.txt") do (
"%monarch_bin%" ^
/Rpt:"%%~fi" ^
/mod:"%%~dpiInvoiceSummary.xmod" ^
/exp:"%%~dpioutput.xlsx" ^
/expfileopt:append /T
)
Where the for
loop will iterate over the list of the .txt
files, and %%i
will hold a reference to the file so, %%~fi
is the full path to the file and %%~dpi
is the drive and path where the file is stored.
Upvotes: 1