|
Answer» Sorry if this is a repeat! I tried searching but it turned up nothing!
Here is the scenario: I have a .bat that copies files from a location on my c:\ to my flash drive (the .bat is run from the flash drive).
Depending which version of software is on the machine, the files will be in a specific place, ie:
Program 1.0 = c:\Program 1.0 Program 2.0 = c:\program files\Program 2.0
I was thinking of setting an IF STATEMENT that checks a variable then outputs to another variable. Something like:
SET varVersion=Program 1.0 (or I would change it to 2.0) IF %varVerson% = Program 1.0 then SET varlocation=c:\Program 1.0 ELSE IF %varVerson% = Program 2.0 then SET varlocation=c:\program files\Program 2.0 END IF
xcopy %varVerson%\file.txt \folder
Problem is, I don't know the syntax! So I have come to the land of gurus!
EDIT: BTW, I'm on 2K & XP machinesThis should cover what you need and will give you the option of inputting the program version you will use if for at the beginning when the .bat is executed.
@ECHO OFF REM This section is to determine which files to copy SET %Version=1 ECHO Which Program are you copying from? SET /P %Version=[1/2 Default 1] IF %Version%==1 SET %Location=C:\Program 1.0 IF %Version%==2 SET %Location=C:\Program Files\Program 2.0 Xcopy “%Location%\” \\
You will need to have the %Location%\ variable in quotes if you have spaces or special characters in it. The same is true for the destination (\\). All the info in <> is info you'll need to fill in as I don't know what you intend on putting in there but for the most part, this should take care of your need. If this doesn’t work for you, reply to this and I’ll get an e-mail. G’luck! Thanks for the quick reply!
That would work, except I don't want user input. I would like to specify the program before hand, run the .bat - sometimes as a unattended scheduled task - and move on.
The intent of the .bat is basically a template. I fill in the blanks, save as, fill in the blanks again and save as another file: As of now, I'm changing four variables depending on the set up of the machine (we have 100+) and wouldn't mind changing one more (varVersion) - it would save me tons of time!
EDIT: Ok, I got this to partially work:
Code: [Select]set varVersion=Program 1 IF varVersion==Program 1 set varLocation="C:\Program 1"
It SEEMS to stall right before the set varLocation="C:\Program 1". I wrote this to test:
Code: [Select]ECHO Version is %varVersion%, Location is %varLocation%.
And it returns: "Version is Program 1, Location is"
It doesn't return the variable for varLocation HELP!Here is what I was able to come up with. The Echo command is having trouble processing the "\" as far as I'm able to tell. I copied, pasted, and edited your code and came up with this:
@ECHO OFF SET %Version="Program 1" IF %Version%=="Program 1" SET %Location="Program 10" ECHO Version is %Version% Location is %Location%
When you run it, it comes up with the following output
Version is "Program 1" Location is "Program 10" You may have to leave out the C:\ in the ECHO command and just try the code out on a test machine to work out the kinks. Also, the quotation marks seem to be important when dealing with inputting spaces in variables if that variable will be used in an echo command. I don't know if this applies when the variable is NOT used as part of an echo command. :-/
Alright, after much trial and error, I think I have it. But let's cover a couple of things. I've found that .bat files don't like you to use the "\" character in a variable At ALL. There may be a workaround for this but I don't know what it is. So I dropped the whole idea of using "\" in variables and came up with the solution below. Also, when I said the .bat would allow for inputting the version number, I was assuming you would be the person running the .bat in the first place. Since you know the version number of the program (another assumption), I figured this would make it easier instead of you having to have multiple .bat files for the same process. If this is so you could get away with using it as below.
The routine below can be modified to change the path(s) you need to cd (change directory) to in order to get to the right folder. Also, the *.* would obviously copy ALL the files in the particular folder you're changing to so you'll need to replace it if necessary if you're only copying one file or files with similar names or extensions.
And finally, the destination you'd be copying the file(s) to can be modified as well.
I can come up with a modified version of the routine below if the files to be copied will have different names based on the program version, it'd only take a few minutes. Let me know if this works out for you.
P.S. I created folders and files based on the code below and the files copied as hoped
@ECHO OFF REM This section is to determine which files to copy SET %Version=1 ECHO Which Program are you copying from? SET /P %Version=[1/2 Default 1] IF %Version%==1 cd C:\Program 1 IF %Version%==2 cd C:\Program Files\Program 2 XCopy *.* F:\Test\Why not just test for the existence of which version is installed?
Code: [Select] if exist "c:\Program 1.0\nul" set varlocation=c:\Program 1.0 if exist "c:\program files\Program 2.0\nul" set varlocation=c:\program files\Program 2.0
xcopy ....
Just my 2 cents worth. MD, thank for all the work! Again, I DON'T want any user input. So this line of the code would need to be removed.
Code: [Select]ECHO Which Program are you copying from? SET /P %Version=[1/2 Default 1]
Also, when I put in the percent signs, below, the .bat opens & closes right away - it doesn't do anything.
Code: [Select]SET %Version=1 IF %Version%==1 cd C:\Program 1 IF %Version%==2 cd C:\Program Files\Program 2
I've tried multiple versions of the code and it seems that the problem is in the IF Statment at the point of setting the variable.
Question: How do you have an IF Statment read one variable and set another?
set varVersion=Program 1 REM * * * Set the variable below to either Program 1 or 2 * * * IF varVersion==1 cd C:\Program 1 IF varVersion==2 cd C:\Program Files\Program 2
Sidewinder, in some cases, both versions will be on the machines! That would be great if it was one version or another!If both versions are on the machine, somehow a rule has to be established as to what to do.
Code: [Select] set count=0 if exist "c:\Program 1.0\nul" set /a count=%count%+1 if exist "c:\program files\Program 2.0\nul" set /a count=%count%+2
count=1 program 1.0 installed count=2 program 2.0 installed count=3 both versions installed
You have to decide what to do in each case. Another option would be to pass a run time option along the command line, but somewhere along the line a decision has to be made as to what path the batch file will follow.
Batch files cannot read your mind. Perhaps in Vista SP37.
Samanathon, Are you trying to run this in a command line using the run command or in console mode? If you just go to Start, click run and type in the name of the .bat, that may be why it opens does whatever and closes. If you click Start\Run, type in cmd and hit enter, you've entered the console mode. From there you type in the .bat name and can interact with it.
Also, Sidewinder has a point. Without either having user input or testing for which version is in the machine, you'll need to maintain as many as three different .bat files (1 for each version individually and one for both versions installed). With you inputting or testing for the version, you can use just one to get the job done. The coding would be a little more difficult but not impossible.
Sidewinder, what does the /a switch do in the SET command? mmmm . . .
So, is there no way to have an IF Statment check a variable and pass another variable? Something like:
Code: [Select]SET varVersion=1 IF %varVersion%==1 set=C:\Program 1 IF %varVersion%==2 set=C:\Program Files\Program 2 xcopy %varVersion%\files.txt c:\anotherfolder
If not, what about using lables? I'm not too familiar with them but could we do somethink like:
Code: [Select]SET varVersion=1 IF %varVersion%==1 GOTO Lable 1 IF %varVersion%==2 GOTO Lable 2
LABLE 1 xcopy C:\Program 1\files.txt c:\anotherfolder GOTO END
Lable 2 xcopy C:\Program Files\Program 2\files.txt c:\anotherfolder GOTO END
EDIT: I am saving this as a .bat, and just running it myself - NOT Start->Run->CMDOkay, if you're running it by Start->Run->X.bat then that's why it's running and shutting down right away. To interact with it and see the results you need to run it in CMD Console.
You CAN have an IF statement check a var and set another var but variables seem to be unable to contain the "\" character as in
IF %varVersion%==1 SET %varLocation=C:\Folder\subfolder etc
The only way around this I know of is to actually cd to the folder based on the var checked by the IF statement. That's why I used the CMD/User input method. I don't know if you can cd to a folder and xcopy stuff to/from it by the Start->Run->X.bat method
If anyone following this can answer that you may have your solution. I'll run a few tests at my end and see what I come up with.
As far as using Labels, I have experience with using those so I can help you in that respect.
May I ask why you don't want to run the .bat in CMD so you can input the version number without having to change the varVersion everytime you use it. It would seem much easier but I'm not in your shoes so there may be a reason for that. I work with 120+ machines myself so if you've got a better method, I'd REALLY like to help you polish it out cause I may need to use it myself. Well, as of now, I have to set 4 variables. I set them in advance, then run the .bat for a week.
Next week, I change those 4 variables to fit my needs, then run them for that week.
I run the .bat by double clicking it on each of 10+ machines. It would just be a huge time saver by not having any user input!
What about:
Code: [Select]set varVersion=Program 2
REM ---------------------------------------------------------
IF varVersion == "Program 1" GOTO Program1 IF varVersion == "Program 2" GOTO Program2
:Program1 SET varLocation=c:\Program 1 GOTO Collection
:Program2 SET varLocation="c:\Program Files\Program 2" GOTO Collection
REM ---------------------------------------------------------
:Collection
ECHO Version is %varVersion%, Location is %varLocation%
I tried this in many forms, but it doesn't seem to pass the first IF Statment, it just does the:
SET varLocation=c:\Program 1 GOTO Collection
And continues! It doesn't check that varVersion is set to 2!Once you set a variable, you have to enclose it with % to access it again.
IF %varVersion% == "Program 1" GOTO Program1 IF %varVersion% == "Program 2" GOTO Program2
What happened to the machine with both versions? Or worse, that STRANGE machine with no versions?
This batch will tell you what exists... (or not) You can reconfigure the outputs to whatever you want.
c: if exist "Program 1.0" (goto exis1) else (goto not1)
:exis1 cls cd "program files" if exist "Program 2.0" (goto exisboth) else (goto not2)
:exisboth cls echo BOTH Vers Exist goto fin
:not1 cls if exist "Program 2.0" (goto exis2) else (goto end)
:not2 cls echo Ver 1.0 Exists! goto fin
:exis2 cls echo Ver 2.0 Exists! goto fin
:end echo Neither Ver Is Installed! pause exit
:fin pause
Good Luck SigmaFirst off, I want to thank you all so much for the help!!
Since, I am the one installing, setting up, run the software and collecting the data off of the machines, I know exactly which version is on the machines and which version is being RAN.
The purpose of the .bat is to make my job easier (aren't they all?!). I would set the variables on day one and run the .bat on the next five days. There are multiple directories and files that I need to copy on multiple machines - luckily all of the files are the same on all of the machines, but their locations might be different depending on which version of the program has been ran (which I know).
On the days that I collect the data, I carry the .bat around on a flash drive (I have different version of the .bat depending on the group of machines - and I know which is which and I know which version of my .bat to run). The data is collected onto my flash drive, sorted into folders according to that machine's setup.
Setting up a network is out of the question (unfortunately) - believe me, it would be so much easier!!!
So, since I have set up four different variables and saved a different version of my .bat according to the machine's setup, I simply walk up to the machine, plug in the flash drive and double click the appropriate .bat.
Files copied, sorted and ready to process!
Now, since I have two version of software, I have to go into the code and change all of the locations - it's a pain! I would love to set just one more variable, %varLocation% and save myself even more time!
Well, it just occured to me that I could just set the location as a vairable instead of using an IF Statement.
EDIT: Actually, according to the version, one file will be named differently. If we could get this IF Statment working, I could change few items . . . Would it work better if we went the IF Statment & LABLE route?
|