Announcement

Collapse
No announcement yet.

Batch Script to search and rename a file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch Script to search and rename a file

    911


    I have run a command to search and rename a file. I have successfully search and rename the file if the directory name does not spaces eg c:\document else it says the syntax of the command is incorrect. The file was located else-where in local drives.

    actual command

    C:\>for /f "delims=*" %a in ('dir local.ini /s /b') do rename %a local.bak

    C:\>rename C:\documents\local.ini local.bak

    C:\>rename C:\documents\New Folder\local.ini local.bak
    The syntax of the command is incorrect.


    please help me to accomplish this goal


    Thanks
    Last edited by totoy bato; 25th August 2009, 08:50. Reason: typo error

  • #2
    Re: Batch Script to search and rename a file

    Google would have told you everything...

    Put " " around paths with spaces in them.

    Code:
    C:\>rename "C:\documents\New Folder\local.ini" local.bak
    Gareth Howells

    BSc (Hons), MBCS, MCP, MCDST, ICCE

    Any advice is given in good faith and without warranty.

    Please give reputation points if somebody has helped you.

    "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

    "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

    Comment


    • #3
      Re: Batch Script to search and rename a file

      Yes, wrap quotes around the name or around the full path

      Code:
      C:\Documents and Settings\user>CD /d "c:\"
      C:\>for /f "delims=" %a in ('dir "local.ini" /s /b 2^>nul') do @rename "%~a" "local.bak"
      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Batch Script to search and rename a file

        You could also use the 8.3 naming scheme by using:
        'dir local.ini /x /s /b'

        the /x will show "My Documents" as something like "MYDOCU~1"

        Hobie

        Comment

        Working...
        X