How can I check if either or file exists in IF Exist? - if-statement

How can I check if either or file exists in a If Exist statement?
Such as
If exist "C:/Windows/" OR "C:/Windows2" (
Do something
) else (
Something else
)
How would I do this? I just want either or to exist and to do something.

Simple example 1:
#echo off
if not exist "%SystemRoot%\" if not exist "C:\Windows2" goto MissingFolderFile
echo Found either the directory %SystemRoot% or the file/folder C:\Windows2.
rem Insert here more commands to run on either the folder C:\Windows
rem or the file/folder (=any file system entry) C:\Windows2 existing.
goto EndDemo
:MissingFolderFile
echo There is neither the directory %SystemRoot% nor the file/folder C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder C:\Windows2 existing.
:EndDemo
pause
The Windows command processor is designed for processing one command line after the other which is the meaning of the word batch. The command GOTO is the preferred command to use in a batch file to continue batch processing not on next command line, but another one depending on an IF condition, i.e. change processing from one stack (other word for batch) of command lines to another group of command lines.
Simple example 2:
#echo off
if exist "%SystemRoot%\" goto FolderExists
if exist "C:\Windows2" goto FS_EntryExists
echo There is neither the directory %SystemRoot%\ nor C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder/reparse point C:\Windows2 existing.
goto EndDemo
:FS_EntryExists
echo The file system entry (file or folder) C:\Windows2 exists.
rem Insert here more commands to run on C:\Windows2 existing.
goto EndDemo
:FolderExists
echo The folder %SystemRoot% exists.
rem Insert here more commands to run on folder C:\Windows existing.
:EndDemo
pause
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.
echo /?
goto /?
if /?
rem /?
NOTE:
The directory separator on Windows is \ and not / as on Linux or Mac. The Windows file management automatically replaces usually all / by \ before passing a file/folder argument string without or with a wildcard pattern to the file system as explained by Microsoft in the documentation about Naming Files, Paths, and Namespaces. But the usage of / instead of \ in file/folder argument strings can result nevertheless in unexpected behavior.
Example for unexpected behavior because of using / on running following command line directly in a command prompt window:
for %I in ("%SystemDrive%/Windows/*.exe") do #if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"
This command line outputs a list of executable file names found by FOR in the Windows directory which do not exist for command IF just because of usage of / resulting in getting assigned to the loop variable the found file names without path. So this command line works only if the current directory on system drive is by chance the Windows directory.
The same command line with usage of \ as directory separator:
for %I in ("%SystemDrive%\Windows\*.exe") do #if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"
This command line outputs each file name of the executables in Windows directory as existing file with full path.
Another example:
There is in root directory of current drive a directory Downloads and the current directory on this drive is Temp, for example D:\Downloads is the wanted current directory and D:\Temp is the current directory.
The used command is:
cd /Downloads
The result is the error message:
The system cannot find the path specified.
The command with correct directory separator usage:
cd \Downloads
This command works on D:\Temp being the current directory and D:\Downloads existing.
CD interprets on incorrect /Downloads the string /D at beginning of the directory path as option /D to change also the drive and searches for that reason for ownloads in current directory instead of Downloads in root directory of current drive. This wrong interpretation by CD is avoided by using the correct directory argument string \Downloads.
SUMMARY: \ is the directory separator and / is for command options.

Related

python is not recognized as an internal or external command while running batch file

I am set environmental variable for python and I am able run python if I not take user input in batch file. But If I take user input I am not able to run python scripts in batch file.
#echo off
set /p Path="Enter Folder path in which all the timestamp files are stored: "
echo %path%
:: this is to read ip address from report folder to ipaddress.txt
dir %Path% /b /a-d > ipaddress.txt
echo %cd%
python trial.py
Choose a variablename other than path.
path is a reservd name that contains a list of the directories to search for an executable - like python.exe

explorer command not working in batch file

