I searched here, found someone using this
set is_dir=0
for %%i in ("%~1") do if exist "%%~si"\nul set is_dir=1
but didn’t work, when %1==c:\this is a file with spaces.csproj, the test still success, which means it will still be treated as a folder!!!
anyone knows the answer, i guess this is a very common problem and Windows has existed for many many years, it should have a very simple solution….
I know the
if exist path\nultest 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 thatif exist path\nulworks 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
%~s1Here 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
%~s1bug. I’m not sure how it performs on MS-DOS.EDIT 2015-12-08: There are a number of Windows situations where this fails
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)
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.
Edit 2015-12-08: This can fail on network drives because the folder test can falsely report a file as a folder
UPDATE 2015-12-08
Finally – a test that truly should work on any Windows version from XP onward, including with network drives and UNC paths
Note – This technique is intended to be used for a path without any wildcards (a single specific file or folder). If the provided path includes one or more wildcards, then it provides the result for the first file or folder that the file system encounters. Identical directory structures may give different sort order results depending on the underlying file system (FAT32, NTFS, etc.)