Lesley Peters
Lesley Peters

Reputation: 409

.bat file for renaming another file without giving the path

I have a folder that cointains a .bat file and another folder called Config.

I can't figure out how to rename a file (test.txt) in the folder Config without giving the path to the file.

I want the .bat file to automatically get the parent folder, then go into the Config folder and rename the test.txt into something else.

This is what I've already found to rename a file

ren C:\Users\Admin\Directory\Config\test.txt something.txt

And I guess that what I want should look like this but I can't figure out how.

ren %ConfigDirectory%\test.txt something.txt

Thanks for your time!

Upvotes: 0

Views: 117

Answers (2)

ender_scythe
ender_scythe

Reputation: 508

This should work:

set parentdir=%cd%
ren %parentdir%\test.txt something.txt

The current directory is stored in the variable %cd%, you could use one line (ren %cd%\test.txt something.txt), but using these two always works.
And if you want to add a directory to the path:

set parentdir=%cd%
set dir=%parentdir%\directory
ren %dir%\test.txt something.txt

EDIT: apparently quotations around the directory helps the command work out:

set parentdir=%cd%
ren "%parentdir%\test.txt" something.txt

Upvotes: 2

mir16
mir16

Reputation: 61

this isn't answer, but i can't commenting on this amazing forum.. huh *a nice rule*

and where are your .bat file?.. his path is..

this path C:\Users\Admin\Directory\Config isn't default path for os windows.. so no one variable, in environment, haven't this value.. and you can't use such variable until you append her:

setx ConfigDirectory C:\Users\Admin\Directory\Config

maximum what you can do until.. %userprofile%\Directory\Config\test.txt

also you can point path until parent dir of the batch file

%~dp0

so if your .bat file is in C:\Users\Admin\Directory that path, until test.txt, will be %~dp0\config

Upvotes: 1

Related Questions