Skip to content

Instantly share code, notes, and snippets.

@KiT106
Created March 28, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KiT106/d61352f28cab28b98cb2fcf710b6ec8b to your computer and use it in GitHub Desktop.
Save KiT106/d61352f28cab28b98cb2fcf710b6ec8b to your computer and use it in GitHub Desktop.
Check directory in Windows batch
@echo off
echo %~1
::::::::::::::::::: has argument ? :::::::::::::::::::
IF ["%~1"]==[""] (
echo Missing argument!!!
echo Syntax:
echo setup ^<main_repo^>
exit /B 1;
)
::::::::::::::::::: argument exist ? :::::::::::::::::::
IF NOT EXIST %~s1 (
echo "%~1" not exist
exit /B 1
)
::::::::::::::::::: is directory ? :::::::::::::::::::
IF NOT EXIST %~s1\NUL (
echo "%~1" is a file. Require directory!
exit /B 1
)
@KiT106
Copy link
Author

KiT106 commented Mar 28, 2017

  • in %~1 - the ~ removes any wrapping " or '.
  • in %~s1 - the s makes the path be DOS 8.3 naming, which is a nice trick to avoid spaces in file-name while checking stuff (and this way no need to wrap the resource with more "s.
  • the ["%~1"]==[""] "can not be sure" if the argument is a file/directory or just a generic string yet, so instead the expression uses brackets and the original unmodified %1 (just without the " wrapping, if any..)
    if there were no arguments of if we've used shift and the arg-list pointer has passed the last one, the expression will be evaluated to [""]==[""].
  • this is as much specific you can be without using more tricks (it would work even in windows-95's batch-scripts...)

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment