.BAT - IF command do not work - batch-file

Ok, so probably I am doing something ultra wrong.
I want to make this:
in C:\programs\app\install.exe
is installation of a program.
It should install to:
c:\program files\app
And my question is how to make IF command inside .bat file to:
if c:\program files\app\appStart.exe exist start c:\program files\app\appStart.exe
if c:\program files\app\appStart.exe not exist start C:\programs\app\install.exe
to make it more simple I test it on this files in this folder (never tried with folders and files from above)
c:\folder\1.txt
c:\folder\2.txt
c:\folder\A.txt
c:\folder\B.txt
And I want to make:
if c:\folder\1.txt exist start c:\folder\A.txt
if c:\folder\1.txt not exist start c:\folder\B.txt
I found tons of sites on google with answers but nothing seems to work to me they won't even open.
Was trying this:
IF EXIST "C:\Nowy folder" (
REM 1.txt
IF EXIST "1.txt" (Start "C:\folder\1.txt") ELSE (Start "C:\folder\A.txt")
) ELSE (
REM 2.txt
IF EXIST "2.txt" (Start "C:\folder\2.txt") ELSE (Start "C:\folder\B.txt")
)
Pause&Exit
or this
if exist "%CD%\1.txt" (Start /wait "%CD%\A.txt") ELSE Start /wait "%CD%\B.txt"
Pause
Also doesn't work.
It is no matter if I give full path or %CD% it just doesn't work.
Anyone can tell me what I am doing wrong ?

if exist "c:\program files\app\appStart.exe" ( start "" "c:\program files\app\appStart.exe"
) else (start "" "C:\programs\app\install.exe")
You need to "quote names containing spaces" and the first quoted parameter to START becomes the window-title, so you'd put an empty-quoted string for the first parameter (or "whatever window title you want")
I'm confused about the remainder of your question. Have you a further question?

This checks for both the exe files and will use the first one in preference to the second exe.
#echo off
if exist "c:\program files\app\appStart.exe" (
start "" "c:\program files\app\appStart.exe"
) else (
if exist "C:\programs\app\install.exe" start "" "C:\programs\app\install.exe"
)

Related

Why is my batch script hiding processed files/folders?

Can anyone here give me a hint, why my batch script marks successfully processed folders as system, hidden, non-archive? Furthermore I cannot even remove the "hidden" attribute via Explorer (probably because of the systemfolder attribute).
The script is meant to process one folder (passed to it as a parameter), looking for raw-photo files (.nef files in my case) that are marked read-only. For every read-only photo the script copies a specified file to the processed folder and renames that copy according to the photo filename.
The folder attribute mess is caused by robocopy. (Without that command there is no problem.) But it doesn't have to touch the folder at all. It only copies one file to that folder. The error only occurs, if at least one file in the folder was marked read-only and gets a sidecar file.
I already tried to move the script from system drive to desktop and start it from there. It made no difference.
(To avoid confusion: I am on a non-English Windows 10, so I used !var! instead of %var%. Hell it took some time to find that trick...)
echo off
setlocal ENABLEDELAYEDEXPANSION
chcp 65001
IF "%~1" == "" (
GOTO myWarning
) ELSE (
IF EXIST "%~1" (
GOTO myFuction
) ELSE (
GOTO myWarning
)
)
GOTO myFuction
:myWarning
echo Ordner-Pfad muss angegeben werden!
pause
GOTO:eof
:myFuction
echo Bearbeite %1
cd "%1"
for /r %%f in (*.nef) do (
set fileattr=%%~af
set readonlyattr=!fileattr:~1,1!
:: check if current file is read-only
IF !readonlyattr!==r (
:: create XMP-Sidecar file for read-only photos
echo %%f
robocopy "C:" "%1" "metadata-2stars.xmp"
rename "metadata-2stars.xmp" "%%~nf.xmp"
)
)
GOTO:eof
Sorry, after I narrowed the problem down to robocopy I found the solution. It seems to be a known bug in robocopy, e.g. described here:
https://blog.coeo.com/how-to-prevent-robocopy-from-hiding-your-files-and-how-to-fix-it-when-it-does
The solution/hotfix is simply to tell robocopy not to mark the destination as system hidden by adding /A-:SH at the end of the command. So with robocopy "C:" "%1" "metadata-2stars.xmp" /A-:SH everything works as expected.

Kodi - open .strm files by running a .bat / VBScript

I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF

Batch script - check to see if file exists from previous iteration, if not process [duplicate]

