1

I am trying to use the answer posted in another Stackoverflow post

According to that post, I should be able to use the following batch script to test if a link is a directory

if exist %1\* echo Directory

However, I am not sure how I am supposed to use this.

I have tried replacing

if exist %1\* echo Directory

with

SET "t=%USERPROFILE%\Desktop\testDir"
if exist %t\* echo Directory

But this doesn't echo out Directory like it should (testDir directory exists on my desktop)

Does the %1 in this conext represent something?

Community
  • 1
  • 1
  • 1
    It has been ages since I last wrote a DOS/Windows shell script (it's MS shell thing, right?), so I can't remember many things. %1 is the first parameter of your script, e.g. if you run `myscript.bat testDir`, then %1 will be "testDir”. I suspect your `SET` line; for debugging purposes, I would include an `echo %t` line there to check if the variable's value is OK. – GergelyPolonkai Apr 29 '15 at 21:52
  • Possible duplicate of [Checking if a folder exists using a .bat file](http://stackoverflow.com/questions/21033801/checking-if-a-folder-exists-using-a-bat-file) – aschipfl Nov 02 '16 at 07:40

2 Answers2

1

I generally use:

if exist %1\. echo Directory

EDIT: Assuming %1 is a directory, the . is a 'file' that is always there, so it's presence ensures it is a directory and not a file.

kenny
  • 21,522
  • 8
  • 49
  • 87
  • why dont you explain why you use **%1\.** instead of **%1\*** and how to use it, your answer doesnt let much off... – donttellthem Apr 29 '15 at 22:18
  • I can't remember how this behave in MS-DOS, but in Windows' `cmd.exe`, this also returns true for files… – aschipfl Nov 08 '22 at 21:47
  • @donttellthem as I remember aschipfl is correct, %1 will show exists on files and the "\." ensures that only directories match. The example shows pretty well how it works, where it echoes "directory" only on directories. – kenny Nov 09 '22 at 11:06
0

%1 is the first command line parameter.

C:\Users\User>if exist c:\windows echo windows exist
windows exist

C:\Users\User>if exist c:\windows1 echo windows exist

C:\Users\User>

And a list of some punctuation

&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.

And if you want to know if a directory or file.

@Echo off
pushd %1 >nul 2>&1  
If errorlevel 0 if not errorlevel 1 Echo %~nx1 is a folder
If errorlevel 1 Echo %~nx1 is not a folder
Popd

If /i "%cmdcmdline:~0,6%"=="cmd /c" pause
Trigger
  • 681
  • 3
  • 4