Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override batch variable number of characters limit?

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?

like image 957
Péqueño Avatar asked Feb 12 '14 09:02

Péqueño


People also ask

What does %% mean in batch script?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt.

How many parameters can I pass to batch file?

There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).


2 Answers

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.

like image 186
MC ND Avatar answered Oct 15 '22 23:10

MC ND


@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.

like image 35
Péqueño Avatar answered Oct 15 '22 22:10

Péqueño