XCOPY not recognized as internal or external command - windows

I am very new to batch file coding. I am trying to use XCOPY for copying some file. First I used like this:
#echo off
MOVE mypath\abc.txt mypath\abc.txt.0001
XCOPY mypath\abc.txt.bak mypath\abc.txt
pause
This code is working perfectly fine.
Then I needed to take some input from user, o I modified the code like this:
#ECHO off
ECHO Starting application.
SET /p path=Enter the path:
SET /p corruptFileName=Enter the corrupted file name:
SET /p oldBackupFileName=Enter the Backup file name to restore:
SET /p correuptedBackupName=Enter the corrupted backup file name:
ECHO Path :
ECHO %path%
ECHO Corrupted file name :
ECHO %corruptFileName%
ECHO Desired Backup file name:
ECHO %correuptedBackupName%
ECHO Backup file name to restore :
ECHO %oldBackupFileName%
MOVE %path%\%corruptFileName% %path%\%correuptedBackupName%
call :waitfor 5000>nul
XCOPY %path%\%oldBackupFileName% %path%\%corruptFileName%
PAUSE
This code is not running. It says that :
The system cannot find the batch label specified - waitfor
'XCOPY' is not recognized as an internal or external command,
operable program or batch file.
Can anyone shed any light in this?
Thanks!

%path% is an environment variable that is used to tell the operating system where programs are located. Without it, you have to give the full path to system executables (like xcopy) before they can be called by name only.
Never overwrite it.
Change the variable name to something else. Anything else.

Related

Change batch file directory in system32 to CMD command execution point

So basically I have a .bat that is inside my System32 folder.
This batch file accepts a parameter input, this input is a file.
I wanted it to be that I could open my Command Prompt, and for example do
batchfile text.txt
And it would pass test.txt into batchfile.bat. Obviously for my terminal to do this it needs to be in System32.
That is where my issue is. Because the batch file is in System32, when executing the command, it changes my directory to System32.
However the parameter I give it is a file. And when the command is executed and it changes the directory to System32, obviously it can no longer access the file.
How can I get around this?
Before you change directories, expand the filename to its fully-qualified filename and then reference it that way:
#echo off
for %%F in (%1) do set USR_REQFNM=%%~dpnxF
C:
cd \Windows\System32
echo The requested file is "%USR_REQFNM%"
goto ENDIT
REM Cleanup
:ENDIT
set USR_REQFNM=

change directory in batch script past a folder without a name

I have a folder without a name and an image where I keep most of my important documents at work (away from prying eyes).
I'm writing a batch script, from command prompt I can change directory to any folder past that folder, but when I duplicate the exact same command in a batch script it doesnt seem to work. I get the error "System cannot find the file specified" error.
here is my script.
#echo on
net use Z: /delete
pause
net use Z: "\\agfs1\IS\Monthly reports"
timeout /t 5
cd "C:\Users\lalderman\Desktop\ \_Monthly reports"
copy /Y "Monthly Report - Lee.txt" Z:\
pause
after the cd it gives me the error. I've tried it with and without quotes.
Well,
First, for clarification, from what I see above, you have a directory with the name, " " (e.g. the directory's name is a space.).
And, I think you are having a conflict in the different ways things are read in the Microsoft platform you are using; namely between how the Command Prompt reads spaces as part of a path versus how the batch script parser is reading those same spaces in the path.
Now, I just tried doing a Change Directory operation in a batch file on WinXP across and under a folder that was just named " ", and it worked as expected.
#echo on
pause
echo "Starting..."
cd "C:\DLS\Tests\ \chkthis"
pause
echo "Did we get it?"
So, now the question is; which platform are you trying to do this on?
HTH.
Use the 8dot3 name. In your desktop directory, type dir /x /ad to see what the 8dot3 name of your "directory without a name" is and use that in your script path.

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 /.

Any way to get folder of batch script if it is run from a different location?

I have put my batch script's folder in my PATH environment variable, so I can open up any command prompt and can run it from anywhere. The problem is I have links to other files as relative paths in the script, so I need to get the path of the actual script.
Typing ~dp0 only gives me the path of the location I am in in the command prompt when running the script, and the same for %cd%. I would like the location of the actual script. Is this possible?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
:: build a simple batch file in "%sourcedir%" which is NOT on the path
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO ECHO %%~dp0
)>"%sourcedir%\q24777129s.bat"
:: Force "%sourcedir%" into the path
SET path=%path%;%sourcedir%
:: execute the batch built
q24777129s
GOTO :EOF
This little script should demonstrate that %~dp0 indeed shows the script file's location. In my case, it showed U:\sourcedir\ which is not on the (original) path.
If it doesn't work for you, then we'd need more information, explicitly specifying the procedure you are using - what you are typing, where the script is located and the exact names of the batches/executables involved.

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"

Resources