I am using set command in cmd to assign global variables
set TEMPDATA = C:\temp_data
In the same cmd session I am calling a Perl script
my $temp_path = $ENV{'TEMPDATA'}."\\temp.c";
But it gives this error:
use of uninitialised value $ENV{'TEMPDATA'}.
when I use setx then it works.
But I need to have a temporary variable which should be deleted as soon as session is closed and for that I need to use set only
do not include blank spaces in variable declaration. Use like this:
set TEMPDATA=C:\temp_data
Related
I have a batch file that runs off a powershell command I created, and I want to make it so you can pass a variable from the PS command to a website url query..
My problem is that the variables I pass from PS are referenced in batch as %1, %2, %n
now I set these variables at the beginning to more meaningful named variables, but in my url eg: www.google.com/myQuery%20has%20spaces would print out my %2 variable from PS instead of a space.
Is there anyway to clear out the %1,%2 variables that are passed? or any work around?
edit: I have tried a simple set %1= to try and set it as a null variable but it didn't work.
You can escape the % in the string with a %% in batch, which solves the issue.
I have to run a VB script which has 2 arguments. So I am running a command below.
Delete_Dummy1.vb
s C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv C:\Users\c6342\Deskto
p\XML_to_CSV\Temp_Files\xml1.txt
**VB Script Sample - not working:**
**sourceloc** = WScript.Arguments.Item(0)
**destloc** = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oTextStream = objFSO.OpenTextFile("**sourceloc**")
Set newFile = objFSO.CreateTextFile("**destloc**")
It throws the error as file not found. But if I hard code the sourceloc and destloc and remove the arguments it is working fine. it is throwing error only when i am using arguments.
**Working VB Script sample:**
Set oTextStream = objFSO.OpenTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml.csv")
Set newFile = objFSO.CreateTextFile("C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt")
This works fine. But as per my project requirement, I cant hard code these file locations. I can pass as parameters from command.
After a length discussion in the comments think it will be best to just update my answer.
The reason for the Arguments not working is you never pass them to the OpenTextFile() and CreateTextFile() methods. Instead you are passing literal strings containing the variable name instead of the actual variables.
sourceloc = WScript.Arguments.Item(0)
destloc = WScript.Arguments.Item(1)
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Pass the variables not a string literal
Set oTextStream = objFSO.OpenTextFile(sourceloc)
Set newFile = objFSO.CreateTextFile(destloc)
As it stands VBScript keeps trying to locate a file called sourceloc and destloc instead of the actual file names from the passed Arguments collection. Which is what likely causes the
Microsoft VBScript Runtime Error: File Not Found
Note: Below is based on initial question which has since been revised.
This comes down to how you are passing the arguments to the script, any spaces in the values will be treated as new arguments. At the moment this is how the arguments are passed;
0. C:\Users\enter
1. code
2. herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
3. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt
I'm sure this isn't what you expect. To avoid this enclose each argument in double quotes ("...").
Delete_Dummy1.vbs "C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv" "C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt"
That way you get more what you expected
0. C:\Users\enter code herec6342\Desktop\XML_to_CSV\Temp_Files\xml.csv
1. C:\Users\c6342\Desktop\XML_to_CSV\Temp_Files\xml1.txt
Quick question... I'm trying the following:
set changedir "cd $JSDIR/"
eval $changedir
..Where $JSDIR is defined already. Running this script gives the error: "set: Variable name must begin with a letter."
What's the fix?
Thanks.
That's not an error message generated by Tcl, because Tcl's variables most definitely do not need to begin with a letter. OK, it tends to be convenient to keep them alphanumeric because the $var shorthand syntax is more restrictive than the general space of variable names, but the set var form can handle virtually anything.
I'm guessing that script is being evaluated by something that isn't Tcl, perhaps bash?
Try to set changedir cd ${JSDIR/} instead
This message can appear when in fact the variable name is entirely correct, and the real problem is that the variable value needs to be quoted. Try instead of: set changedir "cd $JSDIR//"
I have a variable that is common to most of my app called "emails". I also want to use "emails" as the name of a parameter in one of the scripts. I need to refer to the value of both variables in the same script. Ideally there would be a way to refer using module/namespace or something and perhaps there is but I don't know it. You can see how I hack around this but it is ugly and prone to error. Is there a better way?
# PowerShell v1
# Some variable names are very common.
param ($emails)
# My Hack
# We need to save current value so we have it after we source in variables below.
$emails0=$emails
# Below is going to load a variable called "emails" which will overwrite parm above.
. C:\load_a_bunch_of_global_variables.ps1
It is because as documentation says: (the dot sourcing operator) Runs a script so that the items in the script are part of the calling scope.
In this case I would convert C:\load_a_bunch_of_global_variables.ps1 to a module and pass $emails as parameter or export a function that sets the $script:emails variable in the module. Then the variable will not be in a conflict with the variable in the parent script.
For more information about modules you can use get-help about_modules.
I would avoid using global variables if possible in my scripts.
Why? Because it is a code smell (as programmers say). With one script there is no problem. If two scripts use the same global variable and only read, it is maybe acceptable. But if any of them changes the value, then there might be unpleasant conflicts.
In some cases Get-Variable -scope 1 -name myvariable would help, but I would use it only in closed pieces of code like modules or in short scripts (the same reason as with global variables).
While you can use Get-Variable -scope to get access to variables at arbitrary levels of the call stack, it is easier in this case to grab the top level (to the script) variable using the script: modifier e.g.
$script:emails
rerun and stej both helped me out.
I still want to source in the file using ". file.ps1" but changing "$emails=foo#yahoo.com" in my load_a_bunch_of...ps1 file to "$global:emails=foo#yahoo.com" solved the problem. I can now refer to the variable using global key word when I have a local and a global variable, and when there is only one variable to deal with I can leave out the global keyword.
You can alwways access your global variables from a script using $global:var name inside your script you have local scope and you won't get collisions. If you . source your script you will override the global var.
For Ex if a have a script
$Crap ="test"
$Crap
And you run the flowing commands you get what you want. In line 2 we run the script and the var doesn't get a conflict but if you run the script as in line 4 with a . source you get what you are discovering which due to the way the . operator works
1:PS C:\Users\Adam> $crap = "hi"
2:PS C:\Users\Adam> .\test.ps1
test
3:PS C:\Users\Adam> $crap
hi
4:PS C:\Users\Adam> . .\test.ps1
test
5:PS C:\Users\Adam> $crap
test
6:PS C:\Users\Adam>
if You add the following line to the script run it
$global:crap;
you will get
PS C:\Users\Adam> .\test.ps1
test
hi
how do you define a variable to an integer at the command line. For example if i want to assign A=22 and B=23 and then have the A and B = a separate variable such as C..? I am confused on the syntax of this at the command line. I understand how to set variables in a script but how would i do it from a command line using only 1 line?
A=22; B=23; ((C=A+B)); echo $A $B $C
Assuming Bash or Korn shell.
set A=22
set B=23
set A=%C%
set B=%C%
In windows bat-files. Also be careful not to use any extra spaces in batch file syntax. It can screw up everything.