batch not copying file to new location in new cmd window - batch-file

i need to create a file as if i was the browser for a qa test. I figure if i copy and rename cmd.exe i can execute commands and the process will be the browser and complete my criteria for the test. i need to do this via a batch script. Below is my attempt at getting something to run as if it was from the browser.
my issue is the command is not creating the file in the directory like it should be. The dir command shows the directory as being empty even though as far as i can tell, everything worked correctly. There are no error logs or alerts. why can i not copy this file?
thanks!
copy C:\Windows\System32\cmd.exe %userprofile%\AppData\Roaming\firefox.exe
md %userprofile%\AppData\Local\test12
start %userprofile%\AppData\Roaming\firefox.exe \C copy C:\Windows\System32\cmd.exe %userprofile%\AppData\Local\test12\Test12.exe
dir %userprofile%\AppData\Local\test12\

If you try to use /K in stead of \C (the slash is wrong here) the cmd.exe (firefox.exe) will show you any errors that occured :
This works fine here:
start /WAIT %userprofile%\AppData\Roaming\firefox.exe /K copy C:\Windows\System32\cmd.exe %userprofile%\AppData\Local\test12\Test12.exe

Related

Why change of sequence in cmd command gives different result?

I'm new in using cmd and batch script. Here is what I was trying to do with a .bat file.
go to a folder
open that folder in file explorer
open that folder in vscode
start Firefox browser
Sequence doesn't matter here. Doing these tasks in any sequence is fine.
Here are the commands that I initially wrote
cd/
E:
cd folder1/folder2/folder3
code .
start .
start firefox
But with these commands the result I got is
Only vscode started with the folder I wanted to open with vscode
File explorer and Firefox was not starting
cmd.exe continued to run. But it seems like it only executed upto code . command
When I closed vscode, cmd.exe would be closed with it too
Then I changed the sequence of the commands as below
cd/
E:
cd folder1/folder2/folder3
start .
start firefox
code .
This time everything worked as expected. I've looked up documentation for start command but didn't find anything(or maybe I didn't understand) about start . command.
Can anyone explain me why the result changed when I changed the sequence of the commands?
As already mentioned in the comments, code is really a batch file, code.cmd.
If your VSCode installer added a location to your %PATH%, take a look there and you should see code.cmd in that \bin directory.
When you run a batch file from another, if you want control to pass back to the initial script upon completion, you need to run it using the call command.
#CD /D "E:\folder1\folder2\folder3" 2>NUL || Exit /B
#Call "%ProgramFiles%\Microsoft VS Code\bin\code.cmd" .
#Start "" "%SystemRoot%\explorer.exe" .
#Start "" "%ProgramFiles%\Mozilla Firefox\firefox.exe"
If you do not want robust code, and wish to assume file extensions and environment settings, the above could be simplified to this:
#CD /D E:\folder1\folder2\folder3
#Call code .
#Start explorer .
#Start firefox

Environment changes made using "start" are not visible later in a batch file

I have a batch that is like this
#echo off
SET workspace="C:\yii2"
SET deploy_directory="C:\deployment"
SET source_file="C:\deployment\yii2"
SET destination_file="C:\deployment\deployment.zip"
cd "%source_file%"
start /wait update composer
start /wait xcopy "%workspace%" "%source_file%" /s /h /f
cd "%deploy_directory%"
start /wait zipjs.bat zipItem -source "%source_file%" -destination "%destination_file%" -keep yes -force no
pause
The bat file can anywhere be in the system.. So I write something like this.. Currently this is not working .. when it runs and change the directory the command prompt pops out and it does not process the next code.. Is there a silent way to change directory without interruption and will process the next command?
main goal is that to run composer update in the target path. Would appreciate also to explain what's happening here.
Pseudo:
1.) go to workspace path to update composer
2.) Copy workspace to a certain path with my zip bat
3.) change to that directory
4.) Run zipping bat
Edit: Added more steps
start starts a new command window. You just want to change the working directory of the command window that is going to run update. So don't use start for the cd bit:
cd "%source_file%"
start /wait update composer
One reason to use start is to run another batch file without terminating the current one. This works because the second batch file is run in a new command shell (and window). Usually, a better option is to use call, which runs the second batch file in the current command shell / window but doesn't terminate the current shell. In your case, since update is also a batch file, you can use call. So your script now looks like this:
cd "%source_file%"
call update composer

Copy file from directory to another using cmd command

I want to copy a file from sharing folder to another using cmd command ".bat file"
the below code works normally with most files except MS Access files the "accde" extension so, please advise.
MKDIR "\\192.168.0.110\Attendance Sheet\JTA\events\events_media\SysFile"
XCopy /y/z "\\192.168.0.110\Attendance Sheet\JTA\events\events_media\db.accde"
"\\192.168.0.110\Attendance Sheet\JTA\events\events_media\SysFile"
Run this code to have the command execute in the background so that you can continue to work from the command line while it is running:
start /min xcopy /y /z "sourcefile" "destination"
Actually, the command is running in a minimized window of it's own - you can see it popping up in the taskbar. But from a user's perspective it's the same as running "in the background".

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

How do i create msinfo32.nfo file automatic with a BAT file?

I have a bat file i called it test.bat and inside i added one line:
This is what i tried so far : The problem is that the file never created.
start /wait msinfo32.exe /s C:\TEMP\test.nfo
When im running the bat file i see the cmd window for second then i see the msinfo32.exe program . But the file test.nfo never created in C:\TEMP
How can i create the test.nfo automatic and how do i make that after the file is created so the msinfo32.exe program will be exit/closed ?
I Googled command line options and tested this:
start /wait msinfo32.exe /nfo c:\temp\test.nfo
See here: http://ss64.com/nt/msinfo32.html

Resources