Batch error: The input line is too long - windows

So I know there are lots of answers on this already, though it seemed like all of them basically suggested that the command should be broken up in some way. In my case I am not sure how this is possible... it is not even a long command. So does anyone have some suggestions on this?
:myLoop
call timeout 3600
cd c:\automation
call ant_env.bat
call ant -f regression.xml upgrade_sequence REM apparently this line is too long...
GOTO myLoop
I have even tried shortening the file name to just regr.xml.
This is on Windows Sever 2008 R2 Enterprise

This really depends on what you have inside ant_env.bat, you have to keep in mind that PATH is limited to 260 characters, in case you append it eg.
set PATH=%PATH%;%JAVA_HOME%
and the loop is called continuously, it can lead into a situation where the limit is reached.
You can use (if not defined) as can be seen here : "Input line is too long" error in BAT File

Related

Post-Build Event Works With one Project But Not the Other

I have 2 projects for which I am trying to create a generic Post-Build event batch file.
Here is the command in Visual Studio:
Post-Build event
if $(ConfigurationName) == Release ("$(ProjectDir)PostBuildRelease.bat" "$(TargetDir)" #(VersionNumber) "$(TargetFileName)" "$(TargetName)")
So I am calling the file PostBuildRelease.bat with 4 parameters:
Bin\Release Directory
Project Version
File Name With Extension
File Name Without Extension
Project 1
This works perfectly with this batch script:
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET "productionpath=Z:\Unused\Apps\LyncVdiChecker\"
MOVE %productionpath%%3 %productionpath%"_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 %productionpath%
Where the assembly is copied to Z:\Unused\Apps\LyncVdiChecker\ and the existing version copied to _archive in the same folder. The archived version also has the date and version number replace the file extension.
Project 2
This batch script also works perfectly (it does the same thing but in a different folder and for a different project):
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET "productionpath=Z:\Unused\Apps\IT Support App\"
MOVE "Z:\Unused\Apps\IT Support App\"%3 "Z:\Unused\Apps\IT Support App\_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 "Z:\Unused\Apps\IT Support App"
However, if I try using the same script from Project1 (the more generic version) in Project2, I get errors, even though the 2 scripts are equivalent:
Errors
The command "if Release == Release ("C:\Users\Seb.Kotze\Source\Repos\Applications\ITSelfHelp\ITHelp\PostBuildRelease.bat" "C:\Users\Seb.Kotze\Source\Repos\Applications\ITSelfHelp\ITHelp\bin\Release\" 2.0.6100.20905 "IT Self Help.exe" "IT Self Help")" exited with code 4.
Output Window:
The syntax of the command is incorrect.
Invalid number of parameters
This error is rather unhelpful, so I tried commenting out the 2 lines MOVE and XCOPY and build again:
Removed MOVE
Same error as above.
Output window:
Invalid number of parameters
Remove XCOPY
No Visual Studio Error, but this appears in the output window:
The syntax of the command is incorrect.
Parameter Output
When I echo out the parameters being used in Project2, everything seems to be in order:
"Path\to\Bin\Release"
2.0.6100.21082
"IT Self Help.exe"
"IT Self Help"
Z:\Unused\Apps\IT Support App\
How can I debug this issue? How is it possible that my script runs fine without any issues, but when run against a different project none of the commands are recognised? Any help with this is much appreciated!
You should normalize all your arguments, so they don't contain outer quotes.
Then you can use them in a reliable way.
The syntax set "variable=%~1" avoids outer quotes in the variable itself.
set "TargetDir=%~1"
set "VersionNumber=%~2"
set "TargetFileName=%~3"
set "TargetName=%~4"
SET "productionpath=Z:\IT Support App\"
set "dateStamp=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"
MOVE "Z:\IT App\%TargetFileName%" "Z:\IT App\_archive\%TargetName%.%dateStamp%-%VersionNumber%"
XCOPY "%TargetFileName%" "Z:\IT App"
The problem is that the script is messing with the double quotes resulting in invalid paths and invalid number of arguments passed. When dealing with paths built dynamically, it's best to strip any existing " from the parts, and after the path is complete, surround it in ".
Dealing with batch arguments is explained on MSDN. Same thing for variables can be found on SS64.
I've played a bit with the file, and I was able to run it (from command line). The changes you should make in your (Project1) file:
SET productionpath="Z:\Unused\Apps\LyncVdiChecker\"
MOVE "%productionpath:"=%%~3" "%productionpath:"=%_archive\%~4.%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%-%~2"
XCOPY "%~3" "%productionpath:"=%"
I moved the " from the productionpath line to the beginning of its contents. That way will work with paths that contain SPACE s.
In the MOVE and XCOPY lines, I did what I explained above: even if the syntax is not that clear, it's more robust (the last "%productionpath:"=%" could be simple written as %productionpath%, but I left it in the the 1st form for consistency).
Note: You could remove the CMD command at the beginning of your batch, since it starts a new cmd instance(process) that doesn't end.
I found a solution to this, but I am still not sure what the cause was.
I suspect it has something to do with either one of:
Spaces in productionpath causing the command parameter declaration to escape
Quotes around one or more of the parameters creating a non-existent file path
After trying out a few changes to the script, I found that changing the productionpath declaration to SET productionpath="Z:\Unused\Apps\IT Support App\" solved the issue:
CMD
SET parameter=%1 REM Full path to new bin\release\
SET parameter=%2 REM Full Version Number
SET parameter=%3 REM File name + extension
SET parameter=%4 REM File name - extension
SET productionpath="Z:\Unused\Apps\IT Support App\"
MOVE "Z:\Unused\Apps\IT Support App\"%3 "Z:\Unused\Apps\IT Support App\_archive\"%4"."%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"-"%2
XCOPY %3 "Z:\Unused\Apps\IT Support App"
Making the same change to the Project1 script did not cause that to break either, so this seems safe.
Update
After reading some of the other answers, I amended the script once again to the following:
CMD
SET "TargetDir=%~1"
SET "VersionNumber=%~2"
SET "TargetFileName=%~3"
SET "TargetName=%~4"
SET "ProductionPath=Z:\Unused\Apps\IT Support App\"
SET "ArchivePath=%ProductionPath%_archive\"
SET "DateStamp=%DATE:~0,2%%DATE:~3,2%%DATE:~6,4%"
MOVE "%ProductionPath%%TargetFileName%" "%ArchivePath%%TargetName%.%DateStamp%-%VersionNumber%"
XCOPY "%TargetFileName%" "%ProductionPath%"
Notice the "normalisation" of the paramaters - this removes all quotation marks from their values.
Also now using named parameters.

Windows batch file If/Else Start syntax?

I need to make a Windows batch file that will:
1.- Check a directory name and "If" it exists,
a.- Run a specified *.exe from a different directory.
2.- Or "Else"
a.- Rename a directory and also
b.- Rename another directory - then
c.- Run a *.exe from the created directory.
Question is: I'm stuck on the correct syntax ( I suppose) on creating this batch file. This is what I have, (maybe a nested if/Else would be better?) someone please enlighten me... Thanks.
#echo off
IF EXIST "C:\Test\Dir1" (
START "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "C:\Test\Dir1"
ren "C:\Test\Dir2" "C:\Test\Dir"
START "C:\Test\Dir\Test.exe"
)
You can't ren with a target name including a path or drive.
try
ren "C:\Test\Dir2" "Dir"
start has odd syntax - 'though it seems to not be the problem in this case. The first "quoted argument" string becomes the window title and can be routinely disregarded by the executable. Try
routinely using
start "window title that can be empty if you like" "executablename" argument list
Even if this arguably doesn't comply exactly with the documented behaviour.
(and simplifying problems to a generality can obscure the real cause, too)
Thanks dbenham and Magoo. Your responses made a world of difference on something that seems quite simple. It is now working as all the changes you both mentioned help. The final "nail in the coffin" was understanding the syntax for the START parameters. Understanding that the first parameter has double quotes and it uses that as the optional TITLE for the new window, ssoooo giving it an empty double quote ( "" ) gives it an empty title before the name of the program to fake it out. And Voila...
Thank you guys sssooo much. Final Working batch file.
#echo off
IF EXIST "C:\Test\Dir1" (
START "" "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "Dir1"
ren "C:\Test\Dir2" "Dir"
START "" "C:\Test\Dir\Test.exe"
)

Using a passed in parameter value to set PATH variables in a batch file

Right now I have a batch file that sets the PATH variable to all of the required directories.(there is actually a bunch more required directories, i just took them out so the code snippet would not be too long)
#echo off
set PATH=D:/src/trunk/build/bin;D:/src/trunk/build/bin/CoreTools;D:/src/trunk/build/bin/Plugins/Extensions;D:/src/trunk/build/bin/Plugins/CustomUI
set DEBRIEF_INSTALL_DIR=D:/src/trunk/DebriefSuite/D3D_Installation
set READERS=D:/src/trunk/build/bin/CoreTools/Readers
set BINARY_DIR=D:/src/trunk/build
cd D:/src/trunk/build/bin
start PROGRAM.exe --ConfigFile="D:/src/trunk/DebriefSuite/Installation/config/Projects/config.xml" ^
--Mode-File="D:/src/trunk/DebriefSuite/Installation/config/Projects/Common/anotherconfig.xml" ^
--Env:Bin="D:/src/trunk/build/bin"
cd D:/src/trunk
It works fine, but all of the directories are hard-coded. This needs to be able to work for other computers that might have their root directory in a different location. I need to be able to pass in a root directory (something like "D:\different_root_location") and substitute it in to each place in this code that currently says "D:\src\trunk". The problem is, i am not sure what the syntax would be for something like this. I am new to writing batch files. I tried doing something like
SET ROOTDIR=%1 .....
And then
set PATH=%ROOTDIR%/build/bin;%ROOTDIR%/build/bin/CoreTools;%ROOTDIR%/build/bin/Plugins/Extensions;%ROOTDIR%/build/bin/Plugins/CustomUI ..........
start PROGRAM.exe --ConfigFile="%ROOTDIR%/DebriefSuite/Installation/config/Projects/config.xml" ^
but it did not work. I'm not really sure how to make this work! Also, any links to good sources of information about writing batch files in general would be extremely helpful since i am starting out!
Change your line so it includes the original path as well.
From this:
set PATH=D:/src/trunk/build/bin...
to this: (and Windows uses \ and not / even though it works in some cases)
set PATH=%path%;D:\src\trunk\build\bin....

Capture CMD output with AutoHotkey

I'm trying to read Windows CMD's stdout with AutoHotkey. For example, I'd like to have the output of the setconsole command inside AHK stored in a variable. I already achieved it a while ago, which makes me all the more perplex why it's not working now.
In the AHK forums, there's a rather old thread about CMDret, a DLL based functionality to do exactly what I want. The first problem was to find a working download for it, since all the links in the post were dead. Google gave me another site, hosting v3.1.2. Altough there seems to be a newer one (v3.2.1 respectively 4d Beta), I checked it out and tested a simple example:
msgbox % CMDret(COMSPEC " /C set")
CMDret(CMD)
{
VarSetCapacity(StrOut, 10000)
RetVal := DllCall("cmdret.dll\RunReturn", "str", CMD, "str", StrOut)
Return, %StrOut%
}
Unfortunately, the MsgBox contained nothing. I then checked out RetVal which had a value of 0; and the attached readme says:
If the function fails, the return value is zero.
Further down, it says:
Note: only 32 bit console applications will currently work with the
this dll version of CMDret (v3.1.2 or lower). Calls that require
command.com will likely not produce any output and may crash. To avoid
this I have included a file named "cmdstub.exe" with the download (in
the Win9x folder). This file should be used when calling 16 bit
console applications to enable returning output.
In conclusion, I am not sure what the problem is. My machine is running on 64 bit. But is the corresponding clause in the readme supposed to solely exclude 16 bit systems or does it rather only include 32 bit?
If the computing architecture is probably not the problem, then what could be?
What I am looking for is either one of the following:
Can I fix the problem and keep using v3.1.2?
Has anyone a working source (or even a local copy) of a newer version I could check out?
Is there another approach [library, .ahk code, etc.] I could use for my purpose? (preferably similar, because CMDret seems very straightforward)
If you don't need a live output, you could use the cmd box itself to save a text file of itself and then you could have autohotkey detect when the console's PID finished (using the returned result of runwait and read the outputted file into memory.
So you could do this in your run command (just a regular cmd parameter):
ipconfig > myoutput.txt
exit
Now you have a text file with the ipconfig output in it.
OR
you could do the same thing, but instead of outputting to a text file, you could output to the clipboard, like this:
ipconfig | clip
Then, since the output is on the clipboard, you can easily grab it into autohotkey.
New recommended 2 ways of doing as of Nov 2019 - https://www.autohotkey.com/docs/FAQ.htm#output:
How can the output of a command line operation be retrieved?
Testing shows that due to file caching, a temporary file can be very fast for relatively small outputs. In fact, if the file is deleted immediately after use, it often does not actually get written to disk. For example:
RunWait %ComSpec% /c dir > C:\My Temp File.txt
FileRead, VarToContainContents, C:\My Temp File.txt
FileDelete, C:\My Temp File.txt
To avoid using a temporary file (especially if the output is large), consider using the
Shell.Exec() method as shown in the examples for the Run command.
Example for the Run command - https://www.autohotkey.com/docs/commands/Run.htm#StdOut:
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll()
}
Note: the latter method (shell.Exec) will cause quick display of a cmd window.
You can reduce the duration of its appearance by putting these lines at the top of your script, which will also cause the flickering to happen only once the first time you call the cmd command. From https://autohotkey.com/board/topic/92032-how-to-hide-a-wscript-comspec-window/:
;Following 2 lines : the cmd window will flash only once and quickly
DllCall("AllocConsole")
WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
How about this script, StdoutToVar ?
It has support for 64bit consoles.
http://www.autohotkey.com/board/topic/15455-stdouttovar/page-7
This has been bugging me for some time now - and finally this works !
The only prerequisite for this is MS sqlcmd.exe, a database called AHK_Dev
and of course AHK_DBA to read the value when you wish to make use of it.
PS. make sure you replace {yourDir} and {yourServer} with you own values!
USE AHK_DEV
CREATE TABLE [dbo].[AHK_DOS]([dos_out] [varchar](max) NULL) ON [PRIMARY];
insert into ahk_dos select 'empty'
Create the follow script ... call it dos_out.bat
#echo off
if "%1" == "" (
set v_cmd=""
) else (
set v_cmd=%1
)
set v_cmd=%v_cmd:~1,-1%
SETLOCAL ENABLEDELAYEDEXPANSION
if "!v_cmd!" == "" (
set v_cmd="echo ... %COMPUTERNAME% %USERNAME% %DATE% %TIME%"
set v_cmd=!v_cmd:~1,-1!
)
set v_data=""
FOR /F "usebackq delims=¬" %%i in (`!v_cmd!`) do (
set v_data="!v_data:~1,-1!%%i~"
)
set q_cmd="set nocount on;update ahk_dos set dos_out=N'!v_data:~1,-1!'"
"{yourDir}\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -S {yourServer} -E -d ahk_dev -Q !q_cmd! -W
set q_cmd="set nocount on;select len(dos_out) as out_len, dos_out from ahk_dos"
"{yourDir}\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -S {yourServer} -E -d ahk_dev -Q !q_cmd! -W -w 8000
pause
you can run it from AHK using...
dosCmd2db(c) {
runwait, {yourDir\}dos_out.bat "%c%", , , dospid
msgbox %dospid% closed
}
dosCmd2db("")
dosCmd2db("echo This is a test")
dosCmd2db("dir")
As the same field is being updated each time, you would clearly need to do something between each one to make this example useful!
Try it, and let me know how you get on
Regards, Geoff
Just an update to #amynbe answer.
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll() }
Note: the latter method (shell.Exec)
will cause quick display of a cmd window. You can reduce
> the duration of its appearance by putting these lines at the top of
> your script, which will also cause the flickering to happen only once
> the first time you call the cmd command.
You can just do this below to hide cmd and avoid flashing.
MsgBox % RunWaitOne("dir " A_ScriptDir)
RunWaitOne(command) {
DetectHiddenWindows On
Run %ComSpec%,, Hide, pid
WinWait ahk_pid %pid%
DllCall("AttachConsole", "UInt", pid)
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
DllCall( "FreeConsole" )
return exec.StdOut.ReadAll()
}
I found a script only solution that works for AutoHotKey L 64bit at:
http://www.autohotkey.com/board/topic/67687-ahkahk-lusing-wsh-to-interact-with-command-line-progs/
After playing with it a bit I was able to capthre the entire output of a 40k text file that I listed using the DOS Type command. There is a demo that shows how you can interact with time command, which is nice if you need limited two way interaction with a dos command or batch script.

