using space in path for copying files using batch script - windows

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"

Related

How to fetch single file name from folder using their extension without using for loop in shell script?

I want to fetch single file from one specific folder, the folder contains only single file , so don't want to use for loop for that . how can I write batch script for that . Below is my sample code but its uses for loop, and i think if their is only single file then why we use for loop.
cd ${p:DownloadArtifacts}\ReleaseNote
for %%# in (*.xlsx) do echo myvalue=%%~nx#
and one more script is
dir /s /p /b "*.xlsx
using this script i cant store the value into variable , please help..
By saying "from one specific folder, the folder contains only single file" why even do all this fancy stuff?
Assuming you are using copy to copy the file:
cd to dir...
copy /Y *.xlsx "C:\some directory"
I can see no reason whatsoever for wanting to do this without a loop!
The only way I can think of without the use of a temporary holding file would be using ForFiles. However ForFiles is technically a looping command in its own right.
To do it you'd need to run the original script from itself, which I suppose could be termed looping too!
Here it is though, just for the hell of it:
#Echo Off
If Defined me GoTo :Next
Set "me=%~f0"
ForFiles /M *.xlxs /C "Cmd /C 0x220x22%me%0x22 #file0x22"
Exit /B
:Next
Set "var=%~1"
Rem show variable
Set var
Pause

XCOPY not recognized as internal or external command

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.

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

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.

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.

Resources