NUL device in DOS and Windows

General FreeBASIC programming questions.
fzabkar
Posts: 5
Joined: Sep 29, 2018 2:52

NUL device in DOS and Windows

Postby fzabkar » Jul 10, 2019 23:19

I am trying to use the following code to create the next available directory named "Blocks_nn", where nn = 00, 01, 02, etc.

When I compile the code for DOS, it works as expected. However, when I compile it for Win32, it finds no spare directories. It would appear that the NUL device is treated differently by the two platforms.

Is there a universal way to achieve the same end?

Code: Select all

' Find the next available subdirectory for module components and create it

For i = 0 To &HFF
   blockdir = "Blocks_" & Hex( i, 2 )

   If FileExists( blockdir & "\NUL" ) Then
      blockdir = ""
      Continue For
   Else
      Mkdir(  blockdir )
      Exit For
   End If
Next i

If blockdir = "" Then
   Print "No spare Blocks_nn directory  -  program aborted"
   End
End If
MrSwiss
Posts: 3190
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: NUL device in DOS and Windows

Postby MrSwiss » Jul 10, 2019 23:32

Try this: If FileExists( blockdir & "\." ) Then
instead of: If FileExists( blockdir & "\NUL" ) Then
Using dev/nul in a file-system search, seems like a "non-fit" somehow.
fzabkar
Posts: 5
Joined: Sep 29, 2018 2:52

Re: NUL device in DOS and Windows

Postby fzabkar » Jul 10, 2019 23:44

Thanks, I'll try it at my next opportunity.

BTW, using the NUL device to test for the existence of a directory was an old DOS Batch trick.

https://www.robvanderwoude.com/battech_ifexistfolder.php
MrSwiss
Posts: 3190
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: NUL device in DOS and Windows

Postby MrSwiss » Jul 11, 2019 0:42

fzabkar wrote:It would appear that the NUL device is treated differently by the two platforms.
As described clearly in the Article that you've linked!

Btw. I've made every possible attempt, to forget all those "old" DOS workarounds!
(DOS is dead and gone ... my point of view)
fzabkar
Posts: 5
Joined: Sep 29, 2018 2:52

Re: NUL device in DOS and Windows

Postby fzabkar » Jul 12, 2019 4:05

In the following example there is only one existing directory (Blocks_00). DOS correctly finds the next available directory (Blocks_01), but Win32 doesn't.

Code: Select all

#include "vbcompat.bi"

Dim i As Integer
Dim blockdir As String

' Find the next available subdirectory and create it

Print "Testing for Blocks_nn\."
Print

For i = 0 To &HFF
   blockdir = "Blocks_" & Hex( i, 2 )

   If FileExists( blockdir & "\." ) Then
      Print " " & blockdir & " exists"
      blockdir = ""
      Continue For
   Else
      Print " " & blockdir & " is the next available directory"
      Goto testnul
   End If
Next i

If blockdir = "" Then
   Print "No spare Blocks_nn directory - program aborted"
End If


testnul:

Print
Print "Testing for Blocks_nn\NUL"
Print

' Find the next available subdirectory and create it

For i = 0 To &HFF
   blockdir = "Blocks_" & Hex( i, 2 )

   If FileExists( blockdir & "\NUL" ) Then
      Print " " & blockdir & " exists"
      blockdir = ""
      Continue For
   Else
      Print " " & blockdir & " is the next available directory"
      End
   End If
Next i

If blockdir = "" Then
   Print "No spare Blocks_nn directory - program aborted"
End If


results for testdir.bas when compiled for Win32 ...

Code: Select all

Testing for Blocks_nn\.

 Blocks_00 is the next available directory

Testing for Blocks_nn\NUL

 Blocks_00 exists
 Blocks_01 exists
 ...
 Blocks_FF exists
No spare Blocks_nn directory - program aborted


results for testdir.bas when compiled for DOS ...

Code: Select all

Testing for Blocks_nn\.

 Blocks_00 exists
 Blocks_01 is the next available directory

Testing for Blocks_nn\NUL

 Blocks_00 exists
 Blocks_01 is the next available directory
marcov
Posts: 2757
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: NUL device in DOS and Windows

Postby marcov » Jul 12, 2019 8:18

MrSwiss wrote:Try this: If FileExists( blockdir & "\." ) Then


(minor detail: afaik that doesn't work for roots)
fzabkar
Posts: 5
Joined: Sep 29, 2018 2:52

Re: NUL device in DOS and Windows

Postby fzabkar » Jul 12, 2019 8:41

This code works in DOS and Win32:

Code: Select all

#include "vbcompat.bi"
#include "dir.bi"

Dim i As Integer
Dim blockdir As String

' Find the next available subdirectory for module components and create it

For i = 0 To &HFF
   blockdir = "Blocks_" & Hex( i, 2 )

   If Dir( blockdir, fbDirectory ) <> "" Then
      blockdir = ""
      Continue For
   Else
      Mkdir( blockdir )
      Exit For
   End If
Next i

If blockdir = "" Then
   Print "No spare Blocks_nn directory - program aborted"
End If

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests