batch file to copy some files and changing their n

2019-09-05 02:10发布

I need to create a batch file to copy some files. I have several files on a folder, having all of them a date as part of the filename, and only some of them have the current day date. i.e.:

backupone_2013-11-19.zip
backupone_2013-11-18.zip
backupone_2013-11-17.zip

backuptwo_2013-11-19.zip
backuptwo_2013-11-18.zip
backuptwo_2013-11-17.zip

I need to duplicate (copy in the same folder) only those files named with current date, naming the new files with the format (left 8 chars)&"last".zip

Using the same example, it should be:

backupone_2013-11-19.zip
backupone_2013-11-18.zip
backupone_2013-11-17.zip

backuptwo_2013-11-19.zip
backuptwo_2013-11-18.zip
backuptwo_2013-11-17.zip

backupone_last.zip
backuptwo_last.zip

where the "last" files are related to the current day

I know how to select the files to copy (those with today´s date), but I can extract their left filename letters to copy them as new files


Update:

I've been trying the following script:

These are the files

C:\temp\test>dir *.zip /B
backupone__2013-11-18__16-18-53(Completo).zip
backupone__2013-11-19__16-18-53(Completo).zip
backupthree__2013-11-18__16-18-53(Completo).zip
backupthree__2013-11-19__16-18-53(Completo).zip
backuptwo__2013-11-19__16-18-53(Completo).zip

this is the full script

@echo off
cls
rem ----------------------------------------------

REM #### Today´s date
rem DATE FORMAT YYYY-MM-DD
SET fechahoy=%date:~6,4%-%date:~3,2%-%date:~0,2%
echo -----------------Debug: fechahoy
echo %fechahoy%
echo.

REM #### directory where the files are saved
cd "C:\temp\test\"
echo -----------------Debug: dir 
echo  "*%fechahoy%*".zip 
echo.

dir /B /A-D "*%fechahoy%*".zip 
echo.

for /f "Tokens=1 delims=_" %%a in ("C:\temp\test\*%fechahoy%*.zip") do (
 echo %%a_Last.zip 
)

and here is the result:

-----------------Debug: fechahoy
2013-11-19

-----------------Debug: dir
 "*2013-11-19*".zip

backupone__2013-11-19__16-18-53(Completo).zip
backupthree__2013-11-19__16-18-53(Completo).zip
backuptwo__2013-11-19__16-18-53(Completo).zip

C:\temp\test\*2013-11-19*.zip_Last.zip

Any idea?

Any help will be greatly appreciated

4条回答
我想做一个坏孩纸
2楼-- · 2019-09-05 02:45

Here is one way:

 for /f "Tokens=1 delims=_" %%a in ( 
      "backuptwo_2013-11-19.zip") do ( echo %%a_Last.zip )

You could also use SET to extract the 8 or 9 chars.

 SET bu=backuptwo_2013-11-19.zip & set bu=%bu:~0,9% & echo %bu%_Last.zip
查看更多
forever°为你锁心
3楼-- · 2019-09-05 02:49

This uses your code and changes the last loop. It will only echo the ren commands so remove the echo and run it to make it functional.

@echo off
cls
rem ----------------------------------------------

REM #### Today´s date
rem DATE FORMAT YYYY-MM-DD
SET fechahoy=%date:~6,4%-%date:~3,2%-%date:~0,2%
echo -----------------Debug: fechahoy
echo %fechahoy%
echo.

REM #### directory where the files are saved
cd "C:\temp\test\"
echo -----------------Debug: dir 
echo  "*%fechahoy%*".zip 
echo.


for /f "Tokens=1,* delims=_" %%a in ('dir /B /A-D "*%fechahoy%*.zip" ') do (
 echo ren "%%a_%%b" "%%a_Last.zip"
)
查看更多
在下西门庆
4楼-- · 2019-09-05 02:56

Try this:

#! /bin/bash

# some configs

dirFrom=.
dirTo=.
suffix=$(date +%F).zip
newsuffix=last.zip

# end configs

# iterate over files to copy in the from directory
for file in $dirFrom/*$suffix
do
  # copy this file
  basename=$(basename $file)
  cp "$file" "${dirTo%/}/${basename%$suffix}$newsuffix"
done
查看更多
混吃等死
5楼-- · 2019-09-05 03:05

I'd suggest to use realdatetime tool to get time in a determined format or year, month, date apart (please check once again realdatetime.exe documentation for correct command line paramenters).

@echo off
setlocal enabledelayedexpansion

for /F %%a in ('realdatetime.exe -f YYYY') do set curYear=%%a
for /F %%a in ('realdatetime.exe -f MM') do set curMonth=%%a
for /F %%a in ('realdatetime.exe -f DD') do set curDate=%%a

for %%a in (*_????-??-??.zip) do (
    for /F "tokens=1,2,3,4 delims=_-." %%m in ("%%a") do (
        echo %%m_%%n-%%o-%%p
        if %%n==!curYear! if %%o==!curMonth! if %%p==!curDate! (
            copy /Y %%a %%m_last.zip
        )
    )
)

If the date format is known (eg: YYYY-MM-DD) try this code to set today date (no need for realdatetime.exe):

set curYear=%date:~0,4%
set curMonth=%date:~6,2%
set curDate=%date:~9,2%
查看更多
登录 后发表回答