How do I "echo" a command in a batch script? - batch-file

I'm trying to shutdown my PC using a batch script, but when I type "#echo shutdown /r", CMD just displays "shutdown /r" on the screen, instead of doing the command. I'm currently new to batch scripting, so forgive me on my knowledge on the command prompt and batch scripting.
Here is my code (I don't think this will help):
#echo off
title Shutdown.bat
color 0a
echo:
echo Your PC will shutdown (testing).
pause>nul
#echo shutdown /r
pause>nul

command echo text show you only text on command line. Use only shutdown /r for shutdown you pc.

Related

How to get the return code of a batch started with the PSEXEC command?

I use the PSEXEC command to start a batch file on a remote computer:
psexec \\remotemachine -s -d cmd.exe /c c:\test_dir\build_dummy.bat
The build_dummy.bat script:
#echo off
SETLOCAL EnableDelayedExpansion
>output_build_bummy.bat.log (
rem just print something into an output file
echo.
echo This is a dummy batch script
rem close the file output
)
EXIT /B -12345
I want that psexec returns the code -12345
However, I get just the process ID of the started cmd.exe.
How can I get the error code?
Error code of any command is stored in %errorlevel% variable. Simply type echo %errorlevel% and you will get it.
I discovered that if I omit the option -d in the psexec call then psexec returns exactly what I need - the exit code of my batch script
:)

Open command window via batch file when cmd.exe is blocked

cmd.exe is blocked at school. However batch files runs normally and sometimes it's very annoying to type the command in the batch file, write pause>nul and run the batch file to execute a command. Is there anyway to input commands from the user and execute them as cmd.exe does?
#echo off
:Loop
echo %cd%^>
set /p cmd=SkYWAGz Enter Command to Run (Press Ctrl + C to exit)
%cmd%
Goto Loop
Your very own command processor
I found the solution, the code in the batch file would be start
This works for me:
#echo off
break off
title Command Prompt
cls
:cmd
set /p cmd="%cd%>"
%cmd%
echo.
goto cmd

Batch script to wait for file to be generated on FTP server and download

I have a program in my FTP server to generate a file, which may take 3-5 minutes to complete and also I knew the name of the file which i being created by my program. Now, once I initiate the program in my server, I have keep checking until the file is created. Once it is created, I am using the below batch script to ftp the file to my local desktop.
#ftp -i -s:"%~f0"&GOTO:EOF
open 10.100.16.111
username
password
lcd c:\
cd root/output_folder
binary
mget "*partial_file_name*" REM mget using wildcard search
disconnect
bye
This script works fine for me. But the problem is, I need modify this script as such, script should keep running until the file is generated. Because i don't know when the file creation will get completed. So, it will great if some one help/guide me to make a looping script which will wait until the completion of file creation and download the same file through FTP.
With this edit you can launch the batch file with the file name on the command line, like this:
ftpscript.bat "filename.ext"
Note that your lcd uses c:\ which is a restricted location in later versions of windows.
#echo off
>file.tmp echo open 10.100.16.111
>>file.tmp echo username
>>file.tmp echo password
>>file.tmp echo lcd c:\
>>file.tmp echo cd root/output_folder
>>file.tmp echo binary
>>file.tmp echo mget "%~1"
>>file.tmp echo disconnect
>>file.tmp echo bye
:retry
ftp -i -s:"file.tmp"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
del file.tmp
pause
More elegant solution is to use an FTP client that supports parametrized scripts or commands on command-line, such as WinSCP, to avoid creating a temporary script file.
Parametrized script
The batch file would be more or less identical as with the Windows ftp:
#echo off
:retry
winscp.com /script=script.txt /parameter "%~1"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
The ftp script converts to following WinSCP script:
open ftp://username:password#10.100.16.111/
lcd c:\
cd root/output_folder
get -transfer=binary "%1%"
exit
Commands on command-line
You can also inline the commands the to the batch file:
#echo off
:retry
winscp.com /command ^
"open ftp://username:password#10.100.16.111/" ^
"lcd c:\" ^
"cd root/output_folder" ^
"get -transfer=binary ""%~1""" ^
"exit"
if not exist "%~1" timeout /t 300 & goto :retry
echo file has downloaded
pause
References:
Automating file transfers to FTP server;
Upload to multiple servers / Parametrized script.
Had you ever need to upgrade to the FTPS or the SFTP, just modify the session URL in the open command command accordingly.
(I'm the author of WinSCP)

Batch file leaves vbscript program open in Wordpad. Why?

My batch file contains a START command that runs a short vbscript program. When the batch file completes, the code of the vbscript program is shown in an open Wordpad window. This only started happening after we converted to Windows 7. Never happened under XP. Why does this happen and how can I prevent it? I have done extensive internet searching and come up with nothing.
Here is batch file:
#echo off
cls
echo.
echo Copying Latest Version of FREDS Database ...
echo.
xcopy "\\sstore02\S-Drive.OOD\OPI\FREDS\FREDS.mdb" "K:\FREDS\" /i /q /y
echo.
echo If you see "1 File(s) copied" then the copy was successful
echo.
echo Copying Shortcut Installer ...
echo.
xcopy "\\sstore02\S-Drive.OOD\OPI\FREDS\FREDS-Shortcut.vbs" "K:\FREDS\" /i /q /y
echo.
echo If you see "1 File(s) copied" then the copy was successful
echo.
echo Adding Shortcut icon to Desktop ...
echo.
Start K:\FREDS\FREDS-Shortcut.vbs
echo.
pause
You need to call cscript instat of start.
cscript /nologo K:\FREDS\FREDS-Shortcut.vbs
The option /noscript hides the version of cscript in the output.
Notepad is the registered program for the VBS extension on your machine.
Right click a VBS file and select Open With and then navigate to cscript.exe in c:\windows\system32 folder usually. Select the checkbox to always use that program and then the start command will work with VBS scripts and Cscript.

Start a CMD, then echo off of it, then clear the screen

Here are the steps I need, I am using a batch script
Open a cmd with path set to the desktop
Then set it's echo off
Then clear the screen
Then wait for me to type commands
So I can enter the commands directly without a long line blocking the view
Code I tried so far
#echo off
cd "C:\Documents and Settings\Administrator\Desktop"
cls
cmd
#echo off
cls
But when I run this I get a typical CMD window, where I have to type "echo off" & "cls" again to get a clean window.
The output I get is
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator\Desktop>
The output I want is simple
Imagine a blinking underscore
_
Thank you.
Try this line:
start /D "C:\Documents and Settings\Administrator\Desktop" cmd /k "prompt $"
/D <path> will set your working directory
Prompt $ will set your prompt to "nothing", only the blinking cursor will display
The output I want is simple
_
You can achive this with the prompt command, e.g. prompt $S or prompt $g
Here's an example batch file that would open up a new cmd window with an empty prompt:
#echo Off
start cmd /k "prompt $g"
create a VBS file using notepad
add this lines
Set cmd = CreateObject("WScript.Shell")
cmd.run"cmd"
WScript.Sleep 200
cmd.Sendkeys"echo off{Enter}"
WScript.Sleep 200
cmd.Sendkeys"cls{Enter}"
WScript.Sleep 200
This command can be very problematic (cmd /c "echo off | clip")
USE WITHOUT QUOTES:
cmd /c echo off | clip
goto off
(Save as .bat)
No more problems !!!
I just used
cls
echo off
cls
cmd|echo off
cls
in a batch file. Works for me

Resources