Execute windows cmd command on all files in directory - windows

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

Related

Batch file flashing command prompt in endless loop after being executed

Background
I want to use ROBOCOPY to backup folders. To learn this, I created a test source folder, containing other subfolders and dummy files.
F:\RoboCopy\RoboCopy_Files
I am able to ROBOCOPY the source folder from the Command line and PowerShell (with using Windows 10).
ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR
It does exactly what I want.
Now I put the command into batch file Robocopy.cmd.
Problem symptoms
However, when I put the same command into Robocopy.cmd file, in the root F:\RoboCopy folder and run it, I get only flashing cmd window with my command repeated on ever increasing number of lines.
How can I put the command into a CMD file (e.g. Robocopy.cmd) for later use/share/schedule? How to prevent command prompt from flashing in endless loop without running the command?
Note: I feel this is more about learning how to put cmd scripts into files, than how to use ROBOCOPY.
Cause: File and Command have the same name
I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is a mistake.
The file ends up calling itself.
Solution 1: Rename batch file
One solution is to rename the batch file to be different from the command(s) in the file.
Solution 2: Use more explicit command call
Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\robocopy.exe. This would prevent the "confusion" in calling the command vs calling the batch file itself.
Solution 1 x 2
The best solution (thx Mofi again) is to combine the 1 x 2 together. Use unique batch file name AND specify the full path to command (exe) inside the batch file.
Useful related commands: To determine the full path to command (exe), see the WHERE command (e.g. Where Robocopy.exe - This should be somewhere in windows folder.), system variables (e.g. SS64), or the command SET.
The full version in my case would be to run for example BackupRobocopyFiles.cmd with line:
%SystemRoot%\System32\robocopy.exe "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR /R:2
Note: This would work only if the cmd file is in the root folder F:\RoboCopy. If I would like to run the cmd from different folder (or task sheduler), I would specify the full path to SOURCE_FOLDER and DESTINATION_FOLDER parameters of ROBOCOPY command.
Contributions
The answer was found based on comments by: "MC ND", "eryksun". "Mofi" did point out, that the original Q was not helpful. Thanks to all.

executing an exe using sandboxie with parameters

