How to run any program with batch file from any specific location - batch-file

I wrote a batch file for open a specific program but it is not working.I wrote this :
#echo off
C:\Windows\System32\cmd.exe /K "cd /d C:\Program Files (x86)\HTC\HTC Sync Manager\"
start HTCSyncManager.exe
When I run the batch file only this window come, program do not start. How to fix this

#echo off
For /r c: %%f in (path goes here /HTCsyncmanager.exe) do (
start "%%f"
"%%f"
)

Remove the invoking of cmd.exe. All that's doing is starting a new instance of the command processor, which is not what you need at all. (You probably don't need the start, either.)
#echo off
cd /d "C:\Program Files (x86)\HTC\HTC Sync Manager\"
HTCSyncManager.exe

#echo off
start "HTCSyncManager" "C:\Program Files\(x86)\HTC\HTC Sync Manager\HTCSyncManager.exe"
This will open HTCSyncManager.

Related

many start commands batch in the same window

I would like to start 3 commands in the same window.
For now I have this batch but there are 3 different windows at each command.
start /d "c:\Program Files\myfolder" cmd /k cscript A
timeout /t 6 >nul
start /d "c:\Program Files\myfolder" cmd /k cscript B
timeout /t 6 >nul
start /wait /d "c:\PProgram Files\myfolder" cmd /k cscript C
What should I modify to have only one window? thanks
I think you can run a .bat file by changing the directory like so, cd C:\PATH\TO\DIRECTORY\WITH\FILE, then use call (file name here). This should work assuming that all the files are in the same directory, if not you'll just have to change the directory for each call method. If my code doesn't seem very helpful, check this page out https://superuser.com/a/1062322
Example Code
#echo off
cd PATH\TO\FILE\DIRECTORY
call FILE NAME
echo The file (file name here) has run!
pause
this code will make a call to the file and pause the terminal to keep it opened. You can take this code and make as many calls/cd's as you like. I hope this helped, If it doesn't work, please tell me what doesn't work and I'll try to fix it. Have a nice day :)

How to run .EXE from .BAT inside the folder where .EXE is locating

To recreate my problem you need to understand that I have next files in 2 folders.
K:\Script.bat
K:\Project\PortChanger.exe
K:\Project\settings.xml
I want to launch PortChanger.exe using Script.bat that contains next line:
start "K:\Project\PortChanger.exe"
This script is actually executing Program.exe, but my program throws me exception since PortChanger.exe can't find Settings.xml.
How can I launch PortChanger.exe from "K:\Project\", not from "K:\"? Now it seems like .BAT taking .EXE code and just running it where .BAT is locating.
To make it even more clear:
You could use Start with its /D option:
Start "" /D "K:\Project" "K:\Project\PortChanger.exe"
Open a Command Prompt window and enter start /? to read its usage information.
I would suggest you rather use pushd and popd
#echo off
pushd "K:\Project"
start "" PortChanger.exe
popd
pushd will change to the directory, launch the executable from it, then popd will return to the previous directory stored.

CMD/FTP to create folder using to today date & connect ftp download into created folder

I'm new to this cmd/FTP command. I would like to create a new folder at my local directory using today's date and connect to FTP to download the specific file to the newly created folder. If I manually type in command one by one at cmd, it has no issue. But when I use a batch file to run, my command stopped at FTP.
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4"_"job%
mkdir C:\%name%
cd C:\%name%
ftp
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget -i *.*
I did try to separate my command to two batches;
1. folder creation
2. FTP download but the file downloaded didn't go into the folder I created. the downloaded file went to C:\Document & Settings.
main batch file
#echo off
call rename.bat
ftp -i -s:ftp.txt
rename.bat
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd c:\%name%
ftp.txt
open 192.168.31.93
*user*
*password*
binary
cd *directory*
mget *.*
close
Another method I try is using '!' when in FTP environment, then create a folder then exit back to FTP environment. This method again doesn't work with the batch file. Please help
It seems that with command extensions enabled, the working directory set by a child batch file is lost, then the batch file exits.
I'm not sure how to solve it, but you actually do not need the rename.bat file to be a separate file. This "main batch file" should work:
#echo off
setlocal enableextensions
set name=%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"_job"
mkdir c:\%name%
cd /d c:\%name%
ftp -i "-s:%~dp0\ftp.txt"
Also note the /d added to cd. Without that your batch will not work when started from another drive. You also have to use %~dp0 to refer to the batch file folder for the ftp.txt. As at the time ftp is called, you have changed to the target directory.
You possibly do not even need the command extensions to be enabled. So simply removing the setlocal enableextensions might solve the problem too. Though you still need the %~dp0 and /d.
I've decided to post this, although similar to the answer given, there are a couple of differences.
It creates the text file, then deletes it, (this keeps everything more portable).
I have corrected your directory name, (because of a typo).
#Echo Off
Set "Name=%DATE%"
Set "Name=%Name:~-10,2%-%Name:~-7,2%-%Name:~-4%_job"
MD "C:\%Name%" 2>Nul
CD /D "C:\%Name%" || Exit /B
( Echo open 192.168.31.93
Echo *user*
Echo *password*
Echo binary
Echo cd *directory*
Echo mget *.*
Echo close
)>"ftp.txt"
FTP -i -s:ftp.txt
Del "ftp.txt" 2>Nul
Exit /B

