Цитата mwz:
Не с замечанием, а с вопросом: а не лучше "if not exist "c:\New Folder\nul"? »
|
Вот мои результаты тестирования в Windows 8.1:
Код:

C:\>if exist "C:\Program Files\nul" (echo It's a folder!) else (echo It's a file
or I have none of that!)
It's a file or I have none of that!
C:\>if exist C:\PROGRA~1\nul (echo It's a folder!) else (echo It's a file or I h
ave none of that!)
It's a folder!
C:\>if exist "C:\Program Files\." (echo It's a folder!) else (echo It's a file o
r I have none of that!)
It's a folder!
C:\>if exist C:\PROGRA~1\. (echo It's a folder!) else (echo It's a file or I hav
e none of that!)
It's a folder!
.
А вот ответ известного (по крайней мере, мне) эксперта по командной строке и пакетным файлам
Dave Benham (
http://stackoverflow.com/questions/8...669636#8669636 ):
Цитата:
I know the
Код: 
if exist path\nul
test for a folder used to work on MS-DOS. I don't know if it was broken with the introduction of long file names.
I knew that
Код: 
if exist "long path\nul"
does not work on Windows batch. I did not realize until today that
Код: 
if exist path\nul
works on Vista and beyond as long as path is in the short 8.3 form.
The original code appears to work on Vista. It seems like it should work on XP as well, but I believe the following XP bug is getting in the way: Batch parameter
Код: 
%~s1
gives incorrect 8.3 short name.
The original code does not need the FOR loop, it could simply use
Код: 
%~s1
Here is a variation that fully classifies a path as INVALID, FILE or FOLDER. It works on Vista, but does NOT work on XP because of the %~s1 bug. I'm not sure how it performs on MS-DOS.
Код: 
@echo off
if not exist "%~1" ( set "type=INVALID" ) else if exist %~s1\nul ( set "type=FOLDER" ) else ( set "type=FILE" )
@echo "%~1" = %type%
I believe this variation will work with nearly all versions of Microsoft batch, including MS-DOS and XP. (it obviously won't work on early versions of DOS that don't support PUSHD)
Код: 
@echo off
if exist "%~1" (2>nul pushd "%~1" && (popd&set "type=FOLDER") || set "type=FILE" ) else set "type=INVALID"
echo "%~1" = %type%
UPDATE 2014-12-26
I'm pretty sure the following will work on all versions of Windows from XP onward, but I have only tested on Win 7.
Код: 
@echo off
if exist %1\ (
echo %1 is a folder
) else if exist %1 (
echo %1 is a file
) else (
echo %1 does not exist
)
|
.
А вот парадокс от меня (первый раз вижу, чтобы кавычки в таком аспекте влияли на результат):
Код:

C:\>if exist C:\PROGRA~1\nul (echo It's a folder!) else (echo It's a file or I h
ave none of that!)
It's a folder!
C:\>if exist "C:\PROGRA~1\nul" (echo It's a folder!) else (echo It's a file or I
have none of that!)
It's a file or I have none of that!
.
Цитата mwz:
Цитата Iska:
Такой файл или папка теоретически может существовать в искомом каталоге. »
|
Это зарезервированное слово. »
|
При желании их можно создать:
Код:

md \\.\C:\Folder\nul>\\.\D:\Folder\nul
.
Эта команда создаст одновременно папку "C:\Folder\nul" и файл "D:\Folder\nul" в существующей папке.