0

I already check if the folder exists, but recently ran into the problem where vs2013 tried to export headers to an include directory that didn't exist, so it made an include.file and either overwrote or appended the headers to that file.

if not exist "%MY_DIR%\%2\include" ( 
echo making include directory for %2
mkdir "%DEV_DIR%\%2\include"
)

That check failed because it found that "%MY_DIR%\%2\include" existed, and never created the directory. So anything that wanted to use the exported header just cried a little.

Usually the script gets run before build but for whatever reason it didn't this time, so I'd like to change the condition on which it makes a directory but googling has been thus far unsuccessful.

codeMetis
  • 420
  • 4
  • 16
  • Just make the directory. There's no need to test. If it already exists the command will fail. –  Nov 02 '16 at 02:22
  • The check is a function that gets called on like 20 directories, new ones get checked-out on a somewhat frequent basis, when they get checked out they don't come with an include folder, so a correct cmake and a correct build WILL produce the error i spoke of earlier. This check needs to happen somewhere prior to build. But yes, I could add include folders every time I checkout a new repo but many tasks that we regularly automate could also be done by hand. That's not really the point. – codeMetis Nov 02 '16 at 03:02
  • You don't need to check, just create the folder. –  Nov 02 '16 at 03:35
  • Parden me. You meant just keep the mkdir part. Tried it and it still fails when there's a foldername.file Is windows able to distinguish between a folder and a .file of the same name? – codeMetis Nov 02 '16 at 04:27
  • So you mean your filename is just `include` without any ending? Unfortunately you cannot place a file without ending with the same name as an existing folder in the same directory. – geisterfurz007 Nov 02 '16 at 07:02
  • ya I know. I was wrong to say it was a .file though, it's actually extension-less. In response to my previous commented question: Batch can distinguish between a folder and an extensionless file of the same name. running dir displays for folders, and filesizes for files. It correctly identified my file as a file. I suppose it was a silly tangent in retrospect. – codeMetis Nov 02 '16 at 07:14
  • 1
    I think a trailing backslash should do it: `if not exist "%MY_DIR%\%2\include\"` – aschipfl Nov 02 '16 at 07:16
  • 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:38
  • @aschipfl it didn't. – codeMetis Nov 02 '16 at 08:07
  • @aschipfl It still found the extensionless include files. I tried counting the files in the include folders to snuff out extenionless files - all the real folders returned correctly but the extensionless include files returned 1. – codeMetis Nov 02 '16 at 08:14
  • 1
    Possible duplicate (**reach discussion there**) of [How do I test if a file is a directory in a Batch script?](http://stackoverflow.com/questions/138981/how-do-i-test-if-a-file-is-a-directory-in-a-batch-script) – JosefZ Nov 02 '16 at 08:46
  • 1
    The trailing backslash does definitely not match extensionless files! However, `mkdir` is of course going to fail in case there is a file with the same name as the directory to be created. So is that the problem you encounter? – aschipfl Nov 02 '16 at 09:09
  • In programming we do, and if we care we test if successful. Testing then doing chews battery life and takes a long time as two disk accesses are needed. Just create the directory. It will work or not but you'll have your directory at the end of the day. You cannot have two things of the same name. See my answer here http://stackoverflow.com/questions/25754865/keep-getting-the-syntax-of-the-command-is-incorrect to tell if something is a folder. YOU NEED ONE LINE. –  Nov 02 '16 at 10:47
  • aschipfl understands the problem. @Noodles, you do not understand the problem. just doing, will not leave me with a directory at the end of the day, if there is an extension-less file of the same name, mkdir will think that that extension-less file IS THE FOLDER, when in fact A FOLDER IS NOT A FILE. everything will cmake, and build. but any other project that tries to include those headers will fail SILENTLY. for potentially many projects. – codeMetis Nov 03 '16 at 04:13
  • You cannot have a folder and file of the same name. –  Nov 03 '16 at 04:24
  • when did I say I ever had both? mkdir *thinks* the extension-less file is a folder. there is no folder, there is an extension-less file. mkdir thinks the file is the folder, so it thinks it doesn't have to do anything. – codeMetis Nov 03 '16 at 05:00

2 Answers2

0

Check for strictly folders that don't exist by adding \NUL to the end of your address.

The addition of the NUL allows you to find only directories, and not the these scary extensionless files that look like folders. Delete won't be an issue if there's nothing to delete, but if there is a scary file it won't be scary for much longer, then can make an actual directory yay!

if not exist %MY_DIR%\%2\include\NUL ( 
 del %MY_DIR%\%2\include
 mkdir "%MY_DIR%\%2\include" 
)
codeMetis
  • 420
  • 4
  • 16
  • You are wrong as the OP references file in double quotes and then adding `\NUL` trick could fail. For instance: `dir /B /ad "%userprofile%\doc*"` returns `Documents`. None less, `if exist "%userprofile%\Documents\nul" (echo folder) else (echo file)` surprisingly returns `file`. – JosefZ Nov 02 '16 at 08:41
  • I don't know if directories are treated as strings or not, or much else regarding batch variables. So I can't explain why, but I can verify that it works and solved the problem. I ran the batch without double quotes and it fixed the problem. I tested it with empty include folders, not empty include folders, with no include folders and no extension-less include file, and also with the extension-less include file thus no folder. Every test passed. So why is it wrong? Also I think adding nul inside the string would treat the nul as part of the directory. Shouldn't you add nul after the string? – codeMetis Nov 03 '16 at 04:08
0
C:\Windows\system32>md "%userprofile%\desktop\test" && Echo Folder Created || Echo Folder Existed
Folder Created

C:\Windows\system32>md "%userprofile%\desktop\test" && Echo Folder Created || Echo Folder Existed
A subdirectory or file C:\Users\David Candy\desktop\test already exists.
Folder Existed

Shows how to ensue a folder exists.

  • in my situation where i have an extension-less file "test" where i wanted a folder "test", it would echo Folder exists despite not existing. Thats how i got here. That was the whole problem in the first place. – codeMetis Nov 03 '16 at 03:58
  • `md "%userprofile%\desktop\test` returns true if there is an extension-less file called "test" AND also will return true in the separate situation where there is a folder called test. I'm aware you can't have both. thats why i look for extension-less files first, delete them, and then make a directory. – codeMetis Nov 03 '16 at 04:58
  • Read the error message *A subdirectory or file C:\Users\David Candy\desktop\test already exists.*. So what is your question, how to make windows not be windows? –  Nov 03 '16 at 05:40
  • if it prints *A subdirectory or file C:\Users\David Candy\desktop\test already exists*, what use is that? There are different actions if its a file or a folder that exists. You understand the solution wasn't about notification of a potential error, but writing a batch file to fix the error. the only thing i could possibly think you could be suggesting is this: `mkdir "%My_DIR%\%2\include" && Echo Folder Created %2 || ( del "%My_DIR%\%2\include" && mkdir "%DEV_DIR%\%2\include" && Echo extension-less file deleted )` – codeMetis Nov 03 '16 at 07:12
  • The issues with this is that it a) prompts for an "Are you sure (y/n)" response every single time (many many times) . I'm sure you could avoid this somehow by saying always y b) But that would mean unnecessarily deleting every file in every include folder that existed. so after running this script Id have to rebuild every single header. but how on earth is this a better solution than: `if not exist %MY_DIR%\%2\include\NUL ( del %MY_DIR%\%2\include mkdir "%MY_DIR%\%2\include" )`. This only deletes extension-less files or nothing at all because there was neither include file or folder. – codeMetis Nov 03 '16 at 07:12
  • I don't understand where the Y/N is coming from. You can use the statements after the `&&` and `||` to do whatever you want. –  Nov 04 '16 at 05:28