How to make a folder in the location of a batch file - windows

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

Related

Why is this Windows Batch script not able to find the program net.exe?

I'm creating what was meant to be a very simple batch script to map a drive based on user input and check that the drive doesn't already exist first.
#echo off
:EnterInfo
set /P path=Please Enter The Path You Want To Map(EG: \\server\folder)
set /P z=Please Chose A drive Letter To Map.(EG Z)
goto :CheckExist
:CheckExist
%z%:\
pause
if exist %z%:\ (
set /P Sure=A Drive Is Already Using Drive Letter %path%:\ Are You Sure You Want To Replace It?[Y/N]
if /I "%Sure%" EQU "Y" goto :SetDrive
if /I "%Sure%" EQU "N" exit)
goto :SetDrive
:SetDrive
C:
dir
net use z: "%path%"
pause
Here is the code so far.
I'm getting an error:'net' is not recognized as an internal or external command,
when the script gets to the(Net use Z: "%path%")
Although the input to the command would work if i were to run it in a cmd window.
I've checked the location the script and it's running from C:\Users\%username%\ like a cmd box would default it's location too.
I'm really confused as to why it's not recognising net Use. Thanks in advance
The problem is this line:
set /P path=Please Enter The Path You Want To Map(EG: \\server\folder)
The PATH environment variable is already used by Windows. It tells the command interpreter where to find programs like net.exe, which is in your sys(tem32|wow64) folder. You can't just overwrite it as you please and expect everything to work properly afterward. You set the variable PATH to be the network path for the drive you want the user to map, which overwrites the actual PATH variable.
Solution: Use a different variable name, like, say, mypath.

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.

Delete files from folder [Windows]

The problem is as follows - I have a text file with an imagelist which contains the names of photos. The photos reside somewhere else on a hard drive.
How do I delete/move those photos which names are in imagelist? Is there any batch routine?
This very simple sample should help you get going:
set filename=files.txt
set targetdir=bar
set sourcedir=foo
for /f %%i in (%filename%) do move "%sourcedir%\%%i" "%targetdir%"
pause
Note that there is absolutely no error handling. The pause at the end keeps the command window open so you can review the output
type for /? at the command prompt to get help for this command

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.

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