windows batch sqlite backup firefox history

Not familiar with windows stuffs.. , I'm trying to write a little MS Windows batch in order to backup firefox history but I'm not getting the expected result, eg the firefox history dump into a file (not implemented here), and can't figure out why and how to solve. Instead I get a dump of the database in a new window. Here is what I've done till now :
cmd windows terminal
start "TEST" sqlite.cmd
sqlite.cmd
REM backup firefox history
setlocal
set DB_src=places.sqlite
set DB_dest=places1.sqlite
set FF_profile=C:\Documents and Settings\User_A\Application Data\Mozilla\Firefox\Profiles\1e6xxxxx.default
set SQLITE_EXE=C:\Documents and Settings\Admin_User\SoftWare\sqlite3.exe
set SQLITE_SQL=C:\Documents and Settings\Admin_User\Bureau\sqlite.sql
copy "%FF_profile%\%DB_src%" "%FF_profile%\%DB_dest%"
#echo off
start "%SQLITE_EXE%" "%FF_profile%\%DB_dest%" < "%SQLITE_SQL%"
endlocal
sqlite.sql
.dump html
.output moz_places.html
SELECT moz_places.visit_count, moz_places.url FROM moz_places ORDER by visit_count DESC LIMIT 20;
[EDIT]:
Worked around :
- using the right sqlite query (updated in sqlite.sql below)as for these examples.
- using the sql html output "moz_places.html" as I could not get the redirection work.
linux stuffs are easier for me...
This question is lame, because it involves three completely unrelated domains: firefox, sqlite, and batch files. You should have isolated the problem by determining whether this is a firefox issue or an sqlite issue or a batch file issue and then you should have come up with a question regarding the domain in which the issue lies, with absolutely no mention to the other domains.
I am going to give you an answer as best as I can regarding batch files:
First of all, you need to stop needlessly using the start command and just invoke things directly. So, instead of:
start "%SQLITE_EXE%" "%FF_profile%\%DB_dest%" < "%SQLITE_SQL%"
You need this:
"%SQLITE_EXE%" "%FF_profile%\%DB_dest%" < "%SQLITE_SQL%"
Then, you need to redirect the output of the above command into a file of your choice. For this, you need to make use of the '>' operator. So:
"%SQLITE_EXE%" "%FF_profile%\%DB_dest%" < "%SQLITE_SQL%" > myfile.txt
That should do it, as far as batch files are concerned. If it does not do it, then it is a firefox or sqlite issue.

Resources