Windows script to run as a batch to replace character in list of file names

23
2014-04
  • user225817

    I have 4 files as below:

    C:\Documents and Settings\vm\AC R\test
    
    JE.BOS.20130516.CSV
    JE.BES.20130516.CSV
    KFROPN.SAVEFILE.CSV
    KFBAD.SAVEFILE.CSV
    

    I have to replace "." in the filename with "-". But the extension .CSV should remain same.

    1) I executed the below command and i got the error as below:

    for /f "tokens=1*delims=." %%i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
    

    Error:

    C:\Documents and Settings\vm\AC R\test>for /f "tokens=1*delims=." %%
    i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
    %%i was unexpected at this time.
    

    2) I removed one % before i and executed as below:

    for /f "tokens=1*delims=." %i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
    

    and got the error as below:

    C:\Documents and Settings\vmeruga\ACCESS RECS\test>for /f "tokens=1*delims=." %i
     in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
    
    C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%JEFFERIES.%BODPOS.20130
    516.CSV" "%JE-%BODPOS.20130516.CSV"
    The system cannot find the file specified.
    
    C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%JEFFERIES.%BODTRADES.20
    130516.CSV" "%JE-%BODTRADES.20130516.CSV"
    The system cannot find the file specified.
    
    C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBAROPN.%SAVEFILE.CSV"
     "%KFOPN-%SAVEFILE.CSV"
    The system cannot find the file specified.
    
    C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBARTRD.%SAVEFILE.CSV"
     "%KFBARTRD-%SAVEFILE.CSV"
    The system cannot find the file specified.
    
  • Answers
  • Karan

    Here you go, save the following as a .BAT file and execute:

    @setlocal enabledelayedexpansion&&for /f %%a in ('dir /b *.csv') do @set fn=%%~na&&set fn=!fn:.=-!&&ren "%%a" "!fn!%%~xa"
    
  • Endoro

    try this, look at the output and remove the word echo, if it is OK:

    @echo off&setlocal
    cd /d "C:\Documents and Settings\vm\AC R\test"
    for %%i in (*.*.csv) do (
        set "fname=%%~ni"
        setlocal enabledelayedexpansion
        set "nname=!fname:.=-!"
        echo rename "!fname!.CSV" "!nname!.CSV"
        endlocal
    )

  • Related Question

    script - Batch file to replace a single character in Windows filenames
  • Matt Rogish

    I have a Windows Server 2003 server that has a whole bunch of filenames that need renaming. Basically, I just need all - (hyphens) replaced with _ (underscores), no matter where they are in the filename. Assume that there are no duplicates.

    I can do this on my Mac with a little script but the files are too large and crazy to transfer to my Mac, rename, then go back to the server. Is it possible to do this in a Windows command prompt without having to download a renamer or any additional software?


  • Related Answers
  • slhck

    From the command prompt - assuming that all of your files are in the same directory:

    ONE-LINER

    for /f "tokens=* delims= " %i in ('dir /b "*.txt"') do Set LIST=%i& set LIST | ren "%~fi" "%LIST:-=_%"

    Keep in mind that this is a one shot per command prompt window. That means if you cancel this for any reason, then you'll need to open another command prompt and run again.

  • Matt Rogish

    Found it on stackoverflow:

    http://stackoverflow.com/questions/261515/batch-file-script-to-remove-special-characters-from-filenames-windows

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set re  = New RegExp
    
    re.Pattern = "[-]" ' put all characters that you want to strip inside the brackets'
    re.IgnoreCase = True
    re.Global = True
    
    If WScript.Arguments.Unnamed.Count = 1 Then
      If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then
        Recurse fso.GetFolder(WScript.Arguments.Unnamed(0))
      Else
        WScript.Echo "Folder not found."
      End If
    Else
      WScript.Echo "Please give folder name as argument 1."
    End If
    
    
    Sub Recurse(f)
      For Each sf In f.SubFolders
        Recurse sf
         WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "_")
        sf.Name = re.Replace(sf.Name, "_")
      Next
      For Each sf In f.Files
         WScript.Echo sf.Name, " -> ", re.Replace(sf.Name, "_")
    
         If sf.Name <> re.Replace(sf.Name, "_" ) Then
           sf.Name = re.Replace(sf.Name, "_")
         End If
      Next
    End Sub
    
  • slhck

    Another solution would be to use two batch files.

    File 1: run_rn.bat:

    forfiles /m "*.log" /c " cmd /c rn @file"
    

    File 2: rn.bat:

    set LIST1=%1
    set LIST2=%LIST1:_=;%
    ren %LIST1% %LIST2%
    

    The batch files have to be in the same directory like the files you want to change.

  • AdamV

    12noon has a FREE utility to do mass file renaming with full regular expression support, which is pretty cool. Rename Reg Ex info page with links to download

    I haven't used this one in anger, but have used other apps of theirs (especially display changer) and been really happy.

  • Eric Leschinski

    Batch File to replace one character in a filename with another character

    Consider using a free GUI app to hold your hand: http://www.bulkrenameutility.co.uk/Main_Intro.php

    If you must this yourself with a batch file, be super careful! Batch scripts don't have an "undo" button. If you execute your bat script which applies to all files recursively under somewhere like C:, you've just renamed every file on your computer and it will immediately stop working and fail to boot. You'll have to do an full OS reinstall. Always have a backup!

    First you'll have to decide do you want the batch file to work on a single file? To work on all files in a directory? Or do have it done recursively (all files/folders under a directory). Here are some pointers:

    Batch file to replace all underscores _ with the letter M to all files in current directory

    Put this in a batch file named change_underscores_in_this_directory.bat

    @echo off
    setlocal enabledelayedexpansion
    for %%a in (*_*) do (
      set file=%%a
      ren "!file!" "!file:_=M!"
    )
    

    Execute it, and all the files in that directory with an underscore will be changed to an 'M'.

    Use a Batch File to replace spaces with nothing (removing the spaces):

    http://stackoverflow.com/questions/11270453/how-to-remove-spaces-from-file-names-in-bulk

    Use a Batch File to replace spaces with underscores, Recursively:

    http://stackoverflow.com/questions/1613644/how-to-replace-names-recursively-via-windows-batch-operation