i need help understanding this batch script - windows

i dont under stand the
forfiles -p
or the
/C "cmd /c del #path"
what do they do they mean

Basically it does the following.
For each files in c:\test that contains .jpg format and are 0 days old or older will be deleted.
Commands explained...
Forfiles -p : Select a file (or set of files) and execute a command on each file. -p: The Path to search (default=current folder)
/C command : The command to execute for each file.
Wrap the command string in double quotes.
Default = "cmd /c echo #file"
CMD /c : Start a new CMD shell and run Command and then terminate
Del : Delete
#path : Full path of the file.
More details at http://ss64.com/

Related

Execute cmd.exe command from Powershell and pass parameters

I'm creating PS script to remove a bunch of applications from lots of devices.
The theory with the code below being it passes the path of an application to cmd.exe along with the "RD" (remove directory) command and two switches.
However all i get when running this is..
"'rd C:\Program Files\Mozilla Firefox /s /q' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the
path is correct and try again.
Any suggestions please as to how accomplish my goal here
if ($app_path) {
write-host "Now Removing $appname ,install directory ($app_path)"
$command = "rd $app_path /s /q"
& cmd.exe /c $command
}
The arguments need to be separate strings.
& cmd /c rd $app_path /s /q
or
$command = 'rd',$app_path,'/s','/q'
& cmd /c $command
or
cmd /c rd $app_path /s /q

Batch script does not close cmd after execution

I have the below code which starts the siebel tools from a batch script:
#ECHO OFF
D:
CD D:\Siebel\8.1.1.16.0\Tools\BIN\
ECHO Starting Siebel Tools
siebdev.exe /c D:\Siebel\8.1.1.16.0\Tools\CFG\tools.cfg /d DataSrc /u ADMIN /p PASSWORD
cmd
But this does not close the command prompt. Please help.
this alone should be fine, without cmd in the end line
#ECHO OFF
CD /d D:\Siebel\8.1.1.16.0\Tools\BIN\
ECHO Starting Siebel Tools
siebdev.exe /c D:\Siebel\8.1.1.16.0\Tools\CFG\tools.cfg /d DataSrc /u ADMIN /p PASSWORD
just to add,
/d can be used after cd instead of D: and then using cd

Difference in Delayed expansion of ERRORLEVEL on cmd prompt and win32_process

cmd /V:ON /c dir c:\<some non existing directory> & echo %ERRORLEVEL%
Volume in drive C is PC COE Volume Serial Number is 9C37-D0B7
Directory of c:\
File Not Found
0
Lets run the same command using ! to expand the ERRORLEVEL (delayed expansion is enabled)
cmd /V:ON /c dir c:\ERt & echo !ERRORLEVEL!
Volume in drive C is PC COE
Volume Serial Number is 9C37-D0B7
Directory of c:\
File Not Found
!ERRORLEVEL!
It prints !ERRORLEVEL!.
This does work fine when I run the command using a WMI win32_process create command and proper error is returned using the !ERRORLEVEL! variable
What difference it makes when executing in a cmd prompt and executing using WMI win32_process.?
By using
cmd /V:ON /c dir c:\ERt & echo !ERRORLEVEL!
the new command process started with cmd /V:ON executes just the command dir c:\ERt and then closes and the second command echo !ERRORLEVEL! is executed by current command process on which delayed expansion is not enabled.
The command line
cmd /V:ON /c "dir c:\ERt & echo !ERRORLEVEL!"
must be used to run dir c:\ERt AND echo !ERRORLEVEL! in new command process before exiting this command process.
The double quotes around entire command line to execute in new command process makes the difference.
Without the double quotes around command line to execute in new command process the current command process interprets the line with the two commands as when typing
cmd /V:ON /c dir c:\ERt
echo !ERRORLEVEL!
Also possible would be
cmd /V:ON /c dir c:\ERt ^& echo !ERRORLEVEL!
Now & operator is escaped for current command process being interpreted as literal character and therefore the entire line is executed with caret character ^ removed by the new command process.

Passing Y/N when executing a remote bat file from command line

I am trying to execute remote batch file. I could invoke the batch file using PsExec but unable to complete due to :choice in the batch file.
Here is the snippet from batch file
:choice
set /P c=Are you sure you want to continue [Y/N]?
if /I "%c%" EQU "Y" goto :execute_script
if /I "%c%" EQU "N" goto :END
goto :choice
which populating question
I want to handle this question from the command which is like :
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c (^cd C:\BatchExecutors ^& SnapExecutor.bat location^)
Suggestions appreciated. Thanks in Advance.
You can use a strange CMD behaviour when 2 batch files have the same label, to bypass the question. But any code prior to the question would be ignored.
To do this, create another batch file with this inside:
call :execute_script
goto:eof
:execute_script
cd /D C:\BatchExecutors
SnapExecutor.bat %*
So what happens here is that this script will call SnapExecutor.bat, but instead of starting from the begining of the script, it will start from :execute_script
The problem now is how to execute this remotely. You might be able to create this script in a remote writable folder using this command:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "cd /D c:\[temp folder] &echo call :execute_script>temp.bat &echo goto:eof>>temp.bat &echo :execute_script>>temp.bat &echo cd /D C:\BatchExecutors>>temp.bat &echo SnapExecutor.bat %%*>>temp.bat"
(This will create the batch file, then you use this to call it:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "C:\[temp folder]\temp.bat"
NOTE:
Change [temp folder] to a writeable folder on the remote PC.

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

Resources