1.

Solve : Changing file extension case?

Answer»

From a batch script, how do I change all specific file extension combinations to all lower case

testfile.CSV
afile.Csv
somefile.csV

Thanks...if its just one file extension, then
ren *.csv *.csv worksThat works except is the file is named file5.CSV2, it is changing it to file5.csv.  I only WANT it to touch files with extension ".csv", I am not sure why rename is touch that file.

Code: [Select]11/15/2011  12:36 PM    <DIR>          .
11/15/2011  12:36 PM    <DIR>          ..
08/01/2011  01:59 PM            41,534 a.tmp
07/30/2011  05:44 AM    <DIR>          Desktop
11/14/2011  01:31 PM    <DIR>          Favorites
11/15/2011  12:36 PM                 0 file1.Csv
11/15/2011  12:36 PM                 0 file2.CSV
11/15/2011  12:36 PM                 0 file5.CSV2
03/18/2011  02:29 PM                10 MT.txt
03/16/2011  03:21 PM                 7 mt_file
04/11/2011  09:21 AM    <DIR>          My Documents
11/15/2011  12:36 PM                 0 somefile.csv
03/13/2009  01:46 PM    <DIR>          Start Menu
03/24/2011  01:18 PM                11 testfile.txt
09/23/2009  09:47 AM    <DIR>          Testing
03/05/2010  08:37 AM               198 xyz.bat
               9 File(s)         41,760 bytes
               7 Dir(s)   8,771,514,368 bytes free


>rename *.csv *.csv

>dir


11/15/2011  12:37 PM    <DIR>          .
11/15/2011  12:37 PM    <DIR>          ..
08/01/2011  01:59 PM            41,534 a.tmp
07/30/2011  05:44 AM    <DIR>          Desktop
11/14/2011  01:31 PM    <DIR>          Favorites
11/15/2011  12:36 PM                 0 file1.csv
11/15/2011  12:36 PM                 0 file2.csv
11/15/2011  12:36 PM                 0 file5.csv
03/18/2011  02:29 PM                10 MT.txt
03/16/2011  03:21 PM                 7 mt_file
04/11/2011  09:21 AM    <DIR>          My Documents
11/15/2011  12:36 PM                 0 somefile.csv
03/13/2009  01:46 PM    <DIR>          Start Menu
03/24/2011  01:18 PM                11 testfile.txt
09/23/2009  09:47 AM    <DIR>          Testing
03/05/2010  08:37 AM               198 xyz.bat
               9 File(s)         41,760 bytes
               7 Dir(s)   8,771,514,368 bytes free
Well, it will change the c, s, and v in all extensions, but you could use this:

Code: [Select]echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b [/s] /a-d') do (       
  set ext=%%~xA
  set ext=!ext:C=c!
  set ext=!ext:S=s!
  set ext=!ext:V=v!

  set newname=%%~nA!ext!

  echo ren %%~dpnxA !newname!
)

Use the /s if you are wanting to hit all subdirectories as well.

When you are satisfied with the result, remove the "echo" from the last line. Code: [Select]for %%A in (*.csv) do ren "%%A" "%%~nA.csv"
Before:

Code: [Select]C:\Users\Test>dir *.csv
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of C:\Users\Test

15/11/2011  18:30                16 animals.CsV
12/11/2011  10:05                44 Myquotes.cSv
11/11/2011  06:59                23 quotes.cSV
12/11/2011  10:01               525 test.CSV

After:

Code: [Select]C:\Users\Test>dir *.csv
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of C:\Users\Test

15/11/2011  18:30                16 animals.csv
12/11/2011  10:05                44 Myquotes.csv
11/11/2011  06:59                23 quotes.csv
12/11/2011  10:01               525 test.csv

The OP is trying to avoid changing the .CSV2 extension. I suppose a little modification can take care of that:

Code: [Select]for %%A in (*.csv) do if /i "%%~xA"==".csv" ren "%%A" "%%~nA.csv"
Quote from: Raven19528 on November 15, 2011, 11:59:47 AM

The OP is trying to avoid changing the .CSV2 extension. I suppose a little modification can take care of that:

No modification needed. The OP's requirement is catered for. The *.csv filespec will only find files with the extension (any case, upper lower or a mixture) ".csv" that is, a dot and the three characters (only) c , s and v - to include .csv2 it would have to be *.csv* or *.csv?

Before:

Code: [Select]C:\Users\Test>dir *.csv*
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of C:\Users\Test
15/11/2011  18:30                16 animals.CsV
12/11/2011  10:05                44 Myquotes.cSv
11/11/2011  06:59                23 quotes.cSV
12/11/2011  10:01               525 test.CSV
15/11/2011  19:01                12 test.CSV2
15/11/2011  19:01                12 testxxx.CSV2
               6 File(s)            632 bytes
               0 Dir(s)  26,632,110,080 bytes free

After:

Code: [Select]C:\Users\Test>dir *.cs*
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of C:\Users\Test

15/11/2011  18:30                16 animals.csv
12/11/2011  10:05                44 Myquotes.csv
11/11/2011  06:59                23 quotes.csv
12/11/2011  10:01               525 test.csv
15/11/2011  19:01                12 test.CSV2
15/11/2011  19:01                12 testxxx.CSV2
               6 File(s)            632 bytes
               0 Dir(s)  26,632,110,080 bytes free




Hmmm... Maybe my cmd prompt is different than yours. When I run dir *.csv I get all of the .csv2 extensions as well.

Code: [Select]Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Raven\Desktop\Code\Batch\Test>dir *.csv
 Volume in drive C is OSDisk
 Volume Serial Number is 008C-D47E

 Directory of C:\Users\Raven\Desktop\Code\Batch\Test

