1.

Solve : Problems with a DOS script to rename certain files in a directory?

Answer»

I am trying to rename certain files in a dir, for example, for any file with ".txt" in the file name, I would like to strip out .txt and rename with the extension .cpp

I have the following code, and it is not working. I cannot save the current file name
i.e. %%i to a local variable:

FOR %%i IN (*.txt) DO (
REM assign the filename to a local var
set str=%%i

REM strip out .txt in the filename
set str=%str:.txt=the%

REM rename with a new extension
set str=str>>.cpp

REM do the rename
ren %%i str

-one problem I notice is that str is always GETTING the last file in the dir
-I am not sure how to use "!" to ammend this
-also not sure if I should be using /F

Thanks...Any help is appreciated
-Iggy

Code: [Select]@echo off
for %%f in (*.txt) do COPY %%f %%~nf.cpp
Thank you.

But what if the file name is:

myfile.txtcpp04022007.txt OR myfile.txtcpp04022007.cpp, and I would like to rename
it to myfile04022007.cpp?

-also, not sure what %%~nf means?

Thanks again,

-Iggy.Quote from: iggy7 on April 01, 2007, 11:22:31 PM

But what if the file name is:
myfile.txtcpp04022007.txt OR myfile.txtcpp04022007.cpp, and I would like to rename
it to myfile04022007.cpp?
please type set /? on the command line and go through the text it says.
you can do string replacement using set
eg
Code: [Select]C:\temp>set var=myfile.txt2323.cpp
C:\temp>echo %var%
myfile.txt2323.cpp
C:\temp>set VAR1=%var:.txt=%
C:\temp>echo %var1%
myfile2323.cpp



Quote
-also, not sure what %%~nf means?
Thanks again,

-Iggy.

please look at for /? , and right near the end, there is a description.

...

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
......
.....
Was trying to do the following with in the for loop:

set var=%%f
set var1=%var:.txt=%
set var1=var1>>.cpp

i.e. for each file name in the directory. But I the assignment to var
did not work. I kept on getting the last file in the dir.

Any additional help is appreciated.
show the WHOLE code you have>dir/b YIELDS three files:

file1.txt.cpp.03222007.cpp
file2.txt.cpp.03112007.cpp
file3.03012007.cpp

I would like to rename them to:

file1.03222007.txt.cpp
file2.03112007.txt.cpp
file3.03012007.txt.cpp

the code is:

for %%f in (*.txt) do (
REM get the filemname
set var=%%f

REM strip out .txt
set var1=%var:.txt=%

REM strip out .cpp
set var1=%var:.cpp=%

REM append t he extension
set var1=var1>>.txt.cpp

REM so the rename
ren %var% %var1%
)


this is the ouput and this is not what I obviously want:

H:\stuff\script>for %f in (*.txt) do (
set var=%f
set var1=sample.cpp03012007.cpp
set var1=sample.txt03012007.txt
set var1=var1 1>>.txt.cpp
ren sample.txt.cpp03012007.cpp.txt var1
)

H:\stuff\script>(
set var=file1.txt.cpp.03222007.cpp.txt
set var1=sample.cpp03012007.cpp
set var1=sample.txt03012007.txt
set var1=var1 1>>.txt.cpp
ren sample.txt.cpp03012007.cpp.txt var1
)
The system cannot find the file specified.

H:\stuff\script>(
set var=file2.txt.cpp.03112007.cpp.txt
set var1=sample.cpp03012007.cpp
set var1=sample.txt03012007.txt
set var1=var1 1>>.txt.cpp
ren sample.txt.cpp03012007.cpp.txt var1
)
The system cannot find the file specified.
H:\stuff\script>



-Any help is appreciated.
Iggy


-Thank you very much.copy *.txt *.txt.cpp
If you wish to retain the .txt in the filename.
If you want to transform .txt files in .cpp files:
copy *.txt *.cpp


Discussion

No Comment Found