multiple command prompt command on single line - batch-file

I created a batch file with 2 commands on a single line but I can't get it work.
What I'm trying to do is that create a subdirectory and copy test.pdf to DIR3 with new filename hello world.pdf.
Any thoughts guys? how about if the directory already exist?
mkdir W:\DIR1\DIR2\DIR3 && copy "W:\test\test.pdf" W:\DIR1\DIR2\DIR3\hello world.pdf

This is the syntax:
command1 && command2
Tested this for dir && echo foo and it works.
So your command becomes :
mkdir -p "W:\DIR1\DIR2\DIR3" && copy "W:\test\test.pdf" W:\DIR1\DIR2\DIR3\hello world.pdf
Maybe you could have used Google?
Oh and there was an extra " after copy in your command.

Related

Saving the output of a batch file in command prompt

Is there anyway to write the command windows output to a file? To have a transcript if you will.
Yes, for example, on the command prompt you can do this:
dir *.* > c:\temp\a.txt
This operation is called 'output redirection'. The output of the above command will go the named file and will overwrite it if it already exists with the results. You can find many examples on the net for that, example: Redirection.
In command prompt, use >> operator.
For example, If you type below command then output will be saved to output.txt file
dir > output.txt
If you type below then output will be appended to exist file.
dir >> output.txt

How to execute multiple commands in command prompt using batch file?

I am writing a batch file to execute the command (to execute Bitvise SFTP)
start cmd /k sftpc [SERVERNAME] -pk=1
After which the command prompt ask the following
Accept and (S)ave/(A)ccept for This Session/(C)ancel (S/A/C)?
Now the batch file should execute the next command as "A" to go through process.
I want automate the process of providing input "A" in command prompt. How can a batch file execute commands continuously ?
Updated :
After accepting the session, there are two more commands that needs to be executed,
cd [RemoteLocation]
get *.*
How to automate these two commands after the first two commands using the same batch file ?
You could try this to send an "A" to sftpc:
start cmd /k "echo A | sftpc [SERVERNAME] -pk=1"
I don't have sftpc and am just trying a suggestion to help out...
Updated:
If you have multiple commands to give to sftpc, you may find that one of the following methods works, depending on how sftpc works - I am working blind here - still don't have, or want, sftpc.
start cmd /k "echo A & echo cd somewhere & echo get *.* | sftpc [SERVERNAME] -pk=1"
... change "somewhere" to wherever you want to go to.
Or maybe:
Save the following in a file called ftp.cmd
A
cd somewhere
get *.*
Then try:
start cmd /k "sftpc [SERVERNAME] -pk=1 < ftp.cmd"

batch script to change directory and then run kace runkbot 4 0

Hi I am trying to write a batch script to run runkbot 4 0.
So far what I have is:
start cmd.exe /k "C: && cd \program files\dell\kace\runkbot.exe" -s 4 0
Everytime I run this I get an error that directory cannot be found.
If I run it like this it finds the directory:
start cmd.exe /k "C: && cd \program files\dell\kace\
Can anyone tell me how to open a cmd prompt, change the directory and then run the runkbot 4 0 through a batch script?
cd means change directory, but you're giving it a .exe file. It can't find the directory named runbot.exe because that's not a directory. The first example will work if you just remove the cd since the first part of a command is assumed to be executable. cd is a known executable, but so it any other .exe file you specifically request.
try:
start cmd.exe /k "C: && \program files\dell\kace\runkbot.exe" -s 4 0
Here is a batch script:
#echo off
cd /d "C:\program files\dell\kace\"
runkbot.exe -s 4 0
You can try this:
"%PROGRAMFILES(X86)%\Dell\KACE\runkbot.exe" 4 0
"%PROGRAMFILES%\Dell\KACE\runkbot.exe" 4 0

Start cmd.exe and change directory?

I'd like a .bat file which is stored on the desktop to perform 2 simple tasks:
1. Start cmd.exe
2. Change directory to c:\executionsdktest_10.2.2
I have:
#echo off
start cmd.exe \k
cdsdad c:\ExecutionSDKTest_10.2.2
But when I double click the .bat file, this starts cmd.exe but cd to c:\users\qestester\desktop. ANy ideas?
You can use
cmd /k "cd /d c:\ExecutionSDKTest_10.2.2"
And you wouldn't need a batch file for that. This can be put in a normal shortcut.
And if you have a normal shortcut you can just specify its working directory and run cmd directly without any arguments.
This worked for me:
start cmd.exe #cmd /k "cd /d C:\Users\Michael && node test.js"
What I needed ... was from a localhost page served in PHP, open a terminal, change directory, and start a node script. Achieved like this:
pclose(popen("start /B ". $cmd, "r"));
.. where $cmd is the first string above.
start cmd.exe /k "C: && cd \ExecutionSDKTest_10.2.2"
I wanted to:
open a command prompt
change directory (go to another directory)
run a command from there
solution that worked for me:
#echo off
d:
cd\Path\to\wherever
my command
Notes:
the d: after #echo off tells that the path is on the D drive. Write a c instead of the d and You will be on drive C.
third line starts with the mandatory cd, after which starts the path, with a preceding \.
my command can be anything you want, and can contain more that one word (or only one). I used it to run jupyter notebook.

How to create a loop/skip in batch

I am trying to download a chunk of files from an application. The shell command for it is 'go filename download'.
I have a text file containing all the filenames I have to download. All I want to do is to run a script/command such that when the above command is executed
1. the filenames are picked up from the textfile & executed using the above command
2. existing files/unavailable files are skipped
3. the process then continues with the next files in the list
So far I have this idea of using an operator like go $ download & then feed the operator with the text file containing the filenames list. Thanks in advance.
For Windows, you can use for /f to process the file and create a command from it. The following script supergo.cmd shows how this can be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%f in (list.txt) do (
echo go "%%f" download
)
endlocal
The following transcripts shows it in operation:
C:\Pax> type list.txt
file1.txt
file number 2.txt
another file.jpg
C:\Pax> supergo
go "file1.txt" download
go "file number 2.txt" download
go "another file.jpg" download
If you're using a shell like bash, you can use sed to create a temporary script from the input file then run that:
#!/bin/bash
sed -e "s/^/echo go '/" -e "s/$/' download/" list.txt >/tmp/tempexec.$$
chmod u+x /tmp/tempexec.$$
. /tmp/tempexec.$$
rm -rf /tmp/tempexec.$$
This puts an echo go ' at the start of each line, a ' download at the end, then marks it executable and executes it.
In both cases (Windows and bash), remove the echo to get it to do the real work.

Resources