Search code examples
windowsbatch-filecmdbatch-rename

Renaming a Folder using Batch Script


I am currently trying to Rename the Folder that was recently created, i know there is a command called REN (or) RENAME but it is used to rename a file and not the Folder.

Below is the code that i am working to achieve this.

    for %%# in ("%mask%_*") do (
     if not exist "%destination_dir%\%mask%" mkdir "%destination_dir%\%mask%"
     move /y "%%~#" "%destination_dir%\%mask%"
     if exist "%destination_dir%\%mask%" ren "%destination_dir%\%mask%_%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%"
    )

How to achieve this.?


Solution

  • In your batch code in line starting with if exist the command ren is started just with 1 parameter. So the second parameter with new name for folder/file is missing. Please note that the second parameter must be always just the new name of file/folder without path.

    Your batch code should be most likely:

    for %%# in ("%mask%_*") do (
         if not exist "%destination_dir%\%mask%" mkdir "%destination_dir%\%mask%"
         move /y "%%~#" "%destination_dir%\%mask%"
         if exist "%destination_dir%\%mask%" ren "%destination_dir%\%mask%" "%mask%_%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%"
    )