How does the "if exist" command work in Windows batch scripts? - windows

In Windows batch scripts, usually we can find if exist xxx or if not exist xxx.
Does this search all files in the computer, or any specific folder or path, for the xxx specified?

If you have not specified a folder, it will look in current folder.
But you can work use wildcards.
Example:
if exist *.png echo There are images here
will output the text if in the current folder there are any files with the extension .png
or you can specify a full path, for example
if exist d:\temp\*.png echo There are images there

if you want it to check if something exist. and then make it execute something afterwards. then this is how you do it:
if exist "D:randomstuff\random\ranodom\allala.jpg" goto anotherLabel
if not exist "D:randomstuff\random\ranodom\allala.jpg" goto addwrite
:anotherlabel
:addwrite
MKDIR D:randomstuff\random\ranodom\
echo this image doesn't exist> D:randomstuff\random\ranodom\allala.txt
or you can do this:
if exist randomfile.txt (
for /f %%A in (randomfile.txt) do set text=%%A
) else (
goto notexist.
basically what you do. you insert the path to whatever file you want it to check if exist (with file name)
and then you just set it to do an actionl wether its to create, add in, overwrite, copy, change label. etc.

Related

How to make a folder in the location of a batch file

I am creating a batch script for which I have a script that is giving me a frustrating error. The location the batch file is being excecuted from is
C:\Users####\Desktop\Sp2\Sp2.bat. I want the file to prompt the user if it wants to make a batch file. I got that down. I used the code
echo Would you like to create a directory to output the files to?
set /p mkdir=[Y/N]
if %mkdir%==Y (
goto :mkdir
) ELSE (
goto :numset
)
This part works fine. Now here's where the problem arises:
:mkdir
echo Enter a name for your folder.
set /p foldername=
MD %~d0\%foldername%
goto :numset
I this keeps giving me the error "The syntax of this command is incorrect."Can anyone give me a solution to this problem?
%~d0 gives you the drive only. To get drive and path, use %~dp0:
MD %~dp0%foldername%
The complete listing of %~?0 is well hidden. See: for /?
%~d0 expands to a drive of the batchfile. Users don't have permission to create folders in the root of a drive. So it will never work.
Plus the path seperator is \ not the switch character /.

I have a set of recordings with me from which i have to extract only a select few clips

I have about nearly 800 recording clippings from which i have to extract only a few select clips, the clips which i have to extract and store in another folder is a list that i should prepare on excel or on a notepad, whichever is easy to program with
To say my example would look like this
This is my folderlocated in a path called C:\Desktop\Recordings
20140404-204604_1622719993-all
20140404-204638_1622737834-all
20140404-204925_1634300477-all
20140404-205903_1654712140-all
20140404-210135_1664564521-all
20140404-210924_1698260169-all
etc. etc.
from where i have a notepad file called list.txt which will contain the data somewhat like this.
list.txt
1524824025
1524846905
1530242587
1555663573
1555663992
1555811052
1555820729
1555820601
I have to extract the clips in the above folder matching to the set of filenames i have in the notepad/excel above and paste/extract them on a different directory.
Is it possible ?
Place file.txt from notepad in C:\Desktop\Recordings and launch this.
It will show you the copy commands on the screen - remove echo and run it again to actually copy the files.
Replace copy with move if you want to move the files to the target folder, which must already exist.
#echo off
cd /d "C:\Desktop\Recordings" && (
for /f "usebackq delims=" %%a in ("file.txt") do (
echo copy "*%%a*" "c:\new\folder"
)
)
echo if the screen is empty then double check the path in the cd command.
pause

using space in path for copying files using batch script

I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"

batch script to set a variable with the current path location

How can I set a variable with the current location? For instance, if I get in c:\test and want to set the variable to test and if I get inside c:\test\test2 the variable will be set to test2.
I'm thinking about using a for to get inside a lot of folders and check if some file exists, if the correct file exist I want to set the current folder to a variable so I can copy the path and copy the folder.
The main problem deals with copying the rest of the files is the same folder as the .inf file.
The current directory is in the "shadow" variable cd.
You could try
set "var=%cd%"
%~dp0
This expands into the drive & path of the currently running batch file. I usually surround my batch files with something like:
#echo off
pushd %~dp0
...
popd
Edit: It seems I didn't understand the OP. My example gets the location of the currently running script, not the "Current Directory". +1 to jeb.
I think there is a little confussion here. %CD% always have the current directory, so you don't need to add anything to have it. However, by rereading your original question, I think that you need the LAST PART of the current directory, that is, the name of the current location excluding all previous locations. If so, then you may use this:
set i=0
:nextdir
set /a i+=1
for /f "tokens=%i% delims=\" %%a in ("%CD%") do if not "%%a" == "" set lastdir=%%a& goto nextdir
echo Current location: %lastdir%

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.
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.
if exist C:\VTS\NUL echo "Folder already exists"
if not exist C:\VTS\NUL echo "Folder does not exist"
See also https://support.microsoft.com/en-us/kb/65994
(Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )
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.
mkdir C:\VTS 2> NUL
create a folder called VTS and output A subdirectory or file TEST already exists to NUL.
or
(C:&(mkdir "C:\VTS" 2> NUL))&
change the drive letter to C:, mkdir, output error to NUL and run the next command.
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.
#echo off
#break off
#title Create folder with batch but only if it doesn't already exist - D3F4ULT
#color 0a
#cls
setlocal EnableDelayedExpansion
if not exist "C:\VTS\" (
mkdir "C:\VTS\"
if "!errorlevel!" EQU "0" (
echo Folder created successfully
) else (
echo Error while creating folder
)
) else (
echo Folder already exists
)
pause
exit
You can use:
if not exist "C:\VTS\" mkdir "C:\VTS"
You can also expand the code to replace any missing expected files.
if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
This should work for you:
IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder
However, there is another method, but it may not be 100% useful:
md \path\to\your\folder >NUL 2>NUL
This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.
You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.
First, I check to see if the folder doesn't already exist by entering this code:
if not exist "FOLDERPATH" (
mkdir "FOLDERPATH"
)
So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:
if exist "FOLDERPATH" (
rmdir /s /q "FOLDERPATH"
mkdir "FOLDERPATH"
)
Now if I run the code, It will re-create the folder if it already exists. This is the example code:
#echo off
cls
if not exist "C:\ExamplePath\" (
echo Creating Folder...
mkdir "C:\ExamplePath\"
pause
)
if exist "C:\ExamplePath\" (
echo Re-Creating Folder...
rmdir /s /q "C:\ExamplePath"
pause
)
Now the if exist part is Optional. If the folder already exists, you can jump to an label instead like this:
if exist "FOLDERPATH" (
goto :ExampleLabel
:ExampleLabel
echo Hi.
pause
)
Hopefully, this could help with your problem.
Personally, I would do this:
if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)
Let's break that down:
if not exist "C:\YourFolderPathHere\": this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.
mkdir C:\YourFolderPathHere\: Creates the directory if the above statement is true.
echo Folder already exists!: prints to console that it already exists.
Here's the same code, but with comments:
::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)
One-line version:
if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\
Haven't tested it though, so don't quote me on it.
i created this for my script I use in my work for eyebeam.
:CREATES A CHECK VARIABLE
set lookup=0
:CHECKS IF THE FOLDER ALREADY EXIST"
IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)
:IF CHECK is still 0 which means does not exist. It creates the folder
IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"

Resources