Command Prompt Scripting

This is going to be hard to explain, I'll try my best.
I'm using a shell scripting (Test Executor) in command prompt to automate our regression testing. However i'm stuck on one thing.
Basically I have to copy this dynamically created folder in a location into another folder after the automated script is done (in CMD via scripting)
:: create an achieve directory
#SET ACHIEVE_DIR=C:\Test Result\LOG_Files
mkdir "%ACHIEVE_DIR%"
xcopy /y /s /e "%TEST_SOURCE_DIR%"\*.* "%ACHIEVE_DIR%"
This will copy all the files in the TEST_SOURCE_DIR, but i only want this dynamically created folder which changes according to the date stamp (BUT this date stamp is calculated by second - so i can't use that as a variable).
For example: After i run a test it'll create a folder under lets say C:\temp\ another folder named "TEST RUN - _2014-06-30 15_48_01_"
So if go into C:\temp i'll find a folder named "TEST RUN - _2014-06-30 15_48_01_" along with other folders. BUT i only want to copy this dynamic folder into Test Result\LOG_Files
What we are given is that the starting string will always be "TEST RUN -" but the ending string will interchange according to the time to the second (BUT we cannot go by that since it is the second of the starting test result).
I found out if i am in command prompt and i type in "cd TEST RU" and hit tab it'll finish it for me "cd TEST RUN - _2014-06-30 15_48_01_" Is there a way to script this tab hit or something?
Thanks!
This should work in a batch file to isolate the folder:
If there are a number of folders starting with test run - then tell us.
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do xcopy "%%a\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
This code looks for a test folder as discussed in the comments:
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do (
pushd "%%a"
for /d /r %%b in ("test*") do (
xcopy "%%b\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
)
popd
)
This batch file should do the job:
#echo off
rem Find the directory starting with "TEST RUN -" created last in C:\Temp.
set ACHIEVE_DIR=C:\Test Result\LOG_Files
for /F "usebackq delims=" %%D in ( `dir "C:\Temp\TEST RUN -*" /AD /B /O-D /TC` ) do (
set TEST_SOURCE_DIR=%%D
goto CopyDir
)
echo No directory starting with "TEST RUN -" found in C:\Temp.
pause
exit /b
:CopyDir
rem Copy all files and subdirectories from last test run to achieve
rem directory whereby xcopy automatically creates the achieve directory.
xcopy /E /I /Q /S /Y "%TEST_SOURCE_DIR%" "%ACHIEVE_DIR%"
set ACHIEVE_DIR=
set TEST_SOURCE_DIR=
Read the Microsoft pages about
dir
for
xcopy
for details about the used parameters of those 3 commands.
Or execute in a command prompt window the commands
dir /?
for /?
xcopy /?
one after the other to get the shorter help for those 3 commands output in the window.

Running several batch-files in a batch-file

I'm making a batch-file which is supposed to run several batch-files. The problem is that it runs the first batch-file, and then quits
echo "Running SharePoint.Access\buildPackageAndCopy.bat..."
call SharePoint.Access\buildPackageAndCopy.bat
echo "Running SharePoint.Admin\buildPackageAndCopy.bat..."
call SharePoint.Admin\buildPackageAndCopy.bat
echo "Running Sharepoint.Levels\buildPackageAndCopy.bat..."
call SharePoint.Levels\buildPackageAndCopy.bat
I'm sure i have an enter-line between all the commands.
This is one of the batch-files I am running
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
cd /d C:\src\SharePoint\
msbuild /p:IsPackaging=True
md c:\temp\_wsp
xcopy Core\bin\Debug\SharePoint.Core.wsp c:\temp\_wsp /y
xcopy Definitions03\bin\Debug\SharePoint.CustomDefinitions01.wsp c:\temp\_wsp /y
xcopy CustomLists02\bin\Debug\SharePoint.CustomLists01.wsp c:\temp\_wsp /y
xcopy Customs01\bin\Debug\SharePoint.CustomPresentation01.wsp c:\temp\_wsp /y
Try commenting out the content of the first batch file and see if the second one now runs. That way you'll know whether something in the first one is aborting it.

Resources