How to override batch variable number of characters limit?

假装没事ソ 提交于 2019-12-23 09:56:28

问题


I'm having an issue on the current project I'm working on. I have to get a query stored from a file into a variable, whose file only contains a query (It is generated by part of the program). For instance I use the following code:

set /p query=<path\to\my\folder\fileContainingQuery.soql

It works when the query isn't that long, but when its length is beyond 1024 characters, the query is truncated and since I gotta send it to another configuration file using the FART tool, that's quite not convenient.

How to override this problem?


回答1:


There is a limit in how many data can be stored in a environment variable. In theory, the maximum length is 32767 characters, BUT a batch file cannot set a variable that is longer than the maximum command line length (8192).

You can use a for /f command to read the line

for /f "usebackq delims=" %%f in ("path\to\my\folder\fileContainingQuery.soql") do set "q=%%f"

Or you can generate a auxiliary batch file to assign the content of the query

<nul set/px=set q=>sql.set.cmd
type "path\to\my\folder\fileContainingQuery.soql">>sql.set.cmd
call sql.set.cmd

And both will get the maximum possible text into the variable ONLY if the final command line is not too big (8186 characters in my tests). In other case, nothing is retrieved.

BUT, once the value has been retrieved, is has to be used inside a command. And here the same limitation happens. 8192 is the maximum line length and the content of the variable IS inside the line.

So, your final limit for the length of the query is less than that.




回答2:


@MC ND: Thanks for your answer, I used the for /f command this way after thinking back at it. (Didn't read your answer at this moment).

(for /f "delims=" %%b in (path\to\my\folder\fileContainingQuery.soql) do (
    set query=%%b
))
echo %query%

My hypothesis for my previous problem is that the set /p var function may only accept 1024 characters on user input, and as my query file only contains one line which length could be greater than this limit, it truncates the query. The above code works fine for me and I get my complete query in the variable.



来源:https://stackoverflow.com/questions/21723275/how-to-override-batch-variable-number-of-characters-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!