如何编写一个Windows批处理脚本从一个目录复制最新的文件?

时间:2020-11-26 02:24:00

I need to copy the newest file in a directory to a new location. So far I've found resources on the forfiles command, a date-related question here, and another related question. I'm just having a bit of trouble putting the pieces together! How do I copy the newest file in that directory to a new place?

我需要将一个目录中的最新文件复制到一个新的位置。到目前为止,我已经在forfiles命令中找到了资源,这里有一个与日期相关的问题,还有一个相关的问题。我只是有点麻烦把这些碎片拼在一起!如何将该目录中的最新文件复制到一个新位置?

8 个解决方案

#1


60  

Windows shell, one liner:

Windows外壳,一个衬套:

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT

#2


117  

The accepted answer gives an example of using the newest file in a command and then exiting. If you need to do this in a bat file with other complex operations you can use the following to store the file name of the newest file in a variable:

这个被接受的答案给出了一个在命令中使用最新文件然后退出的例子。如果您需要在bat文件中使用其他复杂操作,您可以使用以下方法将最新文件的文件名存储在一个变量中:

FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO SET NewestFile=%%I

Now you can reference %NewestFile% throughout the rest of your bat file.

现在您可以在您的bat文件的其余部分中引用%NewestFile%。

For example here is what we use to get the latest version of a database .bak file from a directory, copy it to a server, and then restore the db:

例如,这里是我们用来从目录中获取数据库.bak文件的最新版本,将其复制到服务器,然后恢复db:

:Variables
SET DatabaseBackupPath=\\virtualserver1\Database Backups

echo.
echo Restore WebServer Database
FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"

sqlcmd -U <username> -P <password> -d master -Q ^
"RESTORE DATABASE [ExampleDatabaseName] ^
FROM  DISK = N'D:\%NewestFile%' ^
WITH  FILE = 1,  ^
MOVE N'Example_CS' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example.mdf',  ^
MOVE N'Example_CS_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example_1.LDF',  ^
NOUNLOAD,  STATS = 10"

#3


10  

To allow this to work with filenames using spaces, a modified version of the accepted answer is needed:

为了让它使用空格来处理文件名,需要一个已接受的答案的修改版本:

FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" <<NewDir>> & GOTO :END
:END

#4


2  

I know you asked for Windows but thought I'd add this anyway,in Unix/Linux you could do:

我知道你想要Windows,但我想我还是要把它添加到Unix/Linux中,你可以这样做:

cp `ls -t1 | head -1` /somedir/

Which will list all files in the current directory sorted by modification time and then cp the most recent to /somedir/

哪个将列出当前目录中的所有文件按修改时间排序,然后是cp最近的/somedir/ ?

#5


2  

This will open a second cmd.exe window. If you want it to go away, replace the /K with /C.

这将打开第二个cmd。exe窗口。如果你想让它消失,用/K替换掉/K。

Obviously, replace new_file_loc with whatever your new file location will be.

显然,将new_file_loc替换为新的文件位置。

@echo off
for /F %%i in ('dir /B /O:-D *.txt') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "window title" "cmd /K copy %~1 new_file_loc"
exit /B 0

#6


2  

@echo off
set source="C:\test case"
set target="C:\Users\Alexander\Desktop\random folder"

FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4

My attempt to copy the newest file from a folder

我试图从一个文件夹复制最新的文件。

just set your source and target folders and it should work

只要设置源和目标文件夹,就可以了。

This one ignores folders, concern itself only with files

这个忽略了文件夹,只关心文件。

Recommed that you choose filetype in the DIR path changing *.* to *.zip for example

再次推荐您选择在DIR路径更改*中选择filetype。* *。邮政为例

TIMEOUT wont work on winXP I think

超时不会在winXP上工作。

#7


2  

@Chris Noe

@Chris诺

Note that the space in front of the & becomes part of the previous command. That has bitten me with SET, which happily puts trailing blanks into the value.

注意,前面的空间成为了前面命令的一部分。这让我很开心,很高兴地把后面的空格加进去。

To get around the trailing-space being added to an environment variable, wrap the set command in parens.

要绕过在环境变量中添加的跟踪空间,请在parens中封装set命令。

E.g. FOR /F %%I IN ('DIR "*.*" /B /O:D') DO (SET NewestFile=%%I)

例:FOR /F %%I IN ('DIR ' *)。*" /B /O:D') DO(设置NewestFile=% I)

#8


-2  

Bash:

Bash:

 find -type f -printf "%T@ %p \n" \
     | sort  \
     | tail -n 1  \
     | sed -r "s/^\S+\s//;s/\s*$//" \
     | xargs -iSTR cp STR newestfile

where "newestfile" will become the newestfile

哪里“newestfile”将成为newestfile ?

alternatively, you could do newdir/STR or just newdir

或者,您也可以使用newdir/STR或仅仅newdir。

Breakdown:

分解:

  1. list all files in {time} {file} format.
  2. 列出{time} {file}格式的所有文件。
  3. sort them by time
  4. 按时间排序
  5. get the last one
  6. 得到最后一个
  7. cut off the time, and whitespace from the start/end
  8. 停止时间,从开始/结束的空格。
  9. copy resulting value
  10. 复制得到的值

Important

重要的

