Batch test if file or directory




















Connect and share knowledge within a single location that is structured and easy to search. I would like to be able to check if a certain folder FolderA exists and if so, for a message to be displayed and then the batch file to be exited.

If FolderB does not exist, a message should be displayed and the folder should be created, and if FolderB does exist, a message should be displayed saying so.

Does anybody have any idea on the code I could simply use on notepad to create a batch file to allow me to do this? How to test if a file is a directory in a batch script?

Please read the original answer because includes details about handling white spaces in the folder name.

As foxidrive said, this might not be reliable on NT class windows. It works for me, but I know it has some limitations which you can find in the referenced question. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Checking if a folder exists using a. Asked 8 years ago. Active 11 months ago. Viewed k times. Improve this question. Jost 5, 8 8 gold badges 39 39 silver badges 67 67 bronze badges. This time I use an external 'find' command. Directory symbolic links or junctions. Broken directory symbolic links or junctions. Doesn't try to resolve links. Directories which you have no read permission on e.

Explorer09 Explorer09 5 5 silver badges 9 9 bronze badges. The NUL technique seems to only work on 8. The problem appears to be not with long file names but directories with spaces in their names. Or rather, it's the quotes around the variable name that stop the NUL trick from working.

I've added some code to my answer which converts file names to 8. Kimae Kimae 31 1 1 bronze badge. Probably not the most efficient but easier to read than the other solutions in my opinion. Samuel Samuel 6, 8 8 gold badges 39 39 silver badges 39 39 bronze badges. I liked your answer as the simplest. For some reason I don't understand it suddenly stopped working so I posted an answer here. Alternative take: if you can cd into it This verifies the path exists, but does not distinguish between a file or a folder.

A very simple way is to check if the child exists. If a child does not have any child, the exist command will return false. Eva Dias 1, 9 9 gold badges 33 33 silver badges 66 66 bronze badges. Cedric Cedric 29 1 1 bronze badge. Frank Nocke Frank Nocke 8, 4 4 gold badges 62 62 silver badges 97 97 bronze badges. Aaron Thoma 3, 32 32 silver badges 31 31 bronze badges.

RomanM RomanM 5, 8 8 gold badges 32 32 silver badges 41 41 bronze badges. Usage: script. Jamal M. Jamal 11 2 2 bronze badges. See also the solution from batchman61 which avoids this issue by checking attributes. It works even if you don't have read permissions It works with Directory Links Junctions. It works with files whose path contains a Directory Link. Amr Ali Amr Ali 1, 14 14 silver badges 10 10 bronze badges.

TechGuy TechGuy 9 1 1 bronze badge. If your objective is to only process directories then this will be useful. Ste Ste 1, 1 1 gold badge 10 10 silver badges 21 21 bronze badges. This is an answer to another question. It doesn't fit to THIS question about detection if a given filename is a directory.

Seeing as the question has been answered to death. This just allows the user to process directories and skip the checking part. If yes, go to Dir coding. If not, go to File coding. If Yes, go to File coding. Lucien Baron Lucien Baron 6 6 bronze badges. This won't work on folders containing a dot. Batch files: If directory exists, do something Ask Question. Asked 6 years, 8 months ago. Active 6 years ago. Viewed 24k times. Improve this question.

Julian Moreno. Julian Moreno Julian Moreno 1, 3 3 gold badges 15 15 silver badges 29 29 bronze badges. Are you trying to print to the console, or to paper? That's just to debug the code and to be sure that the system is executing that. Add a comment. Active Oldest Votes.

But I have seen that test fail in batchfiles, for unknown reasons. Improve this answer. So, if I need to execute several commands



0コメント

  • 1000 / 1000