Lua programming - os.execute() is not working in Windows - windows

I'm creating a function in pure-Lua to scan the files from a directory and put they on a another file.
The command I tryed was:
os.execute( "dir /B C:\\Users\\Fernando\\workspace\\Organizator2\\s1 >
C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt" )
but... dont works! I did many tests with others simpler commands, like "start notepad" or "mkdir C:\test", and they dont worked too! The worse part is that I tryed this same commands directly in the Prompt, and there is all correct.
I tryed use tooo the io.popen(), but it the system returned "illegal operation" for any command i passed (even a empty string!).
here is the all code:
function ScanDirectory(source, str)
local str = str or "temp.txt"
os.execute("dir /B "..source.." > "..str)
directory = io.open(str,"r")
return directory
end
-- main script
do
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\
\workspace\\Organizator2\\temp.txt")
end
I'm using windows 7 and the Luaforwindows, 5.1, and the LuaEclipse
Have someone ever seen a problem like this?

Please try it with this syntax:
os.execute [["dir /B C:\Users\Fernando\workspace\Organizator2\s1 >
C:\Users\Fernando\workspace\Organizator2\temp.txt"]]
Please note that the backslash (\) is not a special character in this case.
(Lua uses cstrings internally, sometimes it leads to some weird and amazing results :P)

Most of the commands you listed appear to be shell commands that only work within a command prompt. Try running cmd.exe directly to see if you get a prompt, and if so, you can try passing commands to cmd.exe via the /c option. You could also try notepad without the start to see if that runs.

os.execute('cmd.exe /c dir /B C:\\> C:\\test.txt')
That works. Useing Linux-style commands in win is a bad idea at all =)

I just tested your code on my computer and it works correct (with my directories, of course). Maybe you are not getting the expected result because your directory string is broken with an newline char, resulting in:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\
workspace\Organizator2\temp.txt
The correct should be:
dir /B C:\Users\Fernando\workspace\Organizator2\s1 > C:\Users\Fernando\workspace\Organizator2\temp.txt
Please try changing the do end to:
local source = "C:\\Users\\Fernando\\workspace\\Organizator2\\s1"
local directory = ScanDirectory(source, "C:\\Users\\Fernando\\workspace\\Organizator2\\temp.txt")

Related

short path of current directory in cmd windows

I wish there's a way to show a shortened directory path in windows CMD, I searched the internet and found this command:
for %I in (.) do %~nxI
if you enter this command in cmd and press enter it would show the current dir name. suppose I was standing in my desktop it will print:
Desktop
thats enough for me. but not yet, I tried to save this command in a environment system variable then pass that variable to cmd line, but this way it would print the command itself instead of the result.
created a variable named PROMPT then value is:
%username%$s$p$s$d$t$_-$g$s
result is:
my_username full_path_to_current_dir date time newline dash greater_than_mark(>)
Mahdi c:\Users\Mahdi\Desktop 23/02/2020 19:27:38.93
->this is an screenshot of what Ihave: (https://i.stack.imgur.com/Auicf.png)
This is not a feature avaible in the standard prompt.
You potentially could hack it using echo off and doskeys,
but I would instead suggest checking out one of the many alternatives such as cmder or Zoc
Is this what you wanted?
For /F "Tokens=1*Delims=|" %I In (""%UserName%"|"%CD%"") Do #Prompt %~I$s%~nxJ$s$d$t$_-$g$s
Alternatively, if you wanted it setting to a variable, run the prompt command inside another for loop, and capture the output of that with the Set command.

Redirecting a command to an .exe causing a repeat in the command

I have an exe program that uses a command line GUI and would like to set up a batch script to run a few commands automatically, so here are the sort of commands that the GUI uses:
? = help
s = start
l = status
r = reset
x = exit
I would like to create a batch file that automatically runs the start command I have tried a few things to no avail such as the below.
using a cannedreponsesfile as the input:
#echo off
START /b <path>\service.exe < <path>\cannedreponses.txt
And I receive the following error:
The system cannot find the path specified.
The path referenced in the batch script seems to be ok I checked with dir /s /b
I have also attempted this command
echo s |START /b service.exe
This seems to have better results, but it seems to keep repeating input.
Any idea what is going on, or how I can do this in a batch file.
Thanks in advance
You appear to have overcensored your question.
In all probability, the problem is with <path>
If the real string you are using contains separators (especially spaces) or some other special characters, then you need "enclose the string in quotes".
There's a quirk with start however - the very first string quoted encountred is interpreted as a "window title" so you may need to use
start /b "" "whatever 1" "whatever 2"
The start is unnecessary here and only introduces a source for errors. Just say
Echo s|service.exe

Why do all Pre-build or Post-build events in Visual Studio fail with exit code 1?

My goal is copying an .exe file from a bin folder of solution A to another solutions B resource folder on the post build event of solution A.
I created the necessary xcopy command and tried it out in powershell: It works perfectly.
Whenever I add any command to the actions in VS build fails with: "#command# exited with code 1", where #command# is, for example, the xcopy command.
I tried running VS as admin and currently I tried just running a .bat file that contains "#echo off #exit 0". That too leads to "#command# exited with code 1".
Have some example of what I tried out as VS post/pre-build command:
call "projdir\test.bat"
call projdir\test.bat
"projdir\test.bat"
projdir\test.bat... I tried projdir as "$(ProjectPath)" and manual path.
I put output to verbose and found the following:The command "C:\Users\Traubenfuchs\AppData\Local\Temp" is written incorrectly or couldn't be found. (That folder actually exists but I don't know what it wants to do it.)The same thing happens when I put an xcopy command in pre/post build.
Anyone knows what I am doing wrong?
Recently I met similar problem.
About your "hello world" post-build event :
try to call powershell directly
powershell -command "write-output 'hello world'; exit 0"
=================
My pre-build event looks like :
powershell -file scriptPath.ps1
My scriptPath.ps1 looks like :
<my epic powershell code>
...
exit 0
Note that without "exit 0" at the end I recieved return code 1 from my script.
We had this issue because of a "Software Restriction Policy" set up in a domain GPO. It appears that pre and post builds create a .cmd batch file in the temp folder. Here's what the logging showed me:
cmd.exe (PID = 8456) identified
C:\Users\brian\AppData\Local\Temp\tmp733425d2c0604973a90a0a175c13353e.exec.cmd
as Disallowed using default rule, Guid =
{11015445-d282-4f86-96a2-9e485f593302}
To fix this we modified the GPO that controlled the SRP so that it did not include .cmd files. After all, the goal of SRP is to block executable malware, not batch files. I may get some security blowback because of this. Hopefully someone knows a better way to fix this issue.
One possible issue is that you have to use $(ProjectDir) instead of $(ProjectPath)
When you use $(ProjectPath) it is actually: "C:\Users\....\Project\MyProject.csproj"
Versus $(ProjectDir) which is: "C:\Users\....\Project\"
Notice, that the first version is actually pointing to your project solution file... which would cause everything to fail.
Another is that that $(ProjectDir) automatically adds the trailing slash to the path. i.e. "$(ProjectDir)\test.bat" would actually translate to: "C:\Users\....\Project\\test.bat"
Also you have to make sure that you enclose all your file paths in double quotes
So the correct call would look like this:
call "$(ProjectDir)test.bat"
Other things to check out would be to make sure that the directory that the script is executing from is correct. See SO: visual studio 2012, postbuild event, bat file not creating new file (not executing)
Have you checked out the suggestions in this StackOverflow? Post Build exited with code 1
Edit
Try this:
echo Hello World
#exit 0
You need to end any commands with #exit 0 otherwise it doesn't think that it finished properly
Edit 2
Why do we use #exit 0 instead of exit 0? #Sunny has a good explanation of the At symbol - # here:
The # symbol tells the command processor to be less verbose; to only
show the output of the command without showing it being executed or
any prompts associated with the execution. When used it is prepended
to the beginning of the command, it is not necessary to leave a space
between the "#" and the command.
Essentially if you call xyz.bat by default every command in the .bat file is echoed back along with the actual output of the command. e.g.:
test.bat
echo Hello World
exit 0
Will output:
C:\>call test.bat
C:\>echo Hello World
Hello World
C:\>exit 0
C:\>
When we add a # in front of the commands, we only get the output of the commands themselves.
test2.bat
#echo Hello World
#exit 0
Will output:
C:\>call c.bat
Hello World
C:\>
#echo off is also a way to turn this off for the rest of the script. We typically do this simply to make the logs easier to read, as the text of the commands muddies the log output.

Windows command line using directory and EXE file parameters

I have been trying to run an application I built and output it to a file. However, I am running into problems with the command line arguments required to do this.
This is an example of my problem using ipconfig.
The following command works:
ipconfig > output.txt
Whereas this will create the file, but not populate it with the ipconfig output:
start /D "C:\>WINDOWS\system32" ipconfig.exe > output.txt
I think it is the use of start that is causing this issue, but I'm not sure.
SOLUTION
This is the code which managed to solve the problem for me:
char path[500]; // Create character array
strcpy (path, "cd "); // Copy 'cd' into the array
strcat (path, toolLocation); // Copy the path of the tool into the array
strcat (path, " & ip.exe > output.txt"); // Append on the name of the exe and output to a file
system (path); // Run the built array
I am creating a character array and then appending to it. The vital bit here was the & being used in the system call. This is working as an and and first cd'ing to the directory before executing the .exe file.
In your command, the > is redirecting the output of start rather than the output of ipconfig. That explains why you are seeing nothing – start is simply not outputting anything.
Based on the comments to the question, you can achieve your goals with ShellExecute like this:
ShellExecute(
0,
"open",
"cmd.exe",
"/C ipconfig > output.txt",
NULL,
SW_HIDE
);
Rather than using start I think you might want to use cd to change the directory.
Try this batch file:
cd "C:\Program Files\Tools\2012"
ip.exe >output.txt
Or for use without a batch and just command line:
"C:\Program Files\Tools\2012" ip.exe >output.txt"
Although system32 is in PATH so I'm not sure why you are accessing the ipconfig exe by it's full path, but this should work.
The error is this:
start /D "C:\>WINDOWS\system32" ipconfig.exe > output.txt
should be
start /D "C:\WINDOWS\system32" ipconfig.exe > output.txt
without > in the path. Although C:\> is shown at the prompt with cmd.exe it is not part of the path name and > is actually invalid for the purpose, to my knowledge.
Additionally I would strongly suggest you use:
start /D "%SystemRoot%\system32" ipconfig.exe > output.txt
Furthermore because start creates a new console (and new stderr and stdout) you are catching the output of start not of ipconfig. So you may wanna use:
pushd "%SystemRoot%\system32" & ipconfig.exe > output.txt & popd
but that will attempt to write output.txt into %SystemRoot%\system32 and will fail on most systems unless you are admin. So give an absolute path or simply leave out the crud:
ipconfig.exe > output.txt
ipconfig.exe is always in the default system PATH variable, so it will work unless the admin has "fixed" the system in which case you can still do:
%SystemRoot%\system32\ipconfig.exe > output.txt

Execute windows cmd command on all files in directory

Very simply, I want to execute a command on every file in a directory, one at a time, and log the output of that command.
I wrote the following batch script:
pushd C:\Program Files\Log Parser 2.2
for %%x in (C:\templogs\3\C\IISLogFiles\W3SVC3\deduped*.log) do (
LogParser "SELECT * INTO weblog FROM %%~dpnx" -i:W3C -o:SQL -server:localhost -database:testdb -driver:"SQL Server" -createTable:OFF >> input.out
)
popd
When I execute it in the command prompt, all it does it copy paste those commands exactly as is. Nothing executes. any idea?
The only situation when that would be the case is when:
dir C:\templogs\3\C\IISLogFiles\W3SVC3\deduped*.log
... would come back blank. Could you please double check that this path actually contains files you want to process...?
I've tried it several ways and always got expected result (LogParser is not recognized in my case), unless I've tried on the path that did not existed on my system...
How about just executing LogParser as:
LogParser "SELECT * INTO weblog FROM C:\templogs\3\C\IISLogFiles\W3SVC3\deduped*.log" -i:W3C -o:SQL -server:localhost -database:testdb -driver:"SQL Server" -createTable:OFF

Resources