Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Questions about writing a batch file.

16 Nov 2011   #1
nzdreamer55

Windows 7 Home Premium 64bit
 
 
Questions about writing a batch file.

Hello everyone,

I've got some softball questions about writing batch files.

1)What is the proper syntax to include multiple folders in a set path statement?

for example I want to run an application called fred.exe and smith.exe.
Fred is located at c:\scripts\torrent seeds\july\fred.exe
Smith is located at \\ASUA-HTPC\Datadrive 1\TV Shows 1\smith.exe
(Note I put spaces within the locations)

2)When it the % needed around both sides of a variable?

I have seen %PATH% and %PATH. I have also seen %1 and %1%.

3)What is the ~ for when associated with a variable?

Such as SET JOB=%~1

My batch writing skills are just starting so the simpler explanation the better.

Thanks for any help you can provide me.
-Steve


My System SpecsSystem Spec
.
16 Nov 2011   #2
CreepinJesus

Windows 7 Ultimate x64
 
 

For the first item, are you wanting to run fred.exe in one folder, and smith.exe in another folder? If so, I think you can use the "cd" command in the script before running the programs.
My System SpecsSystem Spec
16 Nov 2011   #3
nzdreamer55

Windows 7 Home Premium 64bit
 
 

Quote   Quote: Originally Posted by CreepinJesus View Post
For the first item, are you wanting to run fred.exe in one folder, and smith.exe in another folder? If so, I think you can use the "cd" command in the script before running the programs.
Thanks for the reply. I think I could use the CD command, but I am not sure that this would work over the network path. I am wanting to run the exe. I'm not sure what you mean by wanting to run the exe in different folders. They are in different folders. They cannot be put into the same folder if that is what you are thinking.
There should be a way to write these two directories into a path statement so that the exe can be run without changing to the directory where the exe is located. I though that this is what the path statement was used for. Is it used for something else?

Thanks again for the help
-Steve
My System SpecsSystem Spec
.

16 Nov 2011   #4
mickey megabyte

ultimate 64 sp1
 
 

it's not the same as answering your specific questions (my knowledge of batch files is pretty weak ),

but here's a guide which may be useful: batch guide by Terry Newton and here's an a-z list of command syntax by microsoft.

happy reading
My System SpecsSystem Spec
16 Nov 2011   #5
nzdreamer55

Windows 7 Home Premium 64bit
 
 

Quote   Quote: Originally Posted by mickey megabyte View Post
it's not the same as answering your specific questions (my knowledge of batch files is pretty weak ),
No problem.

Quote   Quote: Originally Posted by mickey megabyte View Post
but here's a guide which may be useful: batch guide by Terry Newton and here's an a-z list of command syntax by microsoft.

happy reading
That is a helpful guide. I found the MS syntax list, however it reads like stereo instructions if you get my drift.

I'll leave this not solved just yet in case there are others that have the answer, but thanks for the guide link. Will be happy to read other guides too.

-Steve
My System SpecsSystem Spec
16 Nov 2011   #6
Neutron16

Windows 8.1 ; Windows 7 x86 (Dec2008-Jan2013)
 
 

1) To make cmd understand spaces you need to put the whole path in brakets.

"c:\scripts\torrent seeds\july\fred.exe"
As far as I know you can not put multiple folder locataions in one variable. But you can set a second one like:
Code:
set path="c:\scripts\torrent seeds\july\fred.exe" 
set smith="\\ASUA-HTPC\Datadrive 1\TV Shows 1\smith.exe" 
... 
%path% 
%smith%
%path% will execute fred.exe and %smith% will execute smith.exe

2) %X% will be calling variable, which was set beforehand as X

%1 will be calling value of first parameter passed to a script, %2 - second etc.

3)
Quote   Quote: Originally Posted by http://goo.gl/52ttz
The leading tilde is used to decompose elements in a batch file parameter formatted as a path, such as the parent directory or file extension.
Quote   Quote: Originally Posted by http://goo.gl/kO9Hj
Parameter Description
%1 The normal parameter.
%~f1 expands %1 to a fully qualified path name.
%~d1 expands %1 to a drive letter only.
%~p1 expands %1 to a path only.
%~n1 expands %1 to a file name only (prefix)
%~x1 expands %1 to a file extension only.
%~s1 changes the meaning of n and x options to reference the short name.

%~dp1 expands %1 to a drive letter and path only.
%~nx1 expands %1 to a file name and extension only.
Additional parameter extensions available in Win2K / XP:
%~1 - expand %1 removing any surrounding quotes (")
%~a1 - display the file attributes of %1
%~t1 - display the date/time of %1
%~z1 - display the file size of %1
%~$PATH:1 - search the PATH environment variable and expand %1 to the fully qualified name of the first match found.
My System SpecsSystem Spec
16 Nov 2011   #7
nzdreamer55

Windows 7 Home Premium 64bit
 
 

That was a great explanation. Thanks Neutron16.
-Steve
My System SpecsSystem Spec
17 Nov 2011   #8
CreepinJesus

Windows 7 Ultimate x64
 
 

Quote   Quote: Originally Posted by nzdreamer55 View Post
Quote   Quote: Originally Posted by CreepinJesus View Post
For the first item, are you wanting to run fred.exe in one folder, and smith.exe in another folder? If so, I think you can use the "cd" command in the script before running the programs.
... I'm not sure what you mean by wanting to run the exe in different folders. They are in different folders...
What I meant was you can run applications with a certain location as their "root" location.

For example, you could have a batch script that is simply "dir /B". If you run this script in, let's say, Folder A, it will list the contents of Folder A. But, "cd" to Folder B and run it from there, and it will list the contents of Folder B. It's the same script, but run from different locations.
My System SpecsSystem Spec
17 Nov 2011   #9
strollin

W10 Pro desktop, W10 laptop, W10 laptop, W10 Pro tablet (all 64-bit)
 
 

You really don't want to use Path as a variable in this instance since Path is a system variable and if you set it in a batch file it may have undesirable effects.

When you type in a command at a command prompt, the current folder is searched for that command and if it isn't found then folders listed in the Path system variable are searched. Multiple folders can be put on a Path variable, separated by a semicolon. In a batch file that changes the path you generally concatenate to it instead of replacing it such as:

SET PATH=PATH;C:\NewFolder;D:\AnotherFolder

If you want to see what the path is currently set to just enter path at a command prompt.
My System SpecsSystem Spec
17 Nov 2011   #10
nzdreamer55

Windows 7 Home Premium 64bit
 
 

Thanks everyone for the answers. Got my bat file working :-)
My System SpecsSystem Spec
Reply

 Questions about writing a batch file.




Thread Tools




Similar help and support threads
Thread Forum
Windows 7 is over-writing files that have a different file name
I have a problem when saving files in Windows 7. say I have a png with the name "alduin & khajiit 1.png" and I highlight it to grab the file name but I change the "1" to a "2" for a file name "alduin & Khajiit 2.png" in the file name field, Windows 7 still asks if I want to overwrite it despite the...
General Discussion
writing batch files in windows 7
Greetings the following simple one line batch file used to run in Windows XP fsort.bat sort %1 /o %~ns.%~x fsort list.txt would sort the text file list.txt and put the result in lists.txt now it doesn't run. it seems that windows 7 doesn't handle parameters like %~n and %~x What do I do...
General Discussion
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
Error Opening File for Writing
I have a brand new HP laptop that I have set up. I have add all of the updates, including Service Pack 1. I have tried to load Kaspersky Internet Security 2011. It downloads just fine, but when I try and run the file I get Error opening file for writing. There is no error code associated with...
Software


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 06:29.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App