Discussion:
Is it a file or a folder...in a network share
(too old to reply)
Tom Del Rosso
2024-07-29 22:57:26 UTC
Permalink
if exist "%file%\" (set type=Folder) else (set type=File)

That works for a local file, but if %file% points to a network share
then it always says Folder.

Are you seeing that too?

This is for win7 (not this newsreader machine).
--
Defund the Thought Police
JJ
2024-07-30 03:15:25 UTC
Permalink
Post by Tom Del Rosso
if exist "%file%\" (set type=Folder) else (set type=File)
That works for a local file, but if %file% points to a network share
then it always says Folder.
Are you seeing that too?
This is for win7 (not this newsreader machine).
Check it like this.

if exist "%file%\nul" (set type=Folder) else (set type=File)
JJ
2024-07-30 03:29:19 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
if exist "%file%\" (set type=Folder) else (set type=File)
That works for a local file, but if %file% points to a network share
then it always says Folder.
Are you seeing that too?
This is for win7 (not this newsreader machine).
Check it like this.
if exist "%file%\nul" (set type=Folder) else (set type=File)
No. Scratch that. It doesn't work too.

Use this instead.

@echo off
setlocal
set attr=
for %%A in ("%file%") do set attr=%%~aA
if "%attr%" neq "" (
echo Not found.
goto :eof
)
if "%attr:d=%" == "%attr%" (
echo It's a file.
) else (
echo It's a folder.
)
Tom Del Rosso
2024-07-30 23:46:07 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
if exist "%file%\" (set type=Folder) else (set type=File)
That works for a local file, but if %file% points to a network share
then it always says Folder.
Are you seeing that too?
This is for win7 (not this newsreader machine).
@echo off
setlocal
set attr=
for %%A in ("%file%") do set attr=%%~aA
if "%attr%" neq "" (
echo Not found.
goto :eof
)
if "%attr:d=%" == "%attr%" (
echo It's a file.
) else (
echo It's a folder.
)
That's great. Thanks.
But isn't the logic inverted in the first IF?
If it doesn't exist then %attr% would be an empty string, I think.
--
Defund the Thought Police
JJ
2024-08-01 16:06:44 UTC
Permalink
Post by Tom Del Rosso
Post by JJ
Post by Tom Del Rosso
if exist "%file%\" (set type=Folder) else (set type=File)
That works for a local file, but if %file% points to a network share
then it always says Folder.
Are you seeing that too?
This is for win7 (not this newsreader machine).
@echo off
setlocal
set attr=
for %%A in ("%file%") do set attr=%%~aA
if "%attr%" neq "" (
echo Not found.
goto :eof
)
if "%attr:d=%" == "%attr%" (
echo It's a file.
) else (
echo It's a folder.
)
That's great. Thanks.
But isn't the logic inverted in the first IF?
If it doesn't exist then %attr% would be an empty string, I think.
You're right. It should be the opposite. I need more coffee...
Tom Del Rosso
2024-08-04 00:56:25 UTC
Permalink
Post by JJ
Post by Tom Del Rosso
Post by JJ
Post by Tom Del Rosso
if exist "%file%\" (set type=Folder) else (set type=File)
That works for a local file, but if %file% points to a network
share then it always says Folder.
Are you seeing that too?
This is for win7 (not this newsreader machine).
@echo off
setlocal
set attr=
for %%A in ("%file%") do set attr=%%~aA
if "%attr%" neq "" (
echo Not found.
goto :eof
)
if "%attr:d=%" == "%attr%" (
echo It's a file.
) else (
echo It's a folder.
)
That's great. Thanks.
But isn't the logic inverted in the first IF?
If it doesn't exist then %attr% would be an empty string, I think.
You're right. It should be the opposite. I need more coffee...
Sometimes the test makes it crash if the "attr" variable is "" because
the file doesn't exist (or the file has a unicode name).

So I added a character.
set attr=_%%~aA
if "%attr%" neq "_" (
--
Defund the Thought Police
Loading...