Thanks for looking into my question.
I have a batch command to copy the files to remote server and if I run this command from command prompt it ran well.
echo D|xcopy /E /Y ..\Media \\%Win_Machine%\C$\temp\%APP_VERSION%\Media
Whereas if I run the same command through batch file, it says "Invalid number of parameters".
Please help me if I am missing anything here.
Thanks.
echo D|xcopy /E /Y ..\Media "\\%Win_Machine%\C$\temp\%APP_VERSION%\Media"
Invalid number of parameters would indicate that xcopy sees three or more parameters. Since the first two arguments are switches, then it would seem that the final argument is being interpreted as two or more parameters - which would mean that the values of the user-variables would contain separators. Quoting the arguments tells cmd to interpret the string between the quotes as a single entity.
It was resolved. Forgot to post the answer. I see extra spaces being posted %APP_VERSION%. It is working fine now.
anyway, Thanks Magoo for valuable inputs.
Related
I need to save the output of a curl command to a variable. The only issue is that the output contains spaces and the for command splits them on different lines (the output variable only contains the first block of characters).
Code:
for /F usebackq %%I in (`curl "https://api.checkwx.com/metar/cyyz?x-api-key=c6e048117df34d45871ca85e73"`) do set output=%%I
echo %output%
Outputs: {"data":["CYYZ
Instead of: {"data":["CYYZ 221700Z 00000KT 15SM BKN220 05/M11 A3031 RMK CI7 SLP271"],"results":1}
Am I doing anything wrong?
Thank you #jeb your solution works.
In regards to #Compo's answer, I read the official Microsoft online documentation regarding the for command, but couldn't find a solution there.
It seems like the actual problem was with my . I didn't know they were supposed to be quoted and looking at the examples in the documentation, none were. I tried delims= before but the batch file simply crashed upon that command.
I have a file (let's call it version.txt) that contains a version number and some text:
v5.02
Some text explaining
where and how this
number is used
Based on this answer, I use
set /p version=<version.txt
to store the first line of the file in the version variable. Now I'm trying to write a batch script that operates on folders that contain this version number in their name. However, I get unexpected results because something seems to go wrong when I insert the variable in a path. For example, this script
#set /p version=<version.txt
#echo C:\some\folder\%version%\some\file.exe
prints
C:\some\folder\v5.02
instead of
C:\some\folder\v5.02\some\file.exe
What's going on? I have a feeling there are hidden characters of some sort at the end of the text in the variable, because setting the variable by hand to a constant in the script works.
Edit: I'm using Windows 10 with Notepad++ as my editor, if it helps.
I can only replicate your issue, when version.txt uses Unix line endings (LF) instead of Windows (CRLF). for /f is immune to this issue:
for /f "delims=" %%a in (version.txt) do set "verion=%%a" & goto :skip
:skip
echo C:\some\folder\%version%\some\file.exe
goto :skip breaks the loop after reading the first line.
Since everything I tried didn't seem to work, the solution I found in the end is to call the batch script from a Python script. The Python script reads the first line of the version file and passes it as an argument to the batch script. Out of context, it is a bit of an inelegant solution, but in my case the batch script was already called by a Python script, so it's not that terrible.
Here is a minimal example:
version.txt
v5.02
Some text explaining
where and how this
number is used
script.bat
#echo C:\some\folder\release\%1\some\file.exe
script.py
import os
with open("version.txt") as f:
version = f.readline().rstrip()
os.system("cmd /c script.bat %s" % version)
Edit: Following Stephan's comment, I tried to change the line ending in the text file from LF to CRLF and it indeed solves the problem. However, since I don't really have control over everything that writes in that file, the solution above remains the most feasible in my case.
Edit 2: Stephan's answer (with the for loop) is actually a better solution than this one since it avoids having to transfer part of the work to the calling Python script.
I have the following case. In jenkins I have one build which is running on different envoironments. That's why I have build with parameters with two options PROD/TEST. The build is invoking shell script with parameter PROD or TEST.
Here is example of the script A which jenkins is invoking:
if %1%==TEST(
start F:\test.bat
)
The script A itself is invoking another script - B.
Here is example of script B:
copy test.xt copyFolder\
The problem is that Jenkins only invoking the first script - A - and the second script B doesn't run.
Why does this happen?
You will need to call the batch file, not start it because it creates a new cmd.exe instance, so it can run a called batch file asynchronously (as mentioned by jeb here):
if "%~1" == "TEST" (
call F:\test.bat
)
Here, I want to note some things:
%1% will be interpreted as the first argument of the batch file (if any) and an extra percent-sign (%). You probably wanted here the first argument, so I have replaced %1% with %1. If it is not this what you wanted, then replace it with your variable name, but remember that it should not start with a number!
%1 was replaced by %~1 and quoted because:
%~1 means the first argument without any surrounding quotes.
Quoting the values in an if statement is always the best practice, but if there were quotes, the comparison would fail.
Added a space between ==, just to make the code clearer.
For an one-liner, see aschipfl's comment, it is:
if /I "%~1"=="TEST" (call "F:\test.bat")
See call /? and if /? in cmd for more information about how these commands work.
This is how I have always remembered processing the CONTENTS of a file line-by-line in a batch file (backed up by web search):
for /F "delims=" %%N in ("del files.txt") do del "%%N"
It now deletes "del files.txt" and #echo %%N gives "del files.txt"
Does it have something to do with the name needing to be quoted because of the space? WTF am I missing?
("string") is used to parse strings. ("filename") can be used with usebackq. It seems you confused them in your search. So, you can write:
for /f "usebackq delims=" %%N IN ("del files.txt") do whatever you want.
Please don't accuse me of not reading help.
for /? (first thing I did) yields:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters] MAKE NOTE THAT 'string' IS IN SINGLE QUOTES!!!
FOR /F ["options"] %variable IN (command) DO command [command-parameters]
because I just happened to lose the "if usebackq..." line because it just happened to get lost coinciding withe the "press any key..." line in my command prompt window because it just happened to be sized unfortunately (and still doesn't show file-set in standard quotes).
I was asking why quotes wouldn't be used in (file-set) when they are required for spaces and used in (file-set) without /F (i.e. for \%f in ("c\:path w spc\*.txt") do...)
I have used that exact syntax in other working batch files. though I went and checked an in each case, %1 or an environment var were in () which apparently settled how quotes were addressed.
#1 google search result: ss64.com/nt/for_f.html which in my frustration I misread the usebackq in reverse and tried ` because it went on to say
"The backquote character ` is just below the ESC key on most
keyboards"
, indicating to me that I was supposed to use that (once or twice) as quotes as seems rational to me if I'm giving direction to use that character.
I am providing this clarification in case it helps others experiencing my frustration.
Those who have "graded me down" for what I believe is legitimate confusion by doing so also prevent me from asking a different question in a totally unrelated area and I would appreciate if they would re-evaluate. I have certainly learned my lesson about unfortunate series' of events.
I understand the reasons that underlie the policy but am very frustrated that 1 time in the years I have been a member being criticized, just for poor luck IMHO, for a question should penalize me apparently 4 or 5 days.
(It's also confusing since since the message says
It's been 2 days since you asked your last question. We ask that you wait 2 days before asking again. Use this time to revisit your previous questions, editing to address any issues that folks have pointed out in comments.
Does that mean 2 more days or the two I have waited?)
So I guess if people want to pound on me for this protest, how many more days can it cost me?
And a further Thanks! to double-beep for the useful answer even if it wasn't a complete explanation.
Well, back to the penalty box...
I have the following entry in the file Build.aip. I need to write a batch file that searches for "PackageFileName and prints the value assigned for that in the file. In this case, I need to print MyPackageName on the console:
<ROW BuildKey="DefaultBuild" BuildName="DefaultBuild" BuildOrder="1" BuildType="0" PackageFolder="C:\Build\Build.aip" PackageFileName="MyPackageName" Languages="en" InstallationType="4">
May you please give me some examples how I can do that? I seen in some forums that this can be done using FINDSTR.
Thanks in advance.
findstr "PackageFileName" Build.aip
If you want to make it case insensitive, add the /i argument.
For more details, type findstr /?
Updated for an example to use the for statement
FOR /F "token=2" %i in (`FINDSTR "PackageFileName" Build.aip`) do SET var=%i
A couple things to keep in mind here:
This version is based on Windows 8.1; it may work differently in different versions of Windows.
The token=2 is an example assuming that the word you are looking for is the second word in the line
If PackageFileName appears more than once in Build.aip, this code will break.
The findstring command is surrounded by back-ticks, not single quotes.
I haven't tested it; the SET may not actually survive the for loop. So test!
If you use it in a batch file, you must double all the % signs.