Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:09:26+00:00 2026-05-27T22:09:26+00:00

I searched here, found someone using this set is_dir=0 for %%i in (%~1) do

  • 0

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:09:27+00:00Added an answer on May 27, 2026 at 10:09 pm

    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.
    EDIT 2015-12-08: There are a number of Windows situations where this fails

    @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.
    Edit 2015-12-08: This can fail on network drives because the folder test can falsely report a file as a folder

    @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
    )
    

    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

    for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" (
      echo %1 is a folder
    ) else if "%%A" neq "-" (
      echo %1 is a file
    ) else (
      echo %1 does not exist
    )
    

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched here, but haven't found any question related to this. I got
I searched for this and found Maudite's question about text editors but they were
Ok, I've searched around here but haven't found anything pointing to a solid answer.
I'm new to jQuery. I've searched and found some results that might work, but
I've searched all over and have found possible solutions but they don't quite work
I searched through here as best I could and though I found some relevant
I've searched the web and I've searched the questions here on stackoverflow, but I
first post here, I come in peace :) I've searched but can't quite find
I've searched a bit and didn't see any similar question, so here goes. How
I have searched here and found the assertion that no single application may occupy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.