I have an exe written in cpp, which I would like to execute in a sandbox called Sandboxie.
The exe file is requesting for input (using cin command in cpp), the only way I found passing parameters not within argv but for cin is with echo command.
the output of the exe file will go to txt file.
basically, if my program asks for one input, the run command looks like this :
start "D:\Program Files\Sandboxie\start.exe" /B (echo 3) | exetorun.exe > result.txt
The problem I found here is that it just doesn`t work,
so the solution I found is taking the part of parameters pass and exe name to run and inserting it into a batch file (.bat) and running it from the sandbox.
like that :
start "D:\Program Files\Sandboxie\start.exe" /B "runExec.bat"
and runExec.bat will have :
( echo 3 ) | exetorun.exe > output.txt
The problem now is that I have the basic version of Sandboxie which does not let you define a folder for example that no matter what when you try to run a file from there - it will run it from the sandbox... so basically all my points of sandbox is ruined.
Moreover, the idea of making a batch file for that is pretty stupid I think...
The only solution I find that will be the pretties is finding a way to just run the command of executing the wanted file and passing the wanted paramter to it within the first command instead of creating a batch file that will execute those commands.
Does someone know a way in any aspect (another sandbox, how to run the command not from a batch file) that can solve this ?
Thanks !

Run a batch file from Task Scheduler is not working with a java command

Run a batch file from Task Scheduler is not working with a java command inside the .bat file. If I run the .bat file manually its working good.
Here is the simple .bat file I'm trying to schedule
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_24;
set CMD= "%JAVA_HOME%\bin\java" -version
echo %CMD%
%CMD%
When you type batchfile.bat on the command line, you are telling cmd.exe to read the file and execute each line it finds in it. When you double-click on your batch file in explorer, it calls cmd.exe for you, after reading the file associations in the registry.
Task Manager is not so kind.
So for your task to work, schedule it like this (from memory, not on a Windows box right now) :
cmd /c "c:\full\path\to\your\batchfile.bat"
For extra robustness, you could make sure you batch file run from a known directory, like the one that it reside in, by adding this at the top:
pushd %~dp0
REM .... The original batch file goes here ....
popd
And finally you could disable CMD autorun entry by adding /d right after cmd like this:
cmd /d /c "c:\full\path\to\your\batchfile.bat"
If ixe013's suggestion doesnt work go to
'Actions'
'Edit' the task
'Start in (optional):' Put the path to the directory where the script is
So for the last one if you have 'C:\Users\Desktop\script.py' just put in 'C:\Users\Desktop\' in the 'Start in (optional):' field
What worked for me was running the task as "Users" ( computername\Users ). Once I did that, and "run with highest privileges" checked, it ran without a hitch.
Giving the full path of java.exe in the batch file fixed it for me. In a notepad, I typed the following line:
"C:\Program Files\Java\jdk1.8.0_40\bin\java.exe" -jar "C:\Users\usernameXXXX\Documents\NetBeansProjects\JavaApplication5\dist\JavaApplication5.jar"
Save this as a app1.bat file (C:\temp\app1.bat)
In the Actions tab of the task scheduler, give the path to the batch file, i.e, C:\temp\app1.bat
Also, be careful in the Conditions tab of task scheduler- make sure you uncheck "Start the task only if the computer is on AC power"
All other ways did not work for me, I followed this guide:
http://richardstk.com/2012/06/15/scheduled-task-to-run-a-batch-file/#comment-6873
In order to get the batch file to run, I had to set the "Program\script" box to contain just the name of the script (ie. script.bat) and set the the folder path of the script in the "Start in (optional)" box
I gave full permission to user Everyone from security tab from Properties of the folder in which batch file is. and it started working.
What a coworker discovered on something he had that wasn't working, and I have verified on the system I had that wasn't working is the following:
When the whole task is initially setup, you HAVE TO initially use the radio button "Run only when user is logged on". It will ask for your password for the change.
Now run the task.
Verify that whatever the batch was supposed to do, did happen.
And THEN change to the radio button BACK TO 'Run whether user is logged on or not."
This solved a problem for both of us that we had individually been working on for hours.
Side notes: both issues were also trying to elicit a 3rd party FTP app (WinSCP and WinFTP respectively) in each of our cases. Regular "inhouse" batch/tasks were having no issues.
I had the same problem, and to solve it, I put the next command line into the batch file:
cd "CURRENT_DIRECTORY"
where CURRENT_DIRECTORY is the directory where the batch file is located.
Example:
Suppose i have my batch file named test.bat located into c:\windows\system32\mytest
in my test.bat file, i introduce the next command line:
cd c:\windows\system32\mytest

Lua programming - os.execute() is not working in 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")

SQL Plus change current directory

How does one change the current directory in SQL Plus under windows.
I am trying to write a script with several "# filename" commands.
I know that one can open a script with the File --> Open command, which will change the current directory, but I am looking for a way to do this automatically unattended.
Resolution
Based on Plasmer's response, I set the SQLPATH environment variable in Windows, and got something that's good enough for me. I did not try to set it with the HOST command (I doubt that it will work).
Pourquoi Litytestdata's answer is a good one, but will not work for me (the directories are too far apart). And of course Guy's answer that it cannot be done is also correct. I will vote these two up, and accept Plasmer's answer.
Here is what I do.
Define a variable to help you out:
define dir=C:\MySYSTEM\PTR190\Tests\Test1
#&dir\myTest1.sql
You can't cd in SQL*Plus (you can cd using the host command, but since it is a child process, the setting won't persist in your parent process).
I don't think that you can change the directory in SQL*Plus.
Instead of changing directory, you can use ##filename, which reads in another script whose location is relative to the directory the current script is running in. For example, if you have two scripts
C:\Foo\Bar\script1.sql
C:\Foo\Bar\Baz\script2.sql
then script1.sql can run script2.sql if it contains the line
##Baz\script2.sql
See this for more info about ##.
Could you use the SQLPATH environment variable to tell sqlplus where to look for the scripts you are trying to run? I believe you could use HOST to set SQLPATH in the script too.
There could potentially be problems if two scripts have the same name and both directories are in the SQLPATH.
I don't think you can!
/home/export/user1 $ sqlplus /
> #script1.sql
> HOST CD /home/export/user2
> #script2.sql
script2.sql has to be in /home/export/user1.
You either use the full path, or exit the script and start sqlplus again from the right directory.
#!/bin/bash
oraenv .
cd /home/export/user1
sqlplus / #script1.sql
cd /home/export/user2
sqlplus / #script2.sql
(something like that - doing this from memory!)
With Oracle's new SQLcl there is a cd command now and accompanying pwd.
SQLcl can be downloaded here: http://www.oracle.com/technetwork/developer-tools/sqlcl/overview/index.html
Here's a quick example:
SQL>pwd
/Users/klrice/
NOT_SAFE>!ls *.sql
db_awr.sql emp.sql img.sql jeff.sql orclcode.sql test.sql
db_info.sql fn.sql iot.sql login.sql rmoug.sql
SQL>cd sql
SQL>!ls *.sql
003.sql demo_worksheet_name.sql poll_so_stats.sql
1.sql dual.sql print_updates.sql
SQL>
Have you tried creating a windows shortcut for sql plus and set the working directory?
I think that the SQLPATH environment variable is the best way for this - if you have multiple paths, enter them separated by semi-colons (;). Keep in mind that if there are script files named the same in among the directories, the first one encountered (by order the paths are entered) will be executed, the second one will be ignored.
Years later i had the same problem. My solution is the creation of a temporary batchfile and another instance of sqlplus:
In first SQL-Script:
:
set echo off
spool sqlsub_tmp.bat
prompt cd /D D:\some\dir
prompt sqlplus user/passwd#tnsname #second_script.sql
spool off
host sqlsub_tmp.bat
host del sqlsub_tmp.bat
:
Note that "second_script.sql" needs an "exit" statement at end if you want to return to the first one..
for me shelling-out does the job because it gives you possibility to run [a|any] command on the shell:
http://www.dba-oracle.com/t_display_current_directory_sqlplus.htm
in short see the current directory:
!pwd
change it
!cd /path/you/want

Resources