|
Answer» I'm TRYING to rename a group of files in a directory with a simple DOS command in a .bat file. They all start with the same character and a space. I want to remove the character and the space but using ren or rename is making me crazy...
An example of the file name would be: 2 WKRN 6PM MF-2012-05-08-0.txt (all of my file names begin with "2 WKRN") and I want to remove the "2 " (numeral and space) so it becomes "WKRN 6PM MF-2012-05-08-0.txt". It seems like I should be able to:
rename "2 wkrn*.txt" "wkrn*.txt" but this is giving me "wkrnRN 6PM MF-2012-05-08-0.txt", which seems TOTALLY odd...
Any help would greatly appreciated. I feel like an idiot! Thanks!At the prompt:
CODE: [Select]for /f "tokens=1* delims= " %A in ('dir /b "2 WKRN*.txt"') do Ren "%A %B" "%B" In a batch script you need to DOUBLE the percent signs:
Code: [Select]for /f "tokens=1* delims= " %%A in ('dir /b "2 WKRN*.txt"') do Ren "%%A %%B" "%%B" (copied and pasted from a command window) a demo run from the prompt, with a DIR before and after...
Before:
Code: [Select]C:\Batch\Test>dir Volume in drive C is Win07 Volume Serial Number is E4DB-A92A
Directory of C:\Batch\Test
09/05/2012 20:31 <DIR> . 09/05/2012 20:31 <DIR> .. 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-0.txt 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-1.txt 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-2.txt 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-3.txt 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-4.txt 09/05/2012 20:31 8 2 WKRN 6PM MF-2012-05-08-5.txt 6 File(s) 48 bytes 2 Dir(s) 37,448,957,952 bytes free
The command:
Code: [Select]C:\Batch\Test>for /f "tokens=1* delims= " %A in ('dir /b "2 WKRN*.txt"') do Ren "%A %B" "%B"
The output as each file is renamed in turn:
Code: [Select]C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-0.txt" "WKRN 6PM MF-2012-05-08-0.txt"
C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-1.txt" "WKRN 6PM MF-2012-05-08-1.txt"
C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-2.txt" "WKRN 6PM MF-2012-05-08-2.txt"
C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-3.txt" "WKRN 6PM MF-2012-05-08-3.txt"
C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-4.txt" "WKRN 6PM MF-2012-05-08-4.txt"
C:\Batch\Test>Ren "2 WKRN 6PM MF-2012-05-08-5.txt" "WKRN 6PM MF-2012-05-08-5.txt"
After:
Code: [Select]C:\Batch\Test>dir Volume in drive C is Win07 Volume Serial Number is E4DB-A92A
Directory of C:\Batch\Test
09/05/2012 20:37 <DIR> . 09/05/2012 20:37 <DIR> .. 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-0.txt 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-1.txt 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-2.txt 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-3.txt 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-4.txt 09/05/2012 20:31 8 WKRN 6PM MF-2012-05-08-5.txt 6 File(s) 48 bytes 2 Dir(s) 37,448,957,952 bytes free
C:\Batch\Test>
Put @Echo before the Ren command if you just want to see what it would do, without actually renaming anything
like this
Code: [Select]for /f "tokens=1* delims= " %A in ('dir /b "2 WKRN*.txt"') do @Echo Ren "%A %B" "%B"Thank you so much!!
|