Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Hello, there are sevral folders in d: with different names like apple, sea, blue and…

  1. User

    Hello, there are sevral folders in d: with different names like apple, sea, blue and etc. But in each folder there is a sub folder named vv, in vv folder might has a file named v.txt. I want to write a s bat file to rename all vv.txt to the front folder's name, like apple.txt, sea.txt, blue.txt and etc. Can you help me?

  2. Assistant

    Sure, here's a batch script that should achieve that:

    @echo off
    setlocal enabledelayedexpansion
    for /d %%i in (D:\*) do (
      set foldername=%%~ni
      if exist "%%i\vv\v.txt" (
        ren "%%i\vv\v.txt" "!foldername!.txt"
      )
    )
    

    Explanation:

    • @echo off turns off echoing of commands in the console.
    • setlocal enabledelayedexpansion enables delayed expansion of variables, allowing us to use ! instead of % for variable expansion in the loop.
    • The loop uses the for /d command to iterate over all directories in D:\.
    • %%~ni expands to the name of the current directory being iterated.
    • The if exist condition checks if the v.txt file exists in the vv subfolder of the current directory. If it does, the ren command renames it to the foldername.txt.
    • set and if commands require two %% signs when used in batch files. When run directly from the console, only one % is required for each variable.

    Hope this helps!