| 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 But what if the file name is: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? 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 |
|