batch file with robcopy - batch-file

I use with robcopy in my batch file.
I want that if these batch file will run on OS without robcopy installed the error about it WILL appear in the loG file.the error about it is:
'robocopy' is not recognized as an internal or external command, operable program or batch file.
my code is:
robocopy Obl\BR "%WEBDIR%\BR" /E /LOG+:%TMPLog% >nul
but this error appear only on the console not appear in TMPLog.
maybe errors of this kind should not appear in logs?
if yes what I can to do?
maybe try and catch if it exist in batch file?!

I'm not at my Windows 7 PC at the moment so can't really verify this thing, but would logging the output of robocopy using the >> redirection be the same as logging with the /LOG+ switch? If so, you could redirect stderr (where the not recognized message is sent) to stdout, and stdout to your %TMPLog% file, like this:
robocopy Obl\BR "%WEBDIR%\BR" /E >>%TMPLog% 2>&1

Related

How to create a script that creates text files in every folder in a directory? (Windows Batch)

So basically, let's say I have the following folders in a directory:
test_folder_1
test_folder_2
And I also have a text file with the list of all the folders in that directory.
How can I create a script that will create a text file, test.txt, in all the folders in the directory? (test_folder_1 and test_folder_2)?
I tried modifying some code I found online and got this:
for /F "tokens=1 delims=," %%v IN (folderlist.txt) DO echo test>"C:\Users\myname\My Documents\test\"%%v""
However, running this, I get "Access Denied."
Any idea what went wrong, or alternative ways to do this? Thanks.
The command
echo test>"C:\Users\myname\My Documents\test\"%%v""
despite the wrong double quotes around %%v redirects the text test directly to the directory instead of a file test.txt in the directory. Or in other words: The attempt to create a file with name of an already existing directory is denied by Windows and the result is the access denied error message.
The solution for creating a file test.txt with the line test in each directory specified in text file folderlist.txt is:
for /F "tokens=1 delims=," %%v in (folderlist.txt) do echo test>"%USERPROFILE%\My Documents\test\%%v\test.txt"
It is also possible to create a file test.txt with a file size of 0 bytes in all subfolders of the test folder with following command line in the batch file.
for /D %%v in ("%USERPROFILE%\My Documents\test\*") do echo. >nul 2>"%%v\test.txt"
The command FOR returns in this case the subfolder name with full path always without surrounding double quotes even if the folder path contains a space character as it is the case here.
See How to create empty text file from a batch file? and read the Microsoft article about Using command redirection operators for more details.
echo. >nul 2>test.txt results in writing just a new line to STDOUT redirected to device NUL to avoid printing lots of blank lines on batch execution to screen. And to the file text.txt nothing is redirected from STDERR as there is no error message printed by command ECHO resulting in creating an empty test.txt file.
Run in a command prompt window for /? for help on command FOR.

Preventing output from commands in Batch

