I have a script which uses List = FileItemize('*.*')
then uses filters on the list to create sub-folders and move matching files into them. Recently, I ran the script and kept getting an error which took me quite awhile to figure out. This code illustrates the problem.
intcontrol(73, 2, 0, 0, 0) ; GOSUB error handler.
ErrorList = ''
Chk = @false
; Create a test folder.
Dir1 = 'D:\TestDirs'
dirmake(Dir1)
dirchange(Dir1)
; Create a subfolder.
Dir2 = ' Test '
; This line should cause an error.
if !direxist(Dir2) then Chk = dirmake(Dir2)
else Chk = @true
if !Chk
pause('Folder_From_fName Error.', 'Could not create folder':@crlf:Root:Dir)
return
endif
; Create some test files.
fileput('! test1.txt', 'test1')
fileput('! test2.txt', 'test2')
fileput('! test3.txt', 'test3')
fMove = fileitemize('*.txt')
; And now we error out!
filemove(fMove, Dir2, @false)
:cancel
if ErrorList!=''
clipput(ErrorList)
pause(`Error List on Clipboard!`, `These files were not moved to`:Dir2:@crlf:strreplace(ErrorList,@tab,@crlf)) ;***DEBUG LINE***
endif
Dirs = diritemize('*.*')
Cnts = itemcount(Dirs, @tab)
Test = itemextract(Cnts, Dirs, @tab)
TestLen = strlen(Test)
Dir2Len = strlen(Dir2)
pause(`DEBUG PAUSE`, strCat(`TestLen = `, TestLen, @CRLF, `Dir2Len = `, Dir2Len)) ;***DEBUG LINE***
if direxist(Dir2) then dirremove(Dir2) ; Removes the folder that we can't move files to!
return
;__________________________________________________________________________________________
:WBERRORHANDLER
Error = LastError()
if Error==1003 || Error==1008 ;FileMove: Failed
; Deal with it.
ErrorList = iteminsert(fMove, -1, ErrorList, @lf)
intcontrol(73, 2, 0, 0, 0) ; GOSUB error handler.
return
endif
ErrInfo = strcat('Error: #', Error, ': ', wberrortextstring, @crlf, 'Script: ', wberrorhandlerline, @crlf, 'Assignment: ', wberrorhandlerassignment, @crlf, 'File: ', wberrorhandlerfile, @crlf, 'Procedure: ', wberrorinsegment, @crlf, 'Line No. ', wberrorhandlerlinenumber, @crlf)
clipput(ErrInfo)
pause('Error Message on Clipboard', ErrInfo)
exit
This line sets Chk to @trueif !direxist(Dir2) then Chk = dirmake(Dir2)
but Winbatch doesn't create a folder ' Test ' it creates a folder ' Test' and returns @true. Maybe DirMake(Dir) could throw a minor error when Dir contains trailing spaces or FileMove could be modified to ignore trailing spaces.
FWIW, I did a little testing at the command line. I'm assuming the WB calls are just passthrus to the underlying Windows calls - so the results should be about the same as the command line results.
It looks like leading spaces are preserved, but trailing spaces are silently ignored.
Try (at the CMD.EXE prompt):
md " Test "
dir
if exist " Test" echo Hi
and so on...
Yes, I never thought the problem was with Winbatch itself, right down to
Quoteif exist " test " echo HI
which gets you a nice
HI at the command prompt.
Since posting the problem, I've given it some more thought and now think that DirMake(DIR) should return an error, if DIR has trailing spaces. I think that having FileMove() silently ignore trailing spaces would not be good practice.
Thank you. The developers have been notified of this request.