11/15/2011  10:18 AM                 0 New.csv
11/15/2011  10:18 AM                 0 New2.csv2
11/15/2011  10:18 AM                 0 New3.csv2
11/15/2011  10:18 AM                 0 New4.csV
               4 File(s)              0 bytes
               0 Dir(s)  193,769,201,664 bytes free
odd...

Windows 7 SP1 (64 bit, 64 bit command prompt)

(Edit - the result is the same with 32 bit command prompt)

64 bit command prompt: %windir%\System32\cmd.exe
32 bit command prompt: %windir%\SysWoW64\cmd.exe

Code: [Select]Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\Users\test>dir *.csv
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of c:\Users\test

15/11/2011  18:30                16 animals.csv
12/11/2011  10:05                44 Myquotes.csv
11/11/2011  06:59                23 quotes.csv
12/11/2011  10:01               525 test.csv
               4 File(s)            608 bytes
               0 Dir(s)  26,631,081,984 bytes free

c:\Users\test>dir *.csv*
 Volume in drive C is Win07
 Volume Serial Number is E4DB-A92A

 Directory of c:\Users\test

15/11/2011  18:30                16 animals.csv
12/11/2011  10:05                44 Myquotes.csv
11/11/2011  06:59                23 quotes.csv
12/11/2011  10:01               525 test.csv
15/11/2011  19:01                12 test.CSV2
15/11/2011  19:01                12 testxxx.CSV2
               6 File(s)            632 bytes
               0 Dir(s)  26,631,081,984 bytes free
Is your Windows 7 32 or 64 bit?
Very weird. I run a 32-bit Win7 environment. That may need to be researched as to why that little anomoly is happening.

To et_phonehome_2:

The solution to your exact issue may vary depending on the OS you are running.

I suggest opening your cmd prompt and going to the directory these files are in and typing dir *.csv  If you see .csv2 extensions, use my code, if not, use Salmon Trout's.I thought it might be to do with the filesystem having short-filename creation enabled on your system and disabled on mine, but I checked and it is enabled on mine. This is going to BUG me until I find out what is going on...


[Update] I have done a bit of Googling, and it seems that there is a bug in Win32 wildcard expansion, when the wildcard has exactly 3 chars e.g. *.csv - it finds all the .csv and then all the csv2 csvNNNN etc.

I am going to do some checking in XP 32 bit.

[Edit]

There ya go...

Code: [Select]Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator\test>dir *.csv
 Volume in drive C has no label.
 Volume Serial Number is 24FE-A31E

 Directory of C:\Documents and Settings\Administrator\test

15/11/2011  09:16 PM                 8 test.CSV
15/11/2011  09:17 PM                 8 test.csv2
15/11/2011  09:17 PM                 8 test1.CSV
15/11/2011  09:17 PM                 8 test1.csv2
15/11/2011  09:20 PM                 8 test1.csvN1
15/11/2011  09:20 PM                 8 test1.csvN2
15/11/2011  09:20 PM                 8 test1.csvN3
15/11/2011  09:20 PM                 8 test1.csvN4
15/11/2011  09:17 PM                 8 test2.CSV
15/11/2011  09:18 PM                 8 test2.csv2
15/11/2011  09:17 PM                 8 test3.CSV
15/11/2011  09:18 PM                 8 test3.csv2
              12 File(s)             96 bytes
               0 Dir(s)  132,692,742,144 bytes free

C:\Documents and Settings\Administrator\test>dir *.csvN*
 Volume in drive C has no label.
 Volume Serial Number is 24FE-A31E

 Directory of C:\Documents and Settings\Administrator\test

15/11/2011  09:20 PM                 8 test1.csvN1
15/11/2011  09:20 PM                 8 test1.csvN2
15/11/2011  09:20 PM                 8 test1.csvN3
15/11/2011  09:20 PM                 8 test1.csvN4
               4 File(s)             32 bytes
               0 Dir(s)  132,692,742,144 bytes free
So I believe your check (that the extension is just ".csv" and no longer) is vital for compatibility with all Windows versions.


dir /x shows short filenames generated for non- 8-dot-3 compatible filenames. They have .csv extensions thus win32 finds them and matches the wildcard.
Code: [Select]C:\Documents and Settings\Administrator\test>dir /x
 Volume in drive C has no label.
 Volume Serial Number is 24FE-A31E

 Directory of C:\Documents and Settings\Administrator\test

15/11/2011  09:20 PM    <DIR>                       .
15/11/2011  09:20 PM    <DIR>                       ..
15/11/2011  09:16 PM                 8              test.CSV
15/11/2011  09:17 PM                 8 TEST~1.CSV   test.csv2
15/11/2011  09:17 PM                 8              test1.CSV
15/11/2011  09:17 PM                 8 TEST1~1.CSV  test1.csv2
15/11/2011  09:20 PM                 8 TEST1~2.CSV  test1.csvN1
15/11/2011  09:20 PM                 8 TEST1~3.CSV  test1.csvN2
15/11/2011  09:20 PM                 8 TEST1~4.CSV  test1.csvN3
15/11/2011  09:20 PM                 8 TE3013~1.CSV test1.csvN4
15/11/2011  09:17 PM                 8              test2.CSV
15/11/2011  09:18 PM                 8 TEST2~1.CSV  test2.csv2
15/11/2011  09:17 PM                 8              test3.CSV
15/11/2011  09:18 PM                 8 TEST3~1.CSV  test3.csv2So more than likely, my system is searching the 8-dot-3 filenames in which case any extension starting with .csv shows up. That's a weird bug. Wonder if they are planning to patch that, or if I'm just out of date with the updates and patches.


Discussion

No Comment Found