batch script error using a for loop to populate an array of filenames: ) was unexpected at this time

98 Views Asked by At

I'm stuck.

I've tried to simplify the code and have used Rob van der Woude's LogBatch.bat debugger. That helped me narrow the problem to a for loop that populates an array of file names by performing a dir search within the for command. It appears to run as long as there are file names that match. When no file names match, or when it runs out of matches, I get the error.

.

Error

) was unexpected at this time.

.

Code Snippet

setlocal enabledelayedexpansion

set /A "filecount=-1"
for /F %%i in ('dir /b "C:\files\Spreadsheets\TestFiles\ExportedData*.csv"') do (
    set /A "filecount=filecount+1"
    set "filematches[!filecount!]=%%i"
)

.

Sample files

ExportedData12-31-15.csv

ExportedData2015-12-31.csv

.

Does anyone know how to fix this error? I would appreciate an explanation as well. I'd like to understand the problem.

Also, if there is an alternate approach to populate the array with the file names, I'd be interested in suggestions.

Please limit answers only to solutions that use native CMD.exe batch scripts and localized variables. (Please avoid global variables.) Please do not suggest any .NET, vbscript, or jscript/javascript solutions.

The directory path is fixed. The example path has no spaces, but I'd like the option of handling spaces without reverting down to 8.3 file names.

The example file names should adhere to the convention of FileTitleDATE.ext.

Thank you.

=========================================================

EDIT: Feb. 2nd, 2016

Here is the complete batch script. It is unfinished and for testing, I have commented out the ren (rename), del (delete), and echo stuff to >> logfile.log. I am providing 3 versions on pastebin...

@Magoo @CoveGeek @Mofi Links here

I don't know if the full source will help, but I hope it will. I was hesitant given the length and the multiple versions.

0

There are 0 best solutions below