user2142250
user2142250

Reputation: 349

How to change the output file name in a bat file?

I'm trying to change the output file name, fOut, in a bat file, but have no luck so far.

I'm developing on Windows 7 and will deploy the code to Windows 2003 server.

The code looks like this:

set fName=%1
set fExt=%fName:~-5,-1%

set fOut=%fName:~0,-5%_PAD%fName:~-5%   

Examples of fOut:

abcdc2evv_PAD.dat

abcdefgh33ij_3737_PAD.dat 

How can I change fOut to get the following file names?

A. Adding FMT_ at the beginning of the file name:

FMT_abcdc2evv_PAD.dat

FMT_abcdefgh33ij_3737_PAD.dat

B. Adding FMT_ at the beginning of the file name and remove _PAD before .dat:

FMT_abcdc2evv.dat

FMT_abcdefgh33ij_3737.dat

Addendum: Just one argument is passed to the bat file: path + file name.

x.bat "C\test\xxx.dat"

In the bat file:

@echo ^-input file name = ^%1

set fName=%1
set fExt=%fName:~-5,-1%

set fOut==%fName:~0,-5%_PAD%fName:~-5%

Upvotes: 0

Views: 1998

Answers (3)

Stephan
Stephan

Reputation: 56189

If you want to separate the filename from the extension, don't mess around counting chars; there is a built-in method (described in for /?):

echo Filename=%~n1
echo Extension=%~x1
echo resulting file="FMT_%~1"
REM without _PAD, following with _PAD
set filename="FMT_%~n1_PAD%~x1"

If there is really need to remove _PAD (as Chris already noted, you are explicitely adding it with your code), just replace _PAD. with . only:

set filename=%filename:_PAD.=.%

Upvotes: 1

Callie J
Callie J

Reputation: 31306

I don't know if I'm missing something obvious - it's not clear what the input to this script is.

However adding FMT_ before should just be a case of changing:

set fOut=%fName:~0,-5%_PAD%fName:~-5% 

to:

set fOut=FMT_%fName:~0,-5%_PAD%fName:~-5% 

or if you want to put the FMT_ version into another variable, then:

set bob=FMT_%fOut%

As for removing _PAD, can you not just repeat the SET fOut line without the _PAD? This would seem to be the simplest way to do it. In fact, removing _PAD and prefixing FMT_ would seem to simply be this:

set bob=FMT_%1

Upvotes: 1

mgrenier
mgrenier

Reputation: 1447

if you want to remove pad just take it out of your assignment statement you have:

set fOut=%fName:~0,-5%_PAD%fName:~-5%   

you want:

set fOut=%fName:~0,-5%fName:~-5%

to add FMT_ just add it at the beginning of the file name:

set fOut=%FMT_%fName:~0,-5%_PAD%fName:~-5%

Upvotes: 1

Related Questions