Search code examples
batch-filebatch-rename

add text to end of folder name, batch


First post here :) I am new to creating batch files and have created the below batch file (which took me about 3 weeks to figure out, LOL). Basically, when we get a new assignment we copy the template folder and then rename it the next unused folder name, eg. if the last folder was 15-P0028 the next one would be 15-P0029. the batch works perfectly! But, sometimes we want to add a client name to the end of the folder name, eg. 15-P0029 Brown. I want the batch to pause for user input after renaming the folder the next in sequence so we can enter a client name or just hit enter to insert the new folder without the client name. I think using the set /p would work but I have no idea how to use it properly. Can anyone help me with this, it would be greatly appreciated.

@echo off
setlocal enableDelayedExpansion
cd /d C:\Users\jbrown\Desktop\Test
set I=2
:NextI
if /I %I% LEQ 999 set II=0%I%
if /I %I% LEQ 99 set II=00%I%
if /I %I% LEQ 9 set II=000%I%
if not exist "15-P!II!" (
xcopy /s /e /i "15-P0000-Template" "15-P!II!"
goto :eof
)
set /a I+=1
if /i %I% LSS 9999 goto NextI
) 

Solution

  • yep, you got it, you need the /p "prompt" command. Something like this would work

    if not exist "15-P!II!" (
        set /p "client_name=OPTIONAL Enter a client name and press <ENTER>: "
    
        xcopy /s /e /i "15-P0000-Template" "15-P!II!!client_name!"
        goto :eof
    )
    

    we don't need to do any extra processing to check if the user entered a value. If they simply pressed enter client_name should just be an empty string. so adding an empty string onto the end won't affect the name