- windows — How to verify if a file exists in a batch file? — Stack Overflow
- How to verify if a file exists in a batch file?
- If exist folder rmdir folder && mkdir folder will not execute any command after that &&
- CMD — If exists
- 2 Answers 2
- Create folder with batch but only if it doesn’t already exist
- 9 Answers 9
- How to test if a path is a file or directory in Windows batch file?
- 6 Answers 6
- Create folder in batch script and ignore if it exists
- 2 Answers 2
windows — How to verify if a file exists in a batch file? — Stack Overflow
How to verify if a file exists in a batch file?
Here is a good example on how to do a command if a file does or does not exist:
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
Use the XCOPY command:
I will explain what the /c /d /h /e /i /y means:
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
If it was in the Bash environment it was easy for me, but I do not
know how to test if a file or folder exists and if it is a file or
folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
If exist folder rmdir folder && mkdir folder will not execute any command after that &&
Since you are using rmdir without the /S switch I assume the folder of interest is empty, so why removing and recreating it? You could simply create it and suppress the error message in case it already exists, like:
This variant maintains all folder attributes and the owner, of course.
If you insist on removing and recreating the folder, use this:
This variant destroys all folder attributes and the owner.
In case the folder is not guaranteed to be empty, you need the /S switch for rmdir ; also the /Q is useful to avoid being prompted to really remove the folder, like:
CMD — If exists
The below commands (with debug lines added — indented) should only redirect the echo’s output to a file, should it already exist, according to my understanding.
However, it would seem that if exist %test0% always fills the file (creating it if non-existant) with the echo’s output.
Does anyone know what is wrong?
And the file gets created(!)
EDIT: This above was a simplified example, and unfortunately MSalters answer doesn’t help me solve the full command (I had hoped it would). The full one line if statement is:
if exist %test0% (echo.&echo.&echo ————————————————-&echo.&echo.) else (set /p .= >%test0%&set errorlevel=0||set errorlevel=1
How would I have whichever condition of the if matched output to the file (Hopefully with only one reference to the file, i.e., not one in each if conditional), and have the errorlevel set based on the exist ance of the file?
Could anyone help with the actual full command issue?
2 Answers 2
You should never set ERRORLEVEL directly. That name is reserved for reporting on the results of the prior command. When you set the value directly, you override the intended functionality and it ceases to expand to the actual ERRORLEVEL, it expands to the value you set instead. That can break all kinds of code.
You can force the ERRORLEVEL to a value by running a command with known result, redirecting output to nul if necessary: ver >nul sets ERRORLEVEL to 0, set /p .= sets ERRORLEVEL to 1.
You can force the ERRORLEVEL to any particular value of your choosing by using cmd /c exit /b N , where N is an integral value.
You also have faulty logic. Your IF command succeeds (has no error) regardless whether the condition evaluates to TRUE or FALSE. If you want to set the ERRORLEVEL, then you need to do it within your parenthesized blocks.
There is nothing wrong with putting everything on one line, but I find the code easier to read when using multiple lines for complex statements like yours. I believe the following is what you are looking for.
Edit in response to comments
Create folder with batch but only if it doesn’t already exist
Can anybody tell me how to do the following in in a Windows batch script? ( *.bat ):
- Create a folder only if it doesn’t already exist
In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn’t already exist. I don’t want to overwrite the contents of the folder if it already exists and the batch is executed.
9 Answers 9
You just use this: if not exist «C:\VTS\» mkdir C:\VTS it wll create a directory only if the folder does not exist.
Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.
Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.
Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn’t care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn’t exist, and it doesn’t overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk’s answer.
How to test if a path is a file or directory in Windows batch file?
I searched here, found someone using this
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.
6 Answers 6
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 %
The original code does not need the FOR loop, it could simply use %
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
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)
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
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
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.)
Create folder in batch script and ignore if it exists
How do I create folder(and any subfolders) in batch script? What is important, if folder(or any subfolders) already exists, it should not return error.
For example, something like this:
- mkdir mydir — success(directory is now created)
- mkdir mydir\subdir — success(now mydir contains subdir )
- mkdir mydir — success(folder already exists, should not throw an error)
- mkdir mydir\subdir — success(folders already exists, should not throw an error)
What I actually need is just to ensure that folders structure exists.
2 Answers 2
A standard method to create a directory structure is:
By default command extensions are enabled and delayed expansion is disabled. The batch code above explicitly sets up this environment.
The command MD creates the complete directory structure to specified directory with enabled command extensions.
MD outputs an error if the directory already exists. This might be useful to inform a user entering the command manually about a possible mistake in entered directory path as it could be that the user wanted to create a new directory and has entered just by mistake the name of an already existing directory.
But for scripted usage of command MD it is often a problem that this command outputs an error message if the directory to create already exists. It would be really helpful if command MD would have an option to not output an error message in case of directory to create already existing and exiting with return code 0 in this case. But there is no such option.
The solution above creates the directory and suppresses a perhaps output error message with redirecting it from handle STDERR to device NUL.
But the creation of the directory could fail because of invalid character in directory path, drive not available (on using full path), there is anywhere in path a file with name of a specified directory, NTFS permissions don’t permit creation of the directory, etc.
So it is advisable to verify if the directory really exists which is done with:
It is important that the directory path ends now with \* or at least with a backslash. Otherwise it could be possible for the example that there is a file with name subdir 2 in directory mydir\subdir 1 which on usage of the condition if not exist «%Directory%» would evaluate to false although there is no directory subdir 2 .
It is of course also possible to do the directory check first and create the directory if not already existing.
The user can see now the error message output by command MD if the directory structure could not be created explaining briefly the reason.
This batch code could be written more compact using operator || :
For details about the operators || and & read the answer on Single line with multiple commands using Windows batch file.
The command ENDLOCAL is not used before goto :EOF because this command requires also enabled command extensions. Windows command interpreter executes this command implicit on leaving execution of the batch file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
Read also the Microsoft article about Using Command Redirection Operators.