如您所知,您可以使用该功能forfile
:
forfiles /M "fileA" /C "cmd /c copy @path C:\Documents\archives\@fname_ @fdate"
我正在尝试使用@ftime
,但它包含:
我想用空格或“”替换的内容。
我该怎么做呢?
答案1
对于使用forfile
并删除:
并且删除引号:
@echo off && setlocal EnableDelayedExpansion
set "_=C:\Documents\archives" && for /f tokens^=1-7 %%i in (
'%__APPDIR__%forfiles.exe /M *fileA*.log /C "cmd /C echo @path !_! @fname @fdate @ftime @ext"'
)do && for /f tokens^=^1^ ^delims^=^" %%I in ('echo/%%~i')do set "_L=%%~l_%%~m" && call set "_L=!_L::=!" && (
set "_target=%%~j\_%%~k_!_L!" && call set _target=!_target:^"=!&& cmd /v /c copy "%%~I" "!_target!.%%~o" )
goto :EOF
- 采用常规格式的相同代码:
@echo off && setlocal EnableDelayedExpansion
set "_=C:\Documents\archives"
for /f tokens^=1-7 %%i in ('%__APPDIR__%forfiles.exe /M *fileA*.log /C "cmd /C echo @path !_! @fname @fdate @ftime @ext"'
)do for /f tokens^=^1^ ^delims^=^" %%I in ('echo/%%~i')do (
set "_L=%%~l_%%~m"
call set "_L=!_L::=!"
set "_target=%%~j\_%%~k_!_L!"
call set _target=!_target:^"=!
cmd /v /c copy "%%~I" "!_target!.%%~o"
)
goto :EOF
到copy
和rename
最后的目标文件修改日期/时间,我建议你使用wmic.exe
以及循环for
命令而不是forfiles
:
@echo off && setlocal EnableDelayedExpansion
for /f tokens^=* %%i in ('dir *fileA*.* /b /a:-d /o-d /t:w'
)do set "_F=%%~fi" && for /f "tokens=1 delims=+." %%d in ('
%__APPDIR__%wbem\wmic.exe datafile where name^="!_F:\=\\!" get "LastModified"^|%__APPDIR__%findstr.exe [0-9]'
)do set "_d=%%~d" && call copy "%%~fi" "C:\Documents\archives\%%~ni_!_d:~0,4!!_d:~4,2!!_d:~6,2!_!_d:~8,2!.!_d:~10,2!%%~xi"
%__APPDIR__%timeout.exe -1 & endlocal && goto :EOF
- 采用常规格式的相同代码:
@echo off
setlocal EnableDelayedExpansion
for /f tokens^=* %%i in ('dir *fileA*.* /b /a:-d /o-d /t:w')do (
set "_F=%%~fi"
for /f "tokens=1 delims=+." %%d in (
'%__APPDIR__%wbem\wmic.exe datafile where name^="!_F:\=\\!" get "LastModified"^|%__APPDIR__%findstr.exe [0-9]'
)do (
set "_d=%%~d"
call copy "%%~fi" "C:\Documents\archives\%%~ni_!_d:~0,4!!_d:~4,2!!_d:~6,2!_!_d:~8,2!.!_d:~10,2!%%~xi"
))
%__APPDIR__%timeout.exe -1 & endlocal && goto :EOF
命令和输出示例wmic
获取整个数据文件:
%__APPDIR__%wmic.exe datafile where name='G:\\SUPER_USER\\Q1525416\\xy-_fileA_yx.log' get * /format:list
- 输出:
AccessMask=4294967295
Archive=TRUE
Caption=g:\super_user\q1525416\xy-_filea_yx.log
Compressed=FALSE
CompressionMethod=
CreationClassName=CIM_LogicalFile
CreationDate=20200214134852.240000+***
CSCreationClassName=Win32_ComputerSystem
CSName=LESMA_MANCA
Description=g:\super_user\q1525416\xy-_filea_yx.log
Drive=g:
EightDotThreeFileName=g:\super_user\q1525416\xy-_fi~1.log
Encrypted=FALSE
EncryptionMethod=
Extension=log
FileName=xy-_fileA_yx
FileSize=973
FileType=Text Document
FSCreationClassName=Win32_FileSystem
FSName=FAT32
Hidden=FALSE
InstallDate=20200214134852.240000+***
InUseCount=
LastAccessed=20200214000000.000000+***
LastModified=20200214130618.000000+***
Manufacturer=
Name=g:\super_user\q1525416\xy-_filea_yx.log
Path=\super_user\q1525416\
Readable=TRUE
Status=OK
System=FALSE
Version=
Writeable=TRUE