用于具有不同文件名的驱动器中给定扩展类型的文件复制的批处理脚本

时间:2014-08-07 12:34:16

标签: batch-file

我是批处理脚本的新手,

我想要一个脚本来执行以下工作! 在像(C,D或E)这样的驱动器中搜索所有.txt文件,并希望在相同位置使用不同名称的相同副本,如.txt_backup07082014。

在整个驱动器的任何位置,它都会找到.txt文件,除了文件名的更改外,脚本应该按原样复制文件。

让我们假设在执行脚本之前有以下文件夹结构

C(云台) -

       Test (Folder)--
                    TestResults.txt
                    TestCases.txt
                    TestExecution.txt
        Dev (Folder)--
                    DevCodeFiles.txt
                    Configurations.txt
                    ToolsUsed.txt

       Automation (Folder)-- 
                       Sample.txt
             Sanity (Folder)-- <<<This folder is subfolder for Automation>>>
                            Scripts.txt

执行脚本后,我正在寻找下面的解决方案

C(云台) -

       Test (Folder)--
                    TestResults.txt
                    TestResults.txt_07082014
                    TestCases.txt
                    TestCases.txt_07082014
                    TestExecution.txt
                    TestExecution.txt_07082014
        Dev (Folder)--
                    DevCodeFiles.txt
                    DevCodeFiles.txt_07082014
                    Configurations.txt
                    Configurations.txt_07082014
                    ToolsUsed.txt
                    ToolsUsed.txt_07082014

       Automation (Folder)-- 
                       Sample.txt
                       Sample.txt_07082014
             Sanity (Folder)-- <<<This folder is subfolder for Automation>>>
                            Scripts.txt
                            Scripts.txt_07082014

2 个答案:

答案 0 :(得分:0)

只需将pushd f:\更改为您需要的驱动器:

@echo off
setlocal
pushd f:\
@echo off
pushd "%temp%"
makecab /D RptFileName=~.rpt /D InfFileName=~.inf /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do set "current_date=%%c%%b%%e" && del ~.*

set current_date=%current_date:Jan=01%
set current_date=%current_date:Feb=02%
set current_date=%current_date:Mar=03%
set current_date=%current_date:Apr=04%
set current_date=%current_date:May=05%
set current_date=%current_date:Jun=06%
set current_date=%current_date:Jul=07%
set current_date=%current_date:Aug=08%
set current_date=%current_date:Sep=09%
set current_date=%current_date:Oct=10%
set current_date=%current_date:Nov=11%
set current_date=%current_date:Dec=12%



for /f "tokens=* delims=" %%f in ('dir /s /b /r /a:-d *.txt') do (
    copy /y "%%~f" "%%~f_%current_date%"
)

popd
endlocal

答案 1 :(得分:0)

这应该这样做:将c:\更改为您要处理的驱动器。首先在示例文件上测试它。

此代码的前四行将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec变量。

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%%MM%%YYYY%"

for /r "c:\" %%a in (*.txt) do copy "%%a" "%%a_%datestamp%"
Pause