0

As you know you can use the function forfile:

forfiles /M "fileA" /C "cmd /c copy @path C:\Documents\archives\@fname_ @fdate"

I'm trying to use @ftime, but it contains : which I want to replace with space or "".

How do I do this?

0

For using forfile and remove : and also, removing quotes:


@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

  • Same code in conventional formatting:
@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


To copy and rename the target files with last modified date/time, I suggest that you use the wmic.exe and a looping for command instead of 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

  • Same code in conventional formatting:
@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

An example of command and output wmic to get the entire data file:


%__APPDIR__%wmic.exe datafile where name='G:\\SUPER_USER\\Q1525416\\xy-_fileA_yx.log' get * /format:list

  • Outputs:
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

WMIC.exe /?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.