CMD脚本:复制文件并重命名

huangapple go评论61阅读模式
英文:

CMD script: copy a file and rename it

问题

以下是您提供的 CMD 脚本的翻译:

@echo off
echo 源路径
set /P folder1=
echo 文件名
set /P file1=
echo 目标路径
set /P folder2=

if %folder1% == %folder2% goto path
if exist %folder2%\%file1% goto exist

set file2=%file1%
set "file2=%~dpn1_copy%~x1"

copy %folder1%\%file1% %folder2%\%file2%
goto end

:exist
echo 文件存在于目标文件夹中
goto end

:path
echo 目标路径和源路径相同
goto end

:end

请注意,我已经移除了代码部分的翻译,只提供了您要求的翻译好的部分。如果您需要进一步的帮助,请随时告诉我。

英文:

I have to write a simple CMD script, its task is to copy a file from one folder to another. User have to enter source and destination path and the name of the file. Script has to add "_copy" to the name of copied file. I only know how to add a suffix to the file after file extension but it's not what I need.

I was trying it with %~n1 and %~x1 but it didn't work properly. Can someone help me solve this problem?

@echo off
echo Source path
set /P folder1=
echo File name
set /P file1=
echo Destination path
set /P folder2=

if %folder1% == %folder2% goto path
if exist %folder2%\%file1% goto exist

set file2=%file1%
set "file2=%~dpn1_copy%~x1"

copy %folder1%\%file1% %folder2%\%file2%
goto end

:exist
echo File exist in destination folder
goto end

:path
echo Destination and source paths are the same
goto end

:end

答案1

得分: 0

%1 (输入的第一个参数) 从未被设置,您需要设置它。在这种情况下,使用 call 来设置它。

@echo off
set /p "sourcep=源路径:"
set /p "sourcef=文件:"
set /p "destp=目标路径:"
if "%sourcep%" == "%destp%" echo 源和目标路径相同 & goto :EOF
if exist "%destp%\%sourcef%" echo %sourcef% 已经存在于 %destp% & goto :EOF
call :work "%sourcef%"
goto :EOF
:work
if not exist "%destp%\%~n1_copy%~x1" (
      copy "%~f1" "%destp%\%~n1_copy%~x1"
) else (
      echo "%destp%\%~n1_copy%~x1" 已经存在
)

您也可以使用搜索/替换方法,但这将包括使用 delayedexpansion 或为变量添加一些转义:

@echo off & setlocal enabledelayedexpansion

set /p "sourcep=源路径:"
set /p "sourcef= 文件:"
set /p "destp=目标路径:"
set "ext=%sourcef:*.=%"
set "file=!sourcef:.%ext%=!"
if "%sourcep%" == "%destp%" echo 源和目标路径相同 & goto :EOF
if exist "%destp%\%sourcef%" echo %sourcef% 已经存在于 %destp% & goto :EOF
if exist "%destp%\%file%_copy.%ext%" echo "%destp%\%file%_copy.%ext%" 已经存在 & goto :EOF
copy "%sourcep%\%sourcef%" "%destp%\%file%_copy.%ext%"
英文:

%1 (first argument from input) never get's set, you need to set it. In this case use call to set it.

@echo off
set /p "sourcep=Source path: "
set /p "sourcef=File: "
set /p "destp=Destination path: "
if "%sourcep%" == "%destp%" echo source and destination is the same & goto :EOF
if exist "%destp%\%sourcef%" echo %sourcef% already exists in %destp% & goto :EOF
call :work "%sourcef%"
goto :EOF
:work
if not exist "%destp%\%~n1_copy%~x1" (
      copy "%~f1" "%destp%\%~n1_copy%~x1"
) else (
      echo "%destp%\%~n1_copy%~x1" already exists
)

You can also use the search/replace method, but that would include either using delayedexpansion or by adding some escaping to your variables:

@echo off & setlocal enabledelayedexpansion

set /p "sourcep=Source path: "
set /p "sourcef= File: "
set /p "destp=Destination path: "
set "ext=%sourcef:*.=%"
set "file=!sourcef:.%ext%=!
if "%sourcep%" == "%destp%" echo source and destination is the same & goto :EOF
if exist "%destp%\%sourcef%" echo %sourcef% already exists in %destp% & goto :EOF
if exist "%destp%\%file%_copy.%ext%" echo "%destp%\%file%_copy.%ext%" already exists & goto :EOF
copy "%sourcep%\%sourcef%" "%destp%\%file%_copy.%ext%"

huangapple
  • 本文由 发表于 2023年5月14日 23:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定