Batch file to read the root directory it is in - batch-file

Is there a batch command that can read a root directory without the entire path?
I want a batch file to tell me if its in D:\ or E:\.
I tried to use:
set mypath=%cd%
#echo %mypath%
Pause
But it just says the exact place it is in rather than just the root.

Here's a few options for you which provide the root directory of the scripts current directory:
Using PushD/Popd
PushD\&Call Set "RootDir=%%CD%%"&PopD
Echo(%RootDir%
Pause
Using a For loop
For %%A In (%CD%) Do Set "RootDir=%%~dA\"
Echo(%RootDir%
Pause
Using variable expansion
Set "RootDir=%CD:~,3%"
Echo(%RootDir%
Pause
Edit
After reading your question again, I decided to add a fourth example. This one unlike the other three provides the root directory of the batch files location.
Set "RootDir=%~d0\"
Echo(%RootDir%
Pause

the directory where the batch file is located could be different from the current directory cmd.exe operates in.
TO get the batch file root path use:
for %%a in ("%0") do echo %%~da
To ger the current directory use
echo "%cd:~0,3%"

And let us not forget the &REM trick.
#ECHO OFF
set "root=%cd:\="&rem %
echo %root%

Related

open every file in seperate folders using a batch file

okay, so I have written a little script of code :
for /R %%f in (*.acd) do C:\Users\jelle\Downloads\quickbms\quickbms.exe D:\assetto_corsa_acd.bms %%f "\data"
but the problem is that the file data.acd will be extracted using quickbms.exe to a data folder in the main folder my batch file is located in stead of in the /data folder located in the folder where the data.acd file is, so x:\carname(folder_with_data.acd_in_it)\data
but i can't get that to work,
so my question is how do i get quickbms to extract the acd file to the folder where the acd file is located instead of where the batch file is located?
Also note that quickbms is in my path
Thanks in advance.
Your posted script appears to be running two lines instead of only one.
Based on the following usage:
quickbms.exe [options] <script.BMS> <input_archive/folder> [output_folder]
I would suggest something like this:
#Echo Off
Rem Full path to QuickBMS
Set "eBMS=%UserProfile%\Downloads\quickbms\quickbms.exe"
Rem Full path to BMS script
Set "sBMS=D:\assetto_corsa_acd.BMS"
Rem BMS Options
Set "oBMS=-o -d"
Rem The source directory path to recurse (. is current, %~1 is arg[0])
Set "sDIR=."
Rem The sourcefile extension
Set "sEXT=.acd"
Rem The output directory path (.\ relevant to current, %~2 is arg[1])
Set "tOUT=.\data"
For /R "%sDIR%" %%A In (*%sEXT%) Do "%eBMS%" %oBMS% "%sBMS%" "%%A" "%tOUT%"
Fill in the Set command lines according to the Remarks and give it a try reporting back any issues as necessary.

Batch script that do a backup

im a beginner in batch script,and im trying to do a script that ask for a file path,then create a folder(in the current folder)and I want the created folder name to be like "sav-DATE-TIME"(for example,right now the folder would be named: sav-2015-12-07-18-55-00) and then copy the file given by the user filepath in the created folder.So far I did this:
#ECHO OFF
SET /P pathh=Enter the path
SET foldname=sav%DATE%%TIME%
mkdir %foldname%
cd %foldname%
xcopy /s/e %pathh% %cd%
pause
However when I run this,no matter how I enter the path with " or ' or nothing around the path,it always says that the path is incorrect,and also it create a folder with random number(74,56,21...)as the folder name and I dont understand why it wont work properly,wich mean creating the folder with name as said at the begining of question and also saying the path is always incorrect.
thank you!
You need to use quotes when the variables are expanded (used). The first set of quotes below is only to ensure that you don't have any trailing white space chars. Try this:
#ECHO OFF
SET /P pathh=Enter the path
SET "foldname=sav-%DATE%-%TIME:~0,8%
SET "foldname=%foldname::=-%
echo foldname=%foldname%
rem mkdir "%foldname%"
rem cd "%foldname%"
rem xcopy /s/e "%pathh%" "%cd%"
pause
Remove the rem's when it looks good
set foldname=%date:~-4%-%date:~3,2%-%date:~0,2%:%time:~0,2%:%time:~3,2%,%time:~6,2% this should give you 2015-12-09-10:56,17
Then mkdir %foldname%

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

batch file directory expansion

I am trying to write a batch file that exists in an arbitrary directory and will create a new directory two levels above it. For instance, the batch file here:
w:\src\project\scripts\setup.bat
would create:
w:\src\project.build
I can't seem to figure out how to expand a path. Here is what I am currently doing:
#set SCRIPT_DIR=%~dp0
#set ROOT_DIR=%SCRIPT_DIR%\..
echo ROOT DIR: %ROOT_DIR%
#set ROOT_DIR_NAME=%ROOT_DIR:~0,-1%
#echo ROOT DIR NAME: %ROOT_DIR_NAME%
And this produces:
ROOT DIR: w:\src\w_dev1\scripts\\..
ROOT DIR NAME: w:\src\w_dev1\scripts\\.
What I wanted to do, was get ROOT_DIR_NAME to be the directory itself (without the trailing slash). I know I could hack this and switch the -1 to account for the '..', but is there not a cleaner way to handle this?
Your line #set ROOT_DIR_NAME=%ROOT_DIR:~0,-1% removes ony 1 character from the variable value. You want to remove more ('scripts' has 7, plus you have these '\..' at the end...). Are you sure that you always have 'scripts' as the last directory in the path where you install?
Anyway, if you use #set ROOT_DIR_NAME=%ROOT_DIR:~0,-11% it should work for your particular example.
However, I would suggest you use a more generic approach:
#set SCRIPT_DIR=%~dp0
#pushd %script_dir%
#pushd ..\..
#echo. current directory now is %cd%
#set root_dir=%cd%
#popd
#popd
This works independently of the length of your directory names.
The pushd commands change directories (and remember where it came from) -- the popd commands go back to what was remembered by pushd. The %cd% variable holds the current drive+path.
You can use the for command to resolve a relative path into a canonical absolute path:
#echo off
set "SCRIPT_DIR=%~dp0"
for %%P in ("%SCRIPT_DIR%..\..") do set "ROOT_DIR_NAME=%%~fP"
This script will set ROOT_DIR_NAME to the grand parent directory of the script.
Or a local subroutine
call :setvartopath ROOT_DIR_NAME "%~dp0\..\.."
goto :eof
:setvartopath
set %1=%~f2

How can I change the root path in a .bat file?

In a .bat file, How can I change the root path to be c:\temp\code
cd /d c:\temp\code
I'd suggest pushd over cd in this case. That way you can restore the previous directory with popd at the end. Unless a batch file should actually change the path even after it has been run, I'd always restore it at the end of the batch:
#echo off
rem change current directory
pushd C:\Temp\Code
rem ...
rem something your batch needs to do
rem ...
rem restore old working directory
popd
Sometimes, doing cd c:\temp\code only doesn't work if you're in another drive. This way works all the time:
c:
cd c:\temp\code

Resources