WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: George Vagenas on August 31, 2013, 01:16:04 AM

Title: A problem with DirMake
Post by: George Vagenas on August 31, 2013, 01:16:04 AM
I have a script which uses
Code (winbatch) Select
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.
Code (winbatch) Select
   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 @true
Code (winbatch) Select
if !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.
Title: Re: A problem with DirMake
Post by: snowsnowsnow on August 31, 2013, 04:59:09 AM
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...
Title: Re: A problem with DirMake
Post by: George Vagenas on August 31, 2013, 01:28:20 PM
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.
Title: Re: A problem with DirMake
Post by: Deana on September 03, 2013, 09:59:50 AM
Thank you. The developers have been notified of this request.