Batch File to set variable of a file name

November 8, 2010 at 11:40:45
Specs: Windows XP
I'm try to make batch file that looks in the dir that it is running in looking for .xml. then take the file name of the XML and sets it as a variable for use finding the file that changes need to made to and to be used in the file its self.

I'm use Change.com to change text in the XML file.

But the way that I'm getting the file name dose not work if there is special chracters in the filename like a space.

Here is a working version of what I have:

@echo off
for /F %%a in ('dir /b *.xml') do set FileName=%%~na


echo what file do you have %Filename%
echo.

pause

echo lets make a file....

change.com %Filename%.xml "***FILE_NAME***" "%Filename%"
echo.
pause


See More: Batch File to set variable of a file name

Report •

#1
November 8, 2010 at 12:04:17
Not clear what you want but change.com will not work on any file not 8.3 naming style.


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
November 8, 2010 at 12:12:13
any suggestion on way to do this then?

thanks


Report •

#3
November 8, 2010 at 12:28:17
Also note that because it's a DOS .COM file and works in one segment it's max file size is about 32KB.


===============================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.xml') do (
move /y "%%a" #
change # "bla" "nobla"
move /y # "%%a"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
November 8, 2010 at 17:01:59
Thanks M2!

any ideals on how when I set the variable "filename" it can get them if it has spaces in the file name?

Thanks


Report •

#5
November 8, 2010 at 17:08:59
I have no idea why you're setting a var to a filename with no extension then a few lines later you're sticking on the extension.


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
November 8, 2010 at 17:17:57
I need the filename with out the extension on it to place in the xml file its self.

here is my new code:


@echo off

for /F %%a in ('dir /b *.xml') do set FileName=%%~na
echo what file do you have?
echo %Filename%
pause

echo lets make a file....

@echo off & setLocal EnableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir/b *.xml') do (
move /y "%%a" #
change # "***FILE_NAME***" "%Filename%"
move /y # "%%a"
)

pause


Report •

#7
November 8, 2010 at 17:25:57
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b *.xml') do (
set var=%%~Na
echo.!var!
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question