I have to create a .BAT file that does this:
If C:\myprogram\sync\data.handler exists, exit;
If C:\myprogram\html\data.sql does not exist, exit;
In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
Call other batch file with option sync.bat myprogram.ini.
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 can use IF EXIST to check for a file:
IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
If you do not need an "else", you can do something like this:
set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
Here's a working example of searching for a file or a folder:
REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y means:
/C Continues copying even if errors occur.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/H Copies hidden and system files also.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
`To see all the commands type`xcopy /? in cmd
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
Path/sync.bat
Path/myprogram.ini
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:
Type IF /? to get help about if, it clearly explains how to use IF EXIST.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini

Locate multiple files with the same extension batch script

I'm writing this batch script but when it gets to looking fore if all files of a file extension exists, it goes and does the else even those the file exists. Thanks for the help in advance!
#echo off
if not exist C:\Users\%USERNAME%\Desktop\workBackup (
::creates new folder 'workBackup'
mkdir C:\Users\%USERNAME%\Desktop\workBackup
)
set TIMESTAMP=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%
if not exist C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP% (
::creates new folder to put work into
mkdir C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%
)
if not exist C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs (
::creates logs folder
mkdir C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs
)
if exist C:\User\%USERNAME%\Documents\workspace\out\log\*.xlsx (
::moves all xlsx files to workBackup folder
move "C:\User\%USERNAME%\Documents\workspace\out\log\"*.xlsx C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\
) else (
::exits if file no files are found
echo "Files not found"
exit /b
)
if exist C:\User\%USERNAME%\Documents\workspace\out\log\*.xlsx (
::moves all log files to workBackup folder
move "C:\Users\%USERNAME%\Documents\workspace\out\log\"*.log C:\Users\%USERNAME%\Desktop\workBackup\%TIMESTAMP%\logs
) else (
::exits if file no files are found
echo "Files not found"
exit /b
)
There are several things to mention
the date format is a per user setting, better get date from wmic
when creating a folder structure the deepest one will imply the other
don't test, make and suppress an eventual error msg.
reduce errorprone redundancy by creating vars
put double quotes around the whole path not only part of it
#echo off
for /f %%A in ('wmic os get LocalDateTime^|findstr ^^[0-9]') do Set _DT=%%A
Set yy=%_DT:~0,4%&Set MM=%_DT:~4,2%&Set dd=%_DT:~6,2%
Set "WBu=C:\Users\%USERNAME%\Desktop\workBackup"
Set "WBuDT=%WBu%\%dd%%MM%%yy%"
:: or reverse day and month %MM%%dd%%yy%
Set "WSpOL=C:\Users\%USERNAME%\Documents\workspace\out\log"
mkdir "%WBuDT%\logs" 2>NUL
::moves all xlsx files to workBackup folder
move "%WSpOL%\*.xlsx" "%WBuDT%\" 2>NUL||(Echo "%WSpOL%\*.xlsx" not found&Exit /b)
::moves all log files to workBackup folder
move "%WSpOL%\*.log" "%WBuDT%\logs\" 2>NUL||(Echo "%WSpOL%\*.log" not found&Exit /b)

Start EXE Through Batch File

So, I'm trying to setup a basic script to install WinRAR (as my test, others later on) and I' can't seem to get it to work. Here's what I have:
#ECHO OFF
IF EXIST "C:\Program Files (x86)\WinRAR" GOTO End
IF EXIST "C:\Program Files\WinRAR" GOTO End
IF DEFINED ProgramFiles(x86) (
START C:\WinRAR_4.20_(x64).exe
) ELSE (
START C:\WinRAR_4.20_(x86).exe
)
:End
PAUSE
The first two EXIST checks work fine, but I can't get the START command to work. If I just type it out in the CMD window it starts up the installer, but it just wont do it from the batch file.
Can someone point me to where I'm screwing up?
The problem is with the FileName. Get rid of parenthesis in the file name and it should work fine. WinRAR_4.20_x64.exe and WinRAR_4.20_x86.exe
the start command requires a string for the title of the window, for instance,
start "" apples.exe
will start apples.exe with the title of the console window as
currently you are telling the start script that the title of the console window should be:
C:\WinRAR_4.20_(x64).exe
You should type in the following:
start "" "C:\WinRAR_4.20_(x64).exe"
It worked on first time only.Non of this options.
start "" C:\Program Files (x86)\MSI\Command Center\CommandCenter.exe
2)START /d start "C:\Program Files (x86)\MSI\Command Center" CommandCenter.exe

Resources