目录C:/dat 有三个这样的文件,2008a.txt,2009b.txt,2008c.txt, 其中2008是代表年份,可变的,后面的文件名不可变
我想实现这样的操作
1.把dat下面所有的文件的年份取出来,写到year.txt文件中,
a 2008
b 2009
c 2008
2。然后把文件名的年份去掉变成a.txt,b.txt,c.txt
3。拷贝这些文件到backup目录下,并加上当时的系统日期,如a_20080924.txt,b_20080924.txt,c_20080924.txt
@echo off
set sp=c:/dat
set bp=c:/backup
for /f "tokens=2 delims==" %%t in ('wmic os get localdatetime /value') do (set dt=%%t)
set dt=%dt:~,8%
for /f "tokens=*" %%a in ('dir /b /a-d %sp%') do (call :doit %%a)
goto :eof
:doit
set fn=%1
echo %fn:~4,1% %fn:~,4%>>./year.txt
ren %sp%/%fn% %fn:~4,20%
copy %sp%/%fn:~4,20% %bp%/%fn:~4,1%_%dt%.txt
=========================================================
@echo off
setlocal enabledelayedexpansion
set SrcDir=C:/dat
set DstDir=C:/dat/backup
set DstFile=C:/dat/year.txt
set Today=%date:~6,4%%date:~0,2%%date:~3,2%
for %%a in ("%SrcDir%/*.txt") do (
set file=%%~na
set year=!file:~0,4!
set name=!file:~4!
if !year! lss 9999 (
>>"%DstFile%" echo.!name! !year!
ren "%%a" "!name!%%~xa"
copy "%SrcDir%/!name!%%~xa" "%DstDir%"
ren "%DstDir%/!name!%%~xa" "!name!_%Today%%%~xa"
)
)
转自http://topic.csdn.net/u/20080924/17/034609cc-4596-441e-8607-50d4e07a5864.html