i have a batch file that opens an item in a list of paths but the explorer command is not working (final is a list of paths )
can anyone tellme why this command isnt working
its sending the message:'explorer' is not recognized as an internal or external command,operable program or batch file.
setlocal enabledelayedexpansion
set path=c:\users\user1\desktop
set numz=0
set /p go=
for %%k in (%final%) do (
set /a "numz=!numz!+1"
if [!numz!]==[%go%] (explorer %%k)
)
you changed the %path% variable. Don't do that (use another variable name). Windows uses the %path% variable to know where to find it's executables. Because you deleted that info, Windows is not able to find explorer.exe any more - hence the error message.
Gladly, this concerns only the current cmd session (and it's daughter-processes). Change the variable name, close the cmd window, open a new one and it should work fine.

Where are files placed when a path does not include a path or drive letter?

When I indicate a file to create and write to via ofstream without a path or drive letter, i.e. "testfile.txt" where is it placed when NOT run in an IDE (when run in VS, the file is placed in the project working directory) and run from a shortcut (I needed to indicate command line arguments)? It does not place it in the same location as the executable when run from a shortcut.
Your shortcut has a "Start in" property, which is the directory where your files will be placed by default (i.e. if you don't specify a path). The main exception is that in Vista, if the directory is in \Program Files\ the actual writes will be redirected to your profile directory.
If you start the program from the command prompt, the default directory is the working directory (i.e. your CMD.EXE prompt when you started your program). This isn't necessarily where your program is located. If your program is on the %PATH% or if you specified a full path to your executable, CMD can run your executable even if is stored outside your current working directory.

How to turn off Interactive mode in FTP (perl)

I am trying to delete a non-empty directory via FTP using a Perl script. In order to do this I first need to remove contents inside this directory and then delete directory.
In FTP you need to disable prompt to do this. Otherwise it will keep asking for confirmation on deleting every file.
ftp> prompt
Interactive mode off.
ftp> mdelete 2013-10-01-full/*
ftp> rmdir 2013-10-01-full
How can I turn prompt off in Perl. There is no such feature listed in Net::FTP. I even tried $ftp->prompt;
From the Net::FTP documentation:
rmdir ( DIR [, RECURSE ]) Remove the directory with the name DIR . If
RECURSE is true then rmdir will attempt to delete everything inside
the directory.
You don't need to worry about the prompt thing. Just use $ftp->rmdir($dir, 1) and it will delete the dir including everything in it.

Windows XP Batch IF & XCOPY

I've been struggling with trying to get the below (example) batch file to work on Windows XP SP3. The IF NOT EXIST part seems to work, but I keep receiving the "Does <path\filename.ext> specify a file name or directory name on the target" message despite using the /I flag on XCOPY:
#IF NOT EXIST "\\SERVER\PATH\TO\FILE DIR" (
MKDIR "\\SERVER\PATH\TO\FILE DIR"
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
) ELSE (
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
)
My understanding is that with the /I switch, XCOPY should create the directory structure if it doesn't exist - at least it does when I don't specify a file name. Unfortunately for the requirements of this project, I must specify a file name and cannot keep the original as it's a template file that gets manipulated with an automated process every day.
So, I tried to get around the issue with XCOPY and the directory path not existing by checking for the existence of the path, and if it's not there, creating it with the MKDIR command and then copying the file - but XCOPY still prompts as to whether the destination is a file or directory, which doesn't make sense but maybe I'm missing something.
Just to be clear, this is on Windows XP SP3.
Any ideas?
You might find it easier to do something like this:
md "\\SERVER\PATH\TO\FILE DIR" 2>NUL
copy "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
The initial 'md' will attempt to create the directory. If it already exists, it will output an error message to STDERR. The 2>NUL redirects that to Windows' built-in "null device", which is to say, it just swallows the error message. Assuming you have the appropriate permissions, you can be sure that this directory exists now.
The copy command just copies your file. No need to use xcopy to copy a single file - that's both overkill and fraught with little gotchas like being prompted whether it's a file or directory.
Since the destination file doesn't exist before you copy, xcopy isn't sure if it needs to create a new directory called "FILE FINAL.XLSM", and put the file in there. By the way, since you already create the destination dir, you don't need the /I on your xcopy. Here are a couple ways to do what you want:
echo F | xcopy .... (feed the "F" answer to xcopy)
copy .... (you don't need to use xcopy for a single file)
echo f|XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
should copy the file AND create the directory. No idea why the option to specify "this is a file" wasn't made available, but RTFM - the /i switch is only effective if you are copying MORE than one file, and specifying \ as the last character of the destination name tells XCOPY that the target is a directoryname in any case, so /i sems redundant.
However, be careful if you follow the copy route. It's better in general to use copy /b because plain copy may fail to properly copy some filetypes (like .MPGs) - it may stop on the first ^Z. copy /b appears safe however.