Welz
Welz

Reputation: 235

Batch file that creates another batch file in desired location

Here's my current file (It's supposed to create a folder, goe to that folder and then create a batch file in that folder):

@echo off 

echo md C:\"Program Files (x86)"\AcceleratorTool\

echo cd C:\"Program Files (x86)"\AcceleratorTool\

>AcceleratorTool.bat (
 echo taskkill /f /im chrome.exe
)

Despite me putting cd C:\"Program Files (x86)"\AcceleratorTool\ in, it keeps creating the file in C:\Windows\System32.

It's probably due to the fact that I have to (and do) run the original batch file as administrator, I'm assuming that's related.

Upvotes: 0

Views: 234

Answers (2)

tukan
tukan

Reputation: 17337

Well you have some mistakes in your script. Here is fixed functional version:

@echo off 
REM create the target directory    
mkdir "C:\Program Files (x86)\AcceleratorTool" 2>nul

REM stores your current directory and changes the to the target directory
pushd "C:\Program Files (x86)\AcceleratorTool"

REM echos the command into the .bat file
echo taskkill /f /im chrome.exe > AcceleratorTool.bat

REM returns to the original directory
popd

The comments explain the functionality. Don't forget that you have to have correct rights to access Program Files (x86)!

Upvotes: 1

Compo
Compo

Reputation: 38589

To write a file there's no reason to change directory to the destination location:

@Echo Off
Set "dirName=AcceleratorTool"
Set PROCESSOR_ARCHITE|Find "64">Nul && (
    Set "dirBase=%ProgramFiles(x86)%\%dirName%"
) ||Set "dirBase=%ProgramFiles%\%dirName%"
If Not Exist "%dirBase%\%dirName%\" MD "%dirBase%\%dirName%" 2>Nul ||Exit /B
Echo TaskKill /F /IM chrome.exe /T>"%dirBase%\%dirName%\%dirName%%~x0"

Upvotes: 1

Related Questions