批量复制重命名,重复提示
Batch Copy Rename, Prompt on duplicate
对批处理文件和一般编程非常陌生,因此需要任何帮助;
我正在尝试根据用户输入复制和重命名文件,然后打开新文件。我的问题是,如果有重复项,我希望它重新提示用户输入不同的名称。现在我有:
SET /P "dname=Type new sheet name, then press Enter:"
COPY "O:\master.xlsx" "O:\%dname%.xlsx" /V /-Y
START /D "O:" %dname%.xlsx
这适用于复制和重命名,但如果有重复,我会得到提示 'do you want to copy Y/N',但如果我拒绝,它不会要求使用不同的名称。
谢谢!
编辑 1:
我现在有这个:
SET /P "dname=Type sheet name, then press Enter:"
IF EXIST "O:\%dname%.xlsx" (
:RETRY
SET /P "dname=Error! There is already a file named %dname%. Please choose a new unique name:"
IF EXIST "O:\%dname%.xlsx" (
GOTO :RETRY
ELSE GOTO :COPY
)
) ELSE GOTO :COPY
:COPY
COPY "O:\master.xlsx" "O:\%dname%.xlsx" /V /-Y
而且它似乎工作可靠。有没有更好的方法来 streamline/optimize 这个循环?或者这就足够了吗?
我会这样做:
@echo off
setlocal
set "oldname=master"
:loop
set "newname=%oldname%"
set /p "newname=New Name: "
if exist "%newname%.xlsx" (
echo %newname%.xlsx already exists. Chose another name.
goto :loop
)
COPY "%oldname%.xlsx" "%newname%.xlsx"
对批处理文件和一般编程非常陌生,因此需要任何帮助; 我正在尝试根据用户输入复制和重命名文件,然后打开新文件。我的问题是,如果有重复项,我希望它重新提示用户输入不同的名称。现在我有:
SET /P "dname=Type new sheet name, then press Enter:"
COPY "O:\master.xlsx" "O:\%dname%.xlsx" /V /-Y
START /D "O:" %dname%.xlsx
这适用于复制和重命名,但如果有重复,我会得到提示 'do you want to copy Y/N',但如果我拒绝,它不会要求使用不同的名称。 谢谢!
编辑 1: 我现在有这个:
SET /P "dname=Type sheet name, then press Enter:"
IF EXIST "O:\%dname%.xlsx" (
:RETRY
SET /P "dname=Error! There is already a file named %dname%. Please choose a new unique name:"
IF EXIST "O:\%dname%.xlsx" (
GOTO :RETRY
ELSE GOTO :COPY
)
) ELSE GOTO :COPY
:COPY
COPY "O:\master.xlsx" "O:\%dname%.xlsx" /V /-Y
而且它似乎工作可靠。有没有更好的方法来 streamline/optimize 这个循环?或者这就足够了吗?
我会这样做:
@echo off
setlocal
set "oldname=master"
:loop
set "newname=%oldname%"
set /p "newname=New Name: "
if exist "%newname%.xlsx" (
echo %newname%.xlsx already exists. Chose another name.
goto :loop
)
COPY "%oldname%.xlsx" "%newname%.xlsx"