1.

Solve : MS-DOS rename?

Answer»

I'm trying to rename many similar file names at once.

IE CND_m2_CND-7130-01_WORKING.SHT to CND-7103-01.DGN

in sequence 7103-02, 7103-03, etc...

With rename in ms-dos I can remove “_WORKING.SHT” and replace it with “.DGN”, but I'm having trouble removing “CND_m2_” from the beginning of EVERY filename. * wildcard doesn't change anything & ? seem to be character specific, "ren CND_m2_CND-7130-??.DGN CND-7130-??.DGN" becomes "CND-7130-D-.DGN" where the new ?? represents the 10th & 11th character bringing in “-D” the 10th & 11th characters of the old name.

I've tried the FOR command from an example I found on the net, but it is beyond me to break it down as to why its not working for me.

for /f "delims=O usebackq" %i in (`DIR /b O*`) do @ren O%i %i

from this discussion:

http://discuss.fogcreek.com/joelonsoftware2/default.asp?cmd=show&ixPost=52248

any help WOULD be greatly appreciated!!Code: [Select]@echo off & setlocal enabledelayedexpansion

for %%a in ("CND_m2_*.SHT") do (
set f=%%~na
set f=!f:CND_m2_=!
echo ren "%%~nxa" "!f!.DGN"
)
remove 'echo' when you are sure to renameI tried this as a batch (stored in the same folder as the files) and the cmd window flashed on & off but nothing happened. I opened a cmd window and pasted and got this:

Quote

@echo off & setlocal enabledelayedexpansion

for %%a in ("CND_m2_*.SHT") do (
%%a was unexpected at this time.
set f=%%~na
set f=!f:CND_m2_=!
ren "%%~nxa" "!f!.DGN"
The system cannot find the file specified.
)

In trying to understand the code this is my understanding:
@echo off & setlocal enabledelayedexpansion
COMMANDS NOT DISPLAYED TO SCREEN & PERFORM COMMANDS LOCALLY (CURRENT DIRECTORY ONLY?)

for %%a in ("CND_m2_*.SHT") do (
==FOR %variable IN (set) DO command [command-parameters]==
FINDS WHERE THE FILENAME = “CND_m2_*.SHT” (NAMED VARIABLE A)
set f=%%~na
set f=!f:CND_m2_=!
CREATES SET OF FILES TO PERFORM COMMAND TO (SET NAMED “CND_m2_*.SHT”)
ren "%%~nxa" "!f!.DGN"
RENAME
)

What am I missing or doing wrong?
Thankshere's a vbscript alternative
Code: [Select]
Set objFS = CREATEOBJECT("Scripting.FileSystemObject")
strFolder = "c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
strFileName = strFile.Name
If InStr(strFileName,"CND_m2") > 0 Then
strFileName = Replace(strFileName,"CND_m2_","")
strFileName = Replace(strFileName,"_WORKING.SHT","")
strNewName = strFileName & ".DGN"
strFile.Name= strNewName
End If
Next

save as myscript.vbs and on command prompt,
Code: [Select]c:\test> cscritp /nologo myscript.vbs
1. put the batch file inside the same folder as .SHT files
2. start-->run-->type cmd then enter
3. cd to the directory where the .SHT files eg. cd c:\test\
4. type batchfile.bat at cmd prompt

or rename the batch file to cmd extension and double-click to runQuote
I tried this as a batch (stored in the same folder as the files) and the cmd window flashed on & off but nothing happened. I opened a cmd window and pasted and got this:
I clicked the bat which makes sense now why pops up run and closes a command window, but when
I pasted the command after changing directory (cd) to the folder containing the files i want to change, is when i got the errorThanks gh0std0g74
Yes! That removed the beginning part, but only added .dgn to the end. In the cmd window Quote
ren *_working.sht.dgn *.dgn
doesn't make any changes either. But, Quote
ren ??_working.sht.dgn ??.dgn
(crap 3X?= )
changes the filename correctly. Is there a way to WRITE the script character specific (quantity wise)?Quote from: fsjeep on May 13, 2009, 07:42:01 PM
I'm trying to rename many similar file names at once.

IE CND_m2_CND-7130-01_WORKING.SHT to CND-7103-01.DGN

in sequence 7103-02, 7103-03, etc...
Code: [Select]@echo off & setlocal enabledelayedexpansion

pushd c:\workingshitfolder\ && (
for %%a in ("CND_m2_*.SHT") do (
set f=%%~na
set f=!f:CND_m2_=!
set f=!f:_WORKING=!
set f=!f:~,6!!f:~7,1!!f:~6,1!!f:~8,99!
echo "%%~nxa" --^> "!f!.DGN"
ren "%%~nxa" "!f!.DGN"
)
popd
) || echo invalid folder
pause

example output:
Code: [Select]D:\batch>dir/b cnd*
CND_m2_CND-7130-01_WORKING.SHT

D:\batch>sht
"CND_m2_CND-7130-01_WORKING.SHT" --> "CND-7103-01.DGN"
Press any key to continue . . .

D:\batch>dir/b cnd*
CND-7103-01.DGNThanks Reno! that last one works awesome!!


Discussion

No Comment Found