none
DOS Remane File with Space and Wildard RRS feed

  • Question

  • I recieve multiple files every couple weeks with a naming convention that identifies for me the content of the file and date association. For example:

    BA CAS 2015-10-28.txt, IV CAS 2015-10-28.txt, BA PSNE 2015-10-28.txt, IV PSNE 2015-10-28.txt, etc.

    Notice the space between the 2nd and 5th character and the date at the end. Using a short batch command I would like to rename all the files  to include the space but drop the date.

    REN "BA CAS 2015-10-28.txt" "BA CAS.txt" will give me the appropriate end state, but as the date changes with every new set, coding without a wildcard for the date won't work.

    Is there a way to include the wildcard in the commend to get the intended result?

    i.e. REN BA?CAS*.txt "BA CAS.txt"

    Appreciate any help



    • Edited by Hartsell.b Saturday, November 7, 2015 1:54 PM
    Saturday, November 7, 2015 1:51 PM

Answers

  • Is there a way to include the wildcard in the commend to get the intended result?

    i.e. REN BA?CAS*.txt "BA CAS.txt"

    It the old-fashioned, weird and wonderful world of Console commands you could do it with an arcane batch file like the one below. To activate it you would need to change echo ren to ren.

    @echo off
    SetLocal EnableDelayedExpansion
    for /F "delims=" %%a in ('dir /b *.txt') do (
      set NewName=%%~na
      set NewName=!NewName:~0,6!%%~xa
      if not exist "!NewName!" echo ren "%%a" "!NewName!"
    )

    Saturday, November 7, 2015 6:25 PM

All replies

  • Is there a reason you want to use a DOS?, this would be very easy with PowerShell.
    Saturday, November 7, 2015 3:00 PM
  • Actually never heard of PowerShell. Have some experience with simple batch scripts from days gone by and figured a couple of minutes of development would save me from manually renaming these files every other week. Going to lookup PowerShell now.
    Saturday, November 7, 2015 3:29 PM
  • Are these names always all on one line?


    \_(ツ)_/

    Saturday, November 7, 2015 4:44 PM
  • The following will remove all dates.

     'BA CAS 2015-10-28.txt, IV CAS 2015-10-28.txt, BA PSNE 2015-10-28.txt, IV PSNE 2015-10-28.txt' -replace '\d+-\d+-\d+'


    \_(ツ)_/

    Saturday, November 7, 2015 4:48 PM
  • Actually never heard of PowerShell. Have some experience with simple batch scripts from days gone by and figured a couple of minutes of development would save me from manually renaming these files every other week. Going to lookup PowerShell now.

    Well, welcome to the wonderful world of PowerShell. DOS is a very old way of doing things.  If you are going to learn something it's very much worth picking up PowerShell as it is the new defacto standard.

    There are many ways to do what you are asking for in PowerShell. Here is the way I would do it.

    Get-ChildItem -Path * | Rename-Item -newname { "$($_.name.substring(0,$_.name.lastindexof(' '))).txt" }
     
    Saturday, November 7, 2015 4:51 PM
  • The following will remove all dates.

     'BA CAS 2015-10-28.txt, IV CAS 2015-10-28.txt, BA PSNE 2015-10-28.txt, IV PSNE 2015-10-28.txt' -replace '\d+-\d+-\d+'


    \_(ツ)_/

    Using jrv's example, you could do what you are asking like this

    Get-ChildItem -Path * | Rename-Item -newname { "$($_ -replace ' \d+-\d+-\d+')" }

    • Proposed as answer by Frederik Long Sunday, November 8, 2015 9:49 PM
    Saturday, November 7, 2015 5:04 PM
  • DOS is a very old way of doing things.

    Correction. There is no more "DOS". See the beginning of my article:

    Windows IT Pro: Break Your Batch Habit and Move to PowerShell


    -- Bill Stewart [Bill_Stewart]


    Saturday, November 7, 2015 5:42 PM
    Moderator
  • Is there a way to include the wildcard in the commend to get the intended result?

    i.e. REN BA?CAS*.txt "BA CAS.txt"

    It the old-fashioned, weird and wonderful world of Console commands you could do it with an arcane batch file like the one below. To activate it you would need to change echo ren to ren.

    @echo off
    SetLocal EnableDelayedExpansion
    for /F "delims=" %%a in ('dir /b *.txt') do (
      set NewName=%%~na
      set NewName=!NewName:~0,6!%%~xa
      if not exist "!NewName!" echo ren "%%a" "!NewName!"
    )

    Saturday, November 7, 2015 6:25 PM