create a batch file with folder name provided by user

650 views Asked by At

i have created a batch file which copy the data of one folder into another. But after copying i want to give the naem as a folder name. I am not able to. Please give your suggestions. sorry for changing the question. but now i need this.

This is my code.

@echo off

:: variables

echo Backing up file

set /P source=Enter source folder:

set /P destination=Enter Destination folder:

set listfile=xcopy /L

set xcopy=xcopy /S/E/V/Q/F/H

%listfile% %source% %destination% 

echo files will be copy press enter to proceed

pause

%xcopy% %source% %destination% 

pause
1

There are 1 answers

1
sjoy On

I take it (based on comments) you want to append the date and time to the destination folder. As shown below, use one set command to convert date&time to a format you can use in a folder name (explained here) and another to set the new folder name. After the xcopy, use the move command to rename the folder. (See cited source about keeping quote marks around the %datename% folder.)

@echo off

echo Backing up file
    :: variables
    set /P source=Enter source folder:
    set /P destination=Enter Destination folder:

    :: This converts the date and time environmental variables to a
    :: format you can use in a folder name.
    set dt=%date:~7,2%-%date:~4,2%-%date:~10,4%_%time:~0,2%_%time:~3,2%_%time:~6,2%

    :: Set the new folder name appending the dt variable.  
    :: %destination% needs to be a folder name only, not a full path
    set datename=%destination%%dt%
    set listfile=xcopy /L
    set xcopy=xcopy /S/E/V/Q/F/H

    %listfile% %source% %destination% 

echo files will be copy press enter to proceed
pause

    %xcopy% %source% %destination% 
pause

    :: This renames %destination% to the %datename% you set. 
    :: Keep quote marks. %datename% can have a space depending on time
    MOVE "%destination%" "%datename%"