NUL device in DOS and Windows

General FreeBASIC programming questions.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

NUL device in DOS and Windows

Post by fzabkar »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: NUL device in DOS and Windows

Post by MrSwiss »

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: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: NUL device in DOS and Windows

Post by fzabkar »

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_ ... folder.php
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: NUL device in DOS and Windows

Post by MrSwiss »

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: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: NUL device in DOS and Windows

Post by fzabkar »

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: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: NUL device in DOS and Windows

Post by marcov »

MrSwiss wrote:Try this: If FileExists( blockdir & "\." ) Then
(minor detail: afaik that doesn't work for roots)
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: NUL device in DOS and Windows

Post by fzabkar »

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
Post Reply