How do you prevent output in the CMD window? I know how to do it for these
#echo off
timeout /t 3 /nobreak >nul
MKDIR Files 2>nul
But I was wondering what the general way is for all commands, or how to find out for each command that way I don't have to keep asking. I'm trying to get XCopy to copy silently, that way the screen doesn't get spammed, but I have a feeling I'll need it for other commands too.
There are 2 output streams: stdout (standard output console) and stderr (standard error console).
With >nul or more correct 1>nul the text written to stdout is redirected to the null device.
With 2>nul the text written to stderr is redirected to the null device.
>nul and 2>nul can be used on any command and also both at the same time and should be always at end of the line. They can be also used at beginning of a line which makes sense in some special cases, but most often the redirecting operators are written at end of a line.
Most console commands and applications print a help when running with option /? (Windows) or -? (when ported from Unix). Open a command prompt window and try that with xcopy /?
Some commands like xcopy have special options to suppress output to stdout. The option for xcopy is /Q which is short for quiet. Using this option makes xcopy also faster a little bit as printing the output to stdout needs some time and is of course wasted time if the output is redirected to null device.
But take care of possible halt on errors when redirecting all output to null device. For example on using xcopy the options /C and /R should be used additionally to avoid a halt of the copy process on error or a read-only file in target directory with no information of the user who started the copy process why batch job does not finish because of using /Q and additional >nul 2>nul.
In batch files always >nul should be used and never 1>nul although that is often also possible. The command line interpreter replaces every >nul on execution of the batch file automatically by  1>nul (space + 1>nul). This should be taken into account when echo something into a file to avoid an often unwanted trailing space on each line redirected into a file.
See Microsoft article Using command redirection operators for even more information.
The "general way" is exactly what you're doing, which is to redirect standard output to nul using >nul. It should always be the very last part of each line.
(The mkdir example you're using redirects standard error (2>nul instead of standard output; I'm not sure why it would be doing that, but presumably you know why that's being done.)
The problem with doing so, though, is that you don't get any information about what worked and what failed, which makes it difficult to track down problems. When you need to not show the information on screen but want to make it available later if needed, redirect to a file instead.
REM Redirect first command to log, truncating anything
REM that existed in the file before.
mkdir Files > temp.log
REM Copy some files into the folder just created, appending
REM the results to the same log file
xcopy *.txt Files\ >> temp.log

Suppressing REN errors in Windows batch file

I'm testing my batch file which renames files based on tab-delimited text file pairs:
OldName [tab] NewName
I'm now being rude to my script and testing scenarios with duplicate name attempts, obviously getting error messages A duplicate file name exists, or the file cannot be found.
The question is: can I suppress them and only display a summary at the end of the script? I tried:
ren "%%H." "%%I." >nul
but to no avail.
>nul will write STDOUT ("Stream 1") to nirvana. "Normal" messages will use STDOUT. (>nul is a short form for 1>nul
Errormessages are written to STDERR ("Stream 2"). To suppress them, use 2>nul
If you want a summary, dont write it to nul, but to a file and type the file at the end of your script.
Use >> to append to a file, instead of overwriting it (>)
ren OldName.ext NewName.ext 2>>error.log
REM ... more of your script
echo done. Errors are:
type error.log

Unable to zip using 7z in batch file

Here i'm using batch file to take backups & then zip those backups.Backup process successfully.But zipping process failed and it always show to install the 7zip.But i already installed 7ip.
Here goes my code
#ECHO OFF
osql -S .\sqlexpress -E -i "C:\Sql_Backup.sql" -o "E:\test\testbk.sql"
ECHO.
7za a -tzip "E:\test\newfile.zip" "E:\test\newfile.Bak"
ECHO.
del "E:\test\newfile.Bak"
exit
What does "it always show to install 7zip" mean? Are you seeing an actual error message? Is the error message this?
'7za' is not recognized as an internal or external command,
operable program or batch file.
If so, that's because the program is 7z, not 7za. Just change it in your batch file and see if it starts working...
May be system couldn't find where your 7z.exe is, so just tell your system where your compressing program is by giving the correct path like below:
C:\Program Files\7-Zip\7z.exe a -tzip "E:\test\newfile.zip" "E:\test\newfile.Bak"

batch file to call an exe application

I have a script which I can run perfectly if I call it manually in the command prompt.
cd \
cd impressio
cd input
for %%f in (.txt)do (
echo "%%~nf"
"C:\Program Files\Splunk\bin\splunk cmd python" "D:impressio\deployment code\add_null.py" "%%~nf.txt" "%%~nf_processed.txt"
)
When I save this script as <filename>.bat and double-click, it always prompts me an error message: "Program Files is not recognized as an internal or external command".
Is it because of the environment variable setting issue? Did anyone come across such thing before, or does my code have a problem?
Not sure why Program Files is mentioned in the error message, but there does seem to be an issue with the line where you are calling your Python script.
In particular, this bit:
"C:\Program Files\Splunk\bin\splunk cmd python"
should likely be this instead:
"C:\Program Files\Splunk\bin\splunk" cmd python

Resources