Stop Batch file if its being run from a drive - batch-file

Hi I want to stop a batch file if it is being run from a particular drive. I have tried somehting like this, it doesn't work though. I would appreciate it if someone has a better idea.
if %CD%=="^.*C:\" (goto :CDrive)
Where :CDrive is an error message saying that the user is trying to run it from the wrong drive.
Cheers
Chris

You can use a substring to check:
if "%CD:~0,2%"=="C:" goto CDrive
Another option might be that you just explicitly set the drive you're expecting:
pushd X:
or use full paths instead of relative ones.

Related

Error "Invalid path 0 File(s) copied" using xcopy in batch file

I want to start with a little disclaimer:
I read this thread on a similar issue to mine, but it seems like the solution doesn't work. It might just be me not understanding it completely but here I am asking for clarification.
My goal is to copy a shortcut to the start menu programs folder conserving all of its attributes, icon and start in value. I thought making a copy would be simple but it seems like my brain can't understand anything today.
So here's the actual xcopy argument:
#echo off
xcopy "%~dp0\file.lnk" "%userprofile%\Start Menu\Programs\file.lnk\" /p /v /f
pause
I have tried every combination of adding/removing the file name, with/without the \ at the end and any combination of both... I also tried running the batch file as administrator just in case.
The #echo off is just a habit and the pause is to allow me to read any error messages that could pop up. I also put the extra arguments into the xcopy line to try to get more information. It doesn't seem to help me a lot though.
I'm starting to think the issue is completely isolated from the other thread.
As suggested by SomethingDark, changing the path from %userprofile%\Start Menu\Programs\ to %AppData%\Microsoft\Windows\Start Menu\Programs\ fixed my issue.

Batch Script doesn't copy to Autostart

My batch should solve a issue, but it doesn't quite work. I think it's simple, i just don't see it. I know it needs Admin, but i shortend the code to where the problem actually is.
copy "Data\invisble.vbs" "%appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
cd "%appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
start invisble.vbs
cls
Without seeing the rest of the code it's hard to know the context.
Please note that the %appdata% environment variable changes for each user.
Also note that copy "Data\invisble.vbs" specifies a relative path.
The easiest way to pinpoint the issue is to open up a command prompt and run each line and verify the results.

Batch File - If File Drive Letter Equals W then

I have what I believe should be an easy answer to a question but I am having a difficult time figuring out the coding for it.
I have a batch file that runs when single or multiple files are sent through the SEND TO command when right clicking.
What I would like is if the file(s) are coming or located on a specific drive letter then it will go to a dialog and end.
I just need help figuring out the drive letter part...
if "file directory letter" = W: goto :errordialog
I have tried:
if %cd%=="W:\" goto :errordialog
if %~d1=="W:\" goto :errordialog
I have tried other codes but I think I have to do it another way?
I know I have to be making this too hard right?
Well what seemed to work is :
[%~d1]==[W:]
Late answer, but this is safer:
if "%~d1"=="W:"
The quotes(") makes the string as one piece. So this wouldn't happen:
if [ ]==[W:]
Although this shouldn't happen when %1 is a path, just a pre-caution.

I am trying to start an office program on a flash drive with a batch file

I am putting Kingsoft office on my flash drive, and I want to use a batch file to start the applications because the paths are not easily accessible, I cannon create a .lnk file because the path varies by computer because it may be plugged into a different port. Here is my batch file code, could somebody give some suggestions on how to make this work. Thanks in advance...
set "path=%~dp0"
start %path%office6\wpp.exe
The second line is the problem, the program won't start the program. Thanks!
cd /d "%~dp0"
start "" /b wpp.exe
I think some of the directory names in %path% contain spaces and since %path% is not enclosed within ""( double quotes), the script is unable to find the exe .
You may also want to include a log file so that it becomes easier to debug in case of any errors.
Try this:
set baseFolder=%~dp0
start "%baseFolder%office6\wpp.exe" > "%baseFolder%batchRunLog.log"

Prevent overwriting a file using cmd if exist

I am currently writing a .bat batch file that executes an installation file. Before it runs the installation file I check to see if the directory exists to avoid re-installing the application.
I do this by using a If Not Exist filename statement. If the installed file doesn't exist, I then execute the installation file.
For some reason, when I test it with the application where it has been installed already, it is still trying to reinstall the application over it.
Here is a snippet of my code:
cd "C:\Documents and Settings\John\Start Menu\Programs\"
pause
If NOT exist "Software Folder"\ (
start \\filer\repo\lab\"software"\"myapp"\setup.exe
pause
)
Where SoftwareFolder is a subdirectory of "C:\Documents and Settings\John\Start Menu\Programs\". I am checking to see if it exists in my Programs folder.
I know nothing is wrong with my start command. I have a feeling something is wrong with my beginning CD command or one of its parameters.
Thanks a lot, guys!
Use the FULL path to the folder in your If Not Exist code. Then you won't even have to CD anymore:
If Not Exist "C:\Documents and Settings\John\Start Menu\Programs\SoftWareFolder\"
I noticed some issues with this that might be useful for someone just starting, or a somewhat inexperienced user, to know. First...
CD /D "C:\Documents and Settings\%username%\Start Menu\Programs\"
two things one is that a /D after the CD may prove to be useful in making sure the directory is changed but it's not really necessary, second, if you are going to pass this from user to user you have to add, instead of your name, the code %username%, this makes the code usable on any computer, as long as they have your setup.exe file in the same location as you do on your computer. of course making sure of that is more difficult.
also...
start \\filer\repo\lab\"software"\"myapp"\setup.exe
the start code here, can be set up like that, but the correct syntax is
start "\\filter\repo\lab\software\myapp\" setup.exe
This will run: setup.exe, located in: \filter\repo\lab...etc.\
As in the answer of Escobar Ceaser, I suggest to use quotes arround the whole path. It's the common way to wrap the whole path in "", not only separate directory names within the path.
I had a similar issue that it didn't work for me. But it was no option to use "" within the path for separate directory names because the path contained environment variables, which theirself cover more than one directory hierarchies. The conclusion was that I missed the space between the closing " and the (
The correct version, with the space before the bracket, would be
If NOT exist "C:\Documents and Settings\John\Start Menu\Programs\Software Folder" (
start "\\filer\repo\lab\software\myapp\setup.exe"
pause
)

Resources