Batch file not displaying error when no filename exists - batch-file

I am freshly new to batch and just curious why my .bat file takes input, but does not display an error or anything else, it just closes. Maybe its something i missed. Thanks!
#echo off
REM Clear the screen
REM ------------------------------------------------------
cls
REM Getting user input
REM ------------------------------------------------------
SET /p FilesToCopy="Please enter a file to copy!: "
REM 1. Copies files to myTemp
REM ------------------------------------------
IF EXIST "%FilesToCopy%" (
xcopy %FilesToCopy% C:\myTemp /E
ECHO FILE [%FilesToCopy%] are being copied!
REM 2. Displays error if not found
REM ------------------------------------------
) ELSE (
ECHO Sorry but there was a issue copying [%FilesToCopy%]..
)

Issue was that I was not running batch file from a local folder and through the cmd prompt itself.

Related

If statement in batch script isn't working?

I have a batch script which when given the input "edit", should then echo "hello" as a sort of debug and open the batch scripts file in notepad. However for some inexplicable reason the script will not respond to the if statement no matter what. How do I get it to respond to the "edit" input?
REM #ECHO OFF
cd/
cd projects\py_test
ECHO Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
ECHO.
SET /P name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
REM #ECHO OFF
cmd /k IF %name%==edit GOTO EDIT
REM IF EXIST %name% py %name%
REM IF NOT EXIST %name% echo [101mERROR: The requested file could not be found. Make sure the file exists in "C:\Projects\py_test\" and that the filename includes the ".py" extension.[0m
#ECHO OFF
:EDIT
ECHO HELLO
notepad projects-py_test-dot_start.bat`
Firstly, why all the REM #ECHO OFFs? It looks ugly, especially when they are all caps.
Then, you want to run cmd /k for an if statement for no real reason? With the variable name you need to preferbly enclose the if statement variables in double quotes to eliminate possible whitespace:
#echo off
cd /d "C:\projects\py_test"
echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
echo/
set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
if defined name set "name=%name:"=%"
if /i "%name%"=="edit" goto edit
goto :EOF
:edit
echo hello
notepad echo "%~f0"
but by guessing that you simply want to launch a python script if it exists, else edit itself, then I would instead do this version without the labels. It simply checks if the name typed exists (hoping the user typed the full script with extension) else, we add the extension test in case the user typed only the name and not extension.:
#echo off
cd /d "C:\projects\py_test"
echo Use this batch script to lauch Python modules living in "C:\Projects\py_test\" ONLY.
echo/
set /p name="Type file name with file extension .py to start or type EDIT to edit this .bat: "
if defined name set "name=%name:"=%"
if /i "%name%"=="edit" notepad "%~f0"
if exist "%name%" (
python "%name%"
) else (
if exist "%name%.py" (
python "%name%.py"
) else (
echo "%name%" does not exist
)
)

Batch Script : File Existence Based on User Prompt Location

Step for only one time
First time when the user executes the Batch file - 'Test.bat' it should prompt the user for Directory path & move the path to a text file - Path.txt and proceed with next step :Nextcommand
Step for Everytime
When the User runs the same batch file - 'Test.bat' from Second time, it should check for the existence of the above file 'Path.txt' in the above user specified directory and if available then proceed to next step :Nextcommand else have to create the file as specified in the above step.
My Code
echo off
#echo off
IF EXIST "%FileLoc%\FileLocationPath.txt" GOTO :NextCommand
set loc= Enter File Location:
set /p FileLoc=%loc%
#echo %FileLoc%>>FileLocationPath.txt
GOTO :NextCommand
For information to survive, you need to write it to a file (there are other possibilities, like Registry, but a file is the easiest solution)
#echo off
setlocal
IF EXIST "FileLocationPath.txt" (
<"FileLocationPath.txt" set /p FileLoc=
goto :NextCommand
)
set /p "FileLoc=Enter File Location: "
>"FileLocationPath.txt" echo %FileLoc%
:NextCommand
echo using %FileLoc%
If you ever need to reset the default, just delete the file.
This should be what you want.
#echo off
setlocal enabledelayedexpansion
call :check
if defined FileLoc (
echo %FileLoc%
pause
) else (
set /p "FileLoc=Enter File Location: "
echo/>>"%0"
echo set "FileLoc=!FileLoc!">>"%0"
)
goto :eof
:check
It will check the existance of the variable, which at first will not exist, User will enter it, then once user entered the location, it will store that location as a variable below the :check label. Each time from now when the script is ran, the check label is called and the already set location is set.

How do you use batch IF EXIST to test directories?

The batch here inserts file correctly but provides odd output for the IF EXIST. I have verified the issue as being with the statement by the echos before and after it, but the IF EXIST is pinging as true if the copy is going off. The error I get is the console text of "The system can not find the drive specified."
Code is below.
ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
::If image exists
ECHO 1
::1--
IF EXIST %directoryName% (
::If directory exists
::insert all below images
::2--
ECHO 2
COPY /-Y guest.bmp %directoryName% ) ELSE (
::Else echo directory doesnt exist
::2--
ECHO The folder %directoryName% does not exist.
goto ENDER ) ) ELSE (
::Else echo image doesn't exist
::1--
ECHO Images do not exist in current batch file directory.
goto ENDER )
::Exit insertion
:ENDER
PAUSE
I would highly advise you use a syntax of coding that is readable.
Proper indentation helps with readability of parentheses code blocks.
Using a double colon as a comment inside a parentheses code block can cause undesirable code output.
You can use a backslash to make sure you are testing for the existence of a directory.
Use quotes around your file names and file paths to protect spaces and special characters.
This may fix your problems.
#ECHO OFF
ECHO This batch file will place the background and user icons for Windows 7 install.
SET "directoryName=C:\Users\yourname\Desktop\BatchTestingFolder\ImageInsertReal\testfolder"
ECHO %directoryName%
PAUSE
IF EXIST guest.bmp (
REM If image exists
ECHO 1
REM 1--
IF EXIST "%directoryName%\" (
REM If directory exists
REM insert all below images
REM 2--
ECHO 2
COPY /-Y guest.bmp "%directoryName%\"
) ELSE (
REM Else echo directory doesnt exist
REM 2--
ECHO The folder %directoryName% does not exist.
goto ENDER
)
) ELSE (
REM Else echo image doesn't exist
REM 1--
ECHO Images do not exist in current batch file directory.
goto ENDER
)
::Exit insertion
:ENDER
PAUSE

Batch script to Read the filenames in a directory and ask user to select one

Thanks for looking into my concern.
I have five config files in a given directory. In my batch script, I want to read those file names and prompt them to user. Once user has selected a config file, read the variables from it.
Could anyone help me with some logic here. So that, I can take it forward.
Thank you.
A batch or .cmd file like this demonstrates the menu technique (nothing fancy, the user has to enter the filename precisely). Key items:
FOR
SET /P
IF EXIST
Good luck!
#echo off
REM Show the user the list and ask them which one to use
echo.
echo Please select one of:
echo.
for %%F in ("D:\A Given Directory\*.config") do echo %%~nxF
echo.
set SEL_CFGFNM=
set /P SEL_CFGFNM=Which configuration file:
REM Make sure they answered, and that the file exists
if "%SEL_CFGFNM%" == "" goto ENDIT
if NOT EXIST "D:\A Given Directory\%SEL_CFGFNM%" goto NOCFG
REM User has selected file "D:\A Given Directory\%SEL_CFGFNM%" and it exists
REM Do whatever you want to do with that file now
REM Don't fall through the exit messages
goto ENDIT
REM Exit Messages
:NOCFG
echo.
echo ERROR: Configuration file "%SEL_CFGFNM%" is not on the list
echo.
goto ENDIT
REM Cleanup
:ENDIT
set SEL_CFGFNM=

Altering values in .json files

I have a simple question.
I have a config.json file with the below content
"DefaultAdminUsername": "Administrator#test.com",
"DefaultAdminPassword": "Admin!",
"DefaultNoOfUsers": "100",
I want to modify the value of "DefaultNoOfUsers" from a batch file
for example : Change from 100 to 1000
A pseudo code could be
for /f %%A in (config.json) do (
SET key = value
)
echo %VERSION%
echo %TEST%
Here is a little commented batch file for making this replacement in file config.json without checking for input mistakes.
#echo off
rem Exit this batch file if file config.json does not exist in current directory.
if not exist config.json goto :EOF
rem Make sure that the temporary file used does not exist already.
del "%TEMP%\config.json" 2>nul
rem Define a default value for number of users.
set "DefaultNoOfUsers=100"
rem Ask user of batch file for the number of users.
set /P "DefaultNoOfUsers=Number of users (%DefaultNoOfUsers%): "
rem Copy each line from file config.json into temporary file
rem with replacing on line with "DefaultNoOfUsers" the number.
for /F "tokens=1* delims=:" %%A in (config.json) do (
if /I %%A == "DefaultNoOfUsers" (
echo %%A: "%DefaultNoOfUsers%",>>"%TEMP%\config.json"
) else (
echo %%A:%%B>>"%TEMP%\config.json"
)
)
rem Move the temporary file over file config.json in current directory.
move /Y "%TEMP%\config.json" config.json >nul
rem Delete the used environment variable.
set "DefaultNoOfUsers="
Run in a command prompt window for /? and read entire help output in window to understand how the for loop works.

Resources