【问题标题】:I want to write a script to copy a file from a directory to another directory and rename it我想编写一个脚本来将文件从一个目录复制到另一个目录并重命名它
【发布时间】:2014-11-19 07:45:36
【问题描述】:

我想写一个批处理脚本,我双击它,它应该将一个文件从一个目录复制到另一个目录并将其重命名为fileX,知道目标目录中有一个同名的文件.

所以我想做的是删除已经存在的fileX并复制另一个。

我该怎么做,请评论脚本以便我理解。

【问题讨论】:

  • 如果您查看copy /?,您会看到有一个开关允许您覆盖现有文件。所以你不需要删除现有文件,只需复制它即可。

标签: batch-file scripting


【解决方案1】:

您可以使用命令xcopy 来执行此操作。例如:

echo [Copy files]
echo * Create the file xcopy_EXCLUDE.txt in order to ignore some file and/or directory.
echo .au3 > xcopy_Exclude.txt
echo .pspimage >> xcopy_Exclude.txt
echo \psp\ >> xcopy_Exclude.txt
echo *   - ignore all .au3 files
echo *   - ignore all .pspimage files
echo *   - ignore the \psp\ directory
echo * The file xcopy_EXCLUDE.txt is created.
echo.
echo * Copy additional files with xcopy.
xcopy "%FOLDER_SRC%" "%FOLDER_OUT%" /E /H /Y /EXCLUDE:xcopy_Exclude.txt
echo * Files and directory are copied.
echo.
echo * Delete xcopy_Exclude.txt.
del xcopy_Exclude.txt

【讨论】:

    【解决方案2】:

    如果您愿意研究 powershell。这是您要运行的命令。

    # Path is the location to the original file
    # Destination is where you want it to go
    # -force indicates that it shoud replace a file if it finds it and not prompt you first
    Copy-Item -Path "c:\temp\original.txt" -Destination "c:\folder2\filex.txt" -force
    
    # For more help:
    Get-Help Copy-Item
    

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2016-08-17
      • 2015-07-19
      • 2013-06-01
      • 2020-06-22
      • 2014-08-12
      • 2013-10-24
      • 2011-10-24
      相关资源
      最近更新 更多