I have come across an strange issue that puzzles me at the moment -
The windows script I am running is a wrapper script that will loop through all the csv files from a public directory and will move them to a processing folder before I call one of the sub-processes. The wrapper script is supposed to terminate when there is no more csv files left in the input folder. However I am getting a message saying
'A duplicate file name exists, or the file cannot be found.'
The input folder \abcinc.lcl\utility\aaa sits as an empty folder with no other files or sub-folders under it.
Any reasons why the following for loop is not terminating normally upon no files and failing instead?
for /F %%i in ('dir /b "\abcinc.lcl\utility\aaa*.csv"') do (
Here is the full code snippet along with the output from the log -
***************************************************************************
**** Wrapper Script ****
***************************************************************************
@echo off
Setlocal enabledelayedexpansion
for /F %%i in ('dir /b "\\abcinc.lcl\utility\aaa\*.csv"') do (
echo Folder is NON empty
move "\\abcinc.lcl\utility\aaa\*.csv" E:\abc\INFILES
For %%a in (E:\abc\INFILES\*.csv) Do (
PING 10.1.41.19 -n 5 >NUL
Set "File=LOADIN.csv"
Ren "%%a" "!File!"
CALL "E:\abc\scripts\RUNALL.BAT"
PING 10.1.41.19 -n 5 >NUL
MOVE "E:\abc\INFILES\LOADIN.CSV" "E:\abc\INFILES\ARCHIVE\LOADIN - %DATE:/=-% %TIME::=-%.CSV"
PING 10.2.23.49 -n 3 >NUL
MOVE "\\abcinc.lcl\utility\aaa\OUTPUT\outfile.csv" "\\abcinc.lcl\utility\aaa\OUTPUT\ARCHIVE\outfile - %DATE:/=-% %TIME::=-%.CSV"
)
)
EXIT
***********************************************************************
**** LOG OUTPUT ****
***********************************************************************
Folder is NON empty
\\abcinc.lcl\utility\aaa\File1.csv
\\abcinc.lcl\utility\aaa\File2.csv
2 file(s) moved.
RunAll script ran fine.
1 file(s) moved.
1 file(s) moved.
RunAll script ran fine.
1 file(s) moved.
1 file(s) moved.
Folder is NON empty
A duplicate file name exists, or the file
cannot be found.
movecommand moves all the files to theinfilesfolder, but if there were more than one file thefor /fkeeps iterating over the output of the initialdircommand.dircommand will generate three lines of output. Thefor /fiterates over these three lines so the code in thedoclause is executed three times no matter if you have moved the files and they are not still on the source folder. As you are not using the%%ivalue you can replace the initialfor /fwith just adirand an&&conditional execution operator to execute the rest of the code if thedirdidn't fail.