Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 890 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Batch files: If directory exists, do something

#1
I'm trying to do the following:

IF EXISTS ("C:\Users\user\Desktop\folder1\") {

MOVE "C:\Users\user\Desktop\folder2\" "C:\Users\user\Desktop\folder1\"
RENAME "C:\Users\user\Desktop\folder1\folder2\" "folder3"

} else {

MKDIR "C:\Users\user\Desktop\folder1\"
MOVE "C:\Users\user\Desktop\folder2\" "C:\Users\user\Desktop\folder1\"
RENAME "C:\Users\user\Desktop\folder1\folder2\" "folder3"
}

With the following code:



@ECHO ON

IF EXIST "C:\Users\user\Desktop\folder1\" (GOTO MOVER)

PRINT "It doesn't exists - This is just for debugging"
PAUSE

:MOVER
ECHO "MOVER"
PAUSE
EXIT
:END


But the system does not print the test words.
Reply

#2
`IF EXIST` checks only if a file exists and cannot check folders. Usually, you test like this

IF NOT EXIST "myfolder\NUL" mkdir "myfolder"

The pseudo device `NUL` acts like a file and does in fact exist in every folder. Note the spelling.

But I have seen that test fail in batchfiles, for unknown reasons. So I suggest this instead:

CD myfolder 2>NUL && CD .. || MD myfolder

`CD myfolder` tries a legal operation with the folder and the conditional execution of `MD`/`MKDIR` creates the folder only if that operation fails. `CD ..` reverts the action if the folder should exist. `2>NUL` suppresses the error message if the folder does not exist.

**edit:**
Apparantly there is a simpler method for testing: append a backslash (\\) to the foldername to make it syntactically a folder, like this:

if not exist myfolder\ md myfolder || goto :EOF

This will create the folder "myfolder" if it does not yet exist. Additionally, in case there is a **file** named "myfolder" the `MD` will fail and the batch file will be terminated after the error message is displayed. Also, ERRORLEVEL will be set. I like this more as the error output doesn't have to be redirected.

**edit:**
If you want to execute several commands, run them in a subshell, that is, enclose them in parantheses like this:

if not exist myfolder\ (
md myfolder
dir myfolder
REM ...or run any other commands
) || goto :EOF

Reply

#3
IF EXIST can check for folders
I use it this way:

IF EXIST %local_path%\opensource\eclipse rmdir %local_path%\opensource\eclipse /s /q >nul
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through