After running this once, the newest file will be whatever you just copied :p ( assuming they're both in the same search scope that is ). So you may have to adjust which filenumber you copy if you want this to work more than once.

在运行了这一次之后,最新的文件将会是您刚刚复制的内容:p(假设它们都在相同的搜索范围内)。因此,如果您想要进行不止一次的工作,您可能需要调整您复制的filenumber。

#1


60  

Windows shell, one liner:

Windows外壳,一个衬套:

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT

#2


117  

The accepted answer gives an example of using the newest file in a command and then exiting. If you need to do this in a bat file with other complex operations you can use the following to store the file name of the newest file in a variable:

这个被接受的答案给出了一个在命令中使用最新文件然后退出的例子。如果您需要在bat文件中使用其他复杂操作,您可以使用以下方法将最新文件的文件名存储在一个变量中:

FOR /F "delims=|" %%I IN ('DIR "*.*" /B /O:D') DO SET NewestFile=%%I

Now you can reference %NewestFile% throughout the rest of your bat file.

现在您可以在您的bat文件的其余部分中引用%NewestFile%。

For example here is what we use to get the latest version of a database .bak file from a directory, copy it to a server, and then restore the db:

例如,这里是我们用来从目录中获取数据库.bak文件的最新版本,将其复制到服务器,然后恢复db:

:Variables
SET DatabaseBackupPath=\\virtualserver1\Database Backups

echo.
echo Restore WebServer Database
FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"

sqlcmd -U <username> -P <password> -d master -Q ^
"RESTORE DATABASE [ExampleDatabaseName] ^
FROM  DISK = N'D:\%NewestFile%' ^
WITH  FILE = 1,  ^
MOVE N'Example_CS' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example.mdf',  ^
MOVE N'Example_CS_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Example_1.LDF',  ^
NOUNLOAD,  STATS = 10"

#3


10  

To allow this to work with filenames using spaces, a modified version of the accepted answer is needed:

为了让它使用空格来处理文件名,需要一个已接受的答案的修改版本:

FOR /F "delims=" %%I IN ('DIR . /B /O:-D') DO COPY "%%I" <<NewDir>> & GOTO :END
:END

#4


2  

I know you asked for Windows but thought I'd add this anyway,in Unix/Linux you could do:

我知道你想要Windows,但我想我还是要把它添加到Unix/Linux中,你可以这样做:

cp `ls -t1 | head -1` /somedir/

Which will list all files in the current directory sorted by modification time and then cp the most recent to /somedir/

哪个将列出当前目录中的所有文件按修改时间排序,然后是cp最近的/somedir/ ?

#5


2  

This will open a second cmd.exe window. If you want it to go away, replace the /K with /C.

这将打开第二个cmd。exe窗口。如果你想让它消失,用/K替换掉/K。

Obviously, replace new_file_loc with whatever your new file location will be.

显然,将new_file_loc替换为新的文件位置。

@echo off
for /F %%i in ('dir /B /O:-D *.txt') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "window title" "cmd /K copy %~1 new_file_loc"
exit /B 0

#6


2  

@echo off
set source="C:\test case"
set target="C:\Users\Alexander\Desktop\random folder"

FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END
:END
TIMEOUT 4

My attempt to copy the newest file from a folder

我试图从一个文件夹复制最新的文件。

just set your source and target folders and it should work

只要设置源和目标文件夹,就可以了。

This one ignores folders, concern itself only with files

这个忽略了文件夹,只关心文件。

Recommed that you choose filetype in the DIR path changing *.* to *.zip for example

再次推荐您选择在DIR路径更改*中选择filetype。* *。邮政为例

TIMEOUT wont work on winXP I think

超时不会在winXP上工作。

#7


2  

@Chris Noe

@Chris诺

Note that the space in front of the & becomes part of the previous command. That has bitten me with SET, which happily puts trailing blanks into the value.

注意,前面的空间成为了前面命令的一部分。这让我很开心,很高兴地把后面的空格加进去。

To get around the trailing-space being added to an environment variable, wrap the set command in parens.

要绕过在环境变量中添加的跟踪空间,请在parens中封装set命令。

E.g. FOR /F %%I IN ('DIR "*.*" /B /O:D') DO (SET NewestFile=%%I)

例:FOR /F %%I IN ('DIR ' *)。*" /B /O:D') DO(设置NewestFile=% I)

#8


-2  

Bash:

Bash:

 find -type f -printf "%T@ %p \n" \
     | sort  \
     | tail -n 1  \
     | sed -r "s/^\S+\s//;s/\s*$//" \
     | xargs -iSTR cp STR newestfile

where "newestfile" will become the newestfile

哪里“newestfile”将成为newestfile ?

alternatively, you could do newdir/STR or just newdir

或者,您也可以使用newdir/STR或仅仅newdir。

Breakdown:

分解:

  1. list all files in {time} {file} format.
  2. 列出{time} {file}格式的所有文件。
  3. sort them by time
  4. 按时间排序
  5. get the last one
  6. 得到最后一个
  7. cut off the time, and whitespace from the start/end
  8. 停止时间,从开始/结束的空格。
  9. copy resulting value
  10. 复制得到的值

Important

重要的

After running this once, the newest file will be whatever you just copied :p ( assuming they're both in the same search scope that is ). So you may have to adjust which filenumber you copy if you want this to work more than once.

在运行了这一次之后,最新的文件将会是您刚刚复制的内容:p(假设它们都在相同的搜索范围内)。因此,如果您想要进行不止一次的工作,您可能需要调整您复制的filenumber。