I am trying to run this .bat script
set target_path =\\remoteserver\c$\newfolder\newfolder1\logs >log.txt
findstr /m "ERROR" "%target_path%\file1.txt" >> log.txt
The expected output from this .bat file is that it will pick up "ERROR" string from file1.txt from the "remoteserver" and will show as output in log.txt.
But,once when i run this script it shows me the following error
FINDSTR: Cannot open \remoteserver\c$\newfolder ...
Kindly, suggest me the flaws or correct script so that the above script gets executed successfully with the expected output .
thanks for your quick update and sorry for acknowledging late. I tried executing the batch script with the necessary corrections you mentioned needed in the script , but still i face the same problem . I am unable to fetch the "ERROR" from log file and get the expected output. Is there any other field to be added to "FINDSTR" to pull up the ERROR ??.. Kindly, assist me for the same.
Thanx in advance.
Not at all sure how you achieved that response given the scant 2 lines of code you've posted.
You are setting the variable "target_path ", not "target_path" according to your first line. Batch is sensitive to spaces in a set statement.
Since "target_path" is not set, it's a total mystery how your response is Cannot open \remoteserver\c$\newfolder ... unless the variable "target_path" had been previously set in the environment. The findstr should, with the posted code, have been executed against the file "\file1.txt" - not as reported.
Related
I've a strange problem that I have never seen before in my ~30 years of working with batch files. Given a dummy CMD file such as this:
#echo off
:somelabel
echo Testing something
dir /b
echo All is well.
:end
This mostly runs as expected, but sometimes I get output such as 'ing something' is not recognized as an internal or external command, operable program or batch file.
That's clearly an occurrence where it has chopped off a bit of a line and attempted to execute the rest of it. When this happens, it's always a 'random' fraction of a 'random' line; it's not always line X, or losing Y characters, or occurring where I have a particular character combination. Nor does it affect only echo statements, it could just as well try to execute abel or ir /b.
My system is a fully updated Win2008 R2, running in VirtualBox 5.0.2, running in a fully upgraded Linux Mint, running on a Lenovo ThinkPad. The scripts are all UTF-8 encoded.
... what's going on? How can I avoid this?
Well, in my experience there is no way that a normal Batch file may present this behavior. The only way this can happen is if the Batch file is modified while it is running, so the cmd.exe processor continue reading the "next line" of the Batch file, but at that position in the modified file there is a part of another line. The example below show this behavior:
#echo off
:somelabel
(for /F "skip=1 delims=" %%a in (%~NX0) do echo %%a) > temp.tmp
del "%~NX0" & ren temp.tmp "%~NX0"
echo Testing something
echo All is well.
:end
In this example after the del ... & ren ... line is executed, the next line to execute will be read at the beginning of original position of echo Testing something line, but now at that point there is its ing something part because the first #echo off line was deleted. See this:
#echo offRL
echo Testing something
In previous scheme the RL letters exemplify the CR+LF control characters, so the next position is at the "ing".
Credit goes to #SomethingDark for the comment:
The command line doesn't play nice with UTF-8. Use ANSI instead.
This seems to have resolved the issue (as far as I can be sure with an intermittent problem).
I know this is a couple years late but I actually found the most accurate answer.
I actually did some testing on why it could register only partial code and got the answer. If you are editing the code and run batch while still open, it has a partial chance of only registering partial code. This is actually correct, nothing with encoding although that might be part of it except still does it to me with ANSI but with some testing, I realize that isn't it and that it is because batch cannot catch up with you when editing the code.
I did type a comment on the answer marked as best and said what I think but after further testing on the question I realized the pattern is when editing the code and running the batch file while still open, almost right away it seems to only read partial code.
When you said it seems to work when changing encoding to ANSI, did you just run it or did you edit it and run it at the same time. The main reason I did want to answer this is because my encoding method on a batch file I am working on is in ANSI and still causes the error of partially reading the code. Once I read Aacini answer, it gave me a thought of it and that he is partially correct to KlaymenDK situation.
Do prevent the error of partially reading the code modify and wait a bit than run and if that doesn't work than restart it. If you are planning on publicly posting it, you don't really have to worry about the problem because it would most likely only occur if you edit the code while running it. Nothing that could really be a problem with your code as I know.
Recently I run into the same problem, although the cause might be different.
In my case there was an REM command with text which have special characters in other languange. After I erased the REM command, the problem was solved.
For disclosure the problem did not happened in all computers the code was executed, and still dont know why will special characters cause the batch file to execute partial lines.
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
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.
When I execute the following batch file
#echo off
echo Text from echo
xcopy foobarium
I get the following output:
File not found - foobarium
0 File(s) copied
Text from echo
Obiously the order of the outputs is swapped. How can I ensure that the output appears in the order the commands are specified in?
(This is on Win7)
It seems that this is a bug in clink (a utility that provides bash-like auto-completion for cmd.exe). I've filed a bug report.
I have a batch script setup to automatically retrieve a file from a remote FTP server. Part of the requirement is the file will be named with a new datestamp each day, such as "File_90611.csv." I have a command line tool that generates the filename; which is then supposed to be set to a variable using the line below:
for /f "delims=" %a in ('C:\BIN\YesterdayDateStamp.exe') do #set DATESTAMP=%a
The problem is this. This line works fine when run from the command line directly. However, when I put this exact same line in a batch script and run it; I get this error:
\BIN\YesterdayDateStamp.exe') was unexpected at this time.
I REM'ed everything out in the script except the FOR ... IN commands to make sure there wasn't some sort of conflict; but even with this I still get an error.
Been Googling for an answer but have no leads. Any ideas? Any constructive input is greatly appreciated.
Thanks,
Frank
When for is used in a batch file, you need to double the percent signs in front of the variable name.
From for /?:
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.