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.
Related
I am trying to create a script that will repair and reinstall a program. The problem is that the only place I can find an installer for the program is in C:/ProgramData. The folder name that it is saved in is randomly generated and the actual name of the file will vary depending on which version of the program they have installed 1.1, 1.5.1, etc. I need to create a command that will search for the executable file and run it. An example of what the installer might look like is program1.5.1.exe.
I have already tried using findstr, dir, etc., but I have not had any luck.
Here are some of the commands that I have tried.
cd C:/ProgramData
findstr /s program*.exe
cd C:/ProgramData
dir /s program*.exe
When I use the findstr command, it keeps running and never finds the executable. When I run the dir command, it tells me that "Volume in drive C has no Label", tells me my serial number, and then it says "File Not Found."
I figured out how to run an executable that is in a folder with an unknown name and that has a file name that will be different depending on the version of the program. I was trying to find and run a test file that I called "program1.3.0.exe". Note, I ran this in a batch file so if you are running this directly in the command prompt you will need to type %i instead of %%i.
for /r C:/ProgramData %%i in (program*.exe) do start %%i
The for command searches through the directory
/r tells it to search within the subdirectories
C:/ProgramData is the directory that it will search within
%%i is a variable that holds each file that it finds
in (program*.exe) tells it to only use results that start with program and end with .exe
do start %%i will run each executable that it finds
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 /.
I am writing a batch file that uses some of the files within it's parent directory (lets say Folder1).
C:\User\Steve\Foder1\
Now I want to make the whole Folder_1 relocatable so that I can copy paste the folder anywhere on my/someone else's computer and run batch script.
D:\User\Random_guy\Folder1\
The question is how do I start batch file's command prompt to (D:\User\Random_guy\Folder1) it's parent directory without writing another batch script to do that.
Start your batch file with:
pushd %~dp0
That will set the current directory to be the folder containing the batch file. Then in the batch file, make sure all your paths are relative to the current directory.
However, if your batch file changes to other directories in the course of its execution and you would still like to be able to refer to the batch's home folder's contents without knowing the exact path, use the same %~dp0 as the path to the file(s) you want to use. For instance, a FileA from the same folder as the batch file would be addressed as
"%~dp0FileA"
Note the absence of a \ before the FileA. This is because the %~dp0 already includes a trailing \ and so the entire thing will evaluate to a correct path all right. (Although if you do put another backslash, like "%~dp0\FileA", it should work as well, because Windows usually disregards multiple consecutive backslashes when in the middle of a path.)
%CD% in a batch file gets the current directory/directory batch is being run in.
So - if I've got this right, you want to run
`C:\steve\folder1\yourbat.bat`
which should copy C:\steve\somefiles to D:\Random_guy and set the current directory to D:\Random_guy\folder1 ?
#ECHO OFF
SETLOCAL
SET subdir=folder1
SET destdir=%~1
IF NOT DEFINED destdir ECHO Require destination username&GOTO :EOF
SET destdir=%~1\%subdir%
:: Ensure destination exists
ECHO MD "%destdir%" 2>nul
SET "sourcefrom=%~dp0.."
ECHO COPY "%sourcefrom%\filestocopy" "%destdir%\..\"
ECHO CD /d "%destdir%"
Note that the MD, COPY and CD commands are merely ECHOed, you'd need to remove the ECHO keyword to execute them.
Run the batch with a parameter of D:\Random_guy
Ok so I was making my own little command prompt for my own personal use and I have been fighting to make it work for the past 2 hours. Here is what I have done:
#echo off
set /p labnum="Enter Lab Numnber:"
set labdir=C:\Users\BLAHBLAHBLAH\Dir\Lab-
set labdir2="%labdir%%labnum%"
cd labdir2
:cmd
set /p cmd=">"
%cmd%
cls
goto cmd
I basically want to be able to change the path before each "session" But every time the cd labdir2 command is executed, my computer whines, "The system cannot find the path specified." And I know FOR SURE that the directory exists! I have pasted the text straight from windows explorer. Any and all help is appreciated.
Thank you!
The error is here:
cd labdir2
that changes to a directory called labdir2, but you want to change to a directory indicated by the contents of the variable:
cd %labdir2%
to make sure you can cope with special characters I'd enclose it with double quotes:
cd "%labdir2%"
You might even want to include the /d swith with the cd command to make sure that you also change the current drive. So the final version should be:
cd /d "%labdir2%"
Windows x64 versions contain folders named with parenthesis like "\Program Files (x86)" and this breaks a batch file I use. An example of a problem line:
for %%c in (%path%) do if exist "%%c\xyz.exe" set xyz=OK
i.e. when it reaches ")" in "(x86)" it puts out an error message and exits...
Any ideas on how to fix this?
This is a rather large batch file, and atm I don't have the time to rewrite it in a better language...
Many thanks :)
Doesn't directly answer your question, but if you are trying to do what I thinking you are trying (which is make sure a file exists in the path) you can use something like the following in a batch file.
#echo off
for %%i in (xyz.exe) do set xyz=%%~$PATH:i
if "%xyz%" == "" Goto NotFound
Echo "Found"
Goto TheEnd
:NotFound
Echo "Not found"
:TheEnd
Normally quoting should work, but in this case you want to iterate over all elements seperated by ;.
But you can replace the ; to a " " combination, so the brackets are quoted and you can iterate over the elements.
sample: path=C:\temp;C:\windows;C:\Program Files (x86)
The for-loop will search in
"C:\temp" "C:\windows" "C:\Program Files (x86)"
As code it looks like
setlocal EnableDelayedExpansion
set "searchPath=!path:;=" "!"
for %%c in ("!searchPath!") do (
if exist "%%~c\xyz.exe" set xyz=OK
)
You can use the short names of the folder for this purpose. This is how you do it.
Open command promt in Windows.
Go to C drive (or the drive in which you have the Program Folder)
Type the following and
c:\> dir /x <Hit Enter>
This will return the short forms of all folders.
You will notice now that "\Program Files (x86)" will be represented as "PROGRA~2" (or an equivalent short name).
This is what I use to prevent any errors while creating Batch scripts.
For more options see here.
http://www.computerhope.com/dirhlp.htm
Exlpanation for "dir /x"
"This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place."