windows 使用时间戳创建备份和重命名的批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17996936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batchfile to create backup and rename with timestamp
提问by CustomX
I have the following network path to copy the file to an archive folder. It copies File 1
from Folder
to Archive
but I would like to add these 2 adjustments that won't work.
我有以下网络路径可以将文件复制到存档文件夹。它File 1
从Folder
to复制,Archive
但我想添加这两个不起作用的调整。
- Rename
File 1-1
toFile 1 - date + time
- Running it now displays a cmd box with the copy code, is it possible to run it in the background or have a loading screen to show the progress?
- 重命名
File 1-1
为File 1 - date + time
- 现在运行它会显示一个带有复制代码的 cmd 框,是否可以在后台运行它或有一个加载屏幕来显示进度?
For my code I followed this exampleto change the name to a date.
对于我的代码,我按照此示例将名称更改为日期。
copy "F:\Folder\File 1.xlsx" "F:\Folder\Archive\File 1-1.xlsx"
/f "tokens=1-5 delim s=/ " %%d in ("%date%") do rename "F:\Folder example 2.xlsx" "F:\Folder\File example %%e-%%f-%%g.xlsx"
回答by Endoro
try this:
尝试这个:
ren "File 1-1" "File 1 - %date:/=-% %time::=-%"
回答by foxidrive
See if this is what you want to do:
看看这是否是你想要做的:
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
copy "F:\Folder\File 1.xlsx" "F:\Folder\Archive\File 1 - %stamp%.xlsx"
回答by KuriaNdungu
I've modified Foxidrive's answer to copy entire folders and all their contents. this script will create a folder and backup another folder's contents into it, including any subfolders underneath.
我修改了 Foxidrive 的答案以复制整个文件夹及其所有内容。此脚本将创建一个文件夹并将另一个文件夹的内容备份到其中,包括下面的所有子文件夹。
If you put this in say an hourly scheduled task you need to be careful as you could fill up your drive quickly with copies of your original folder. Before bitbucket etc i was using as similar script to save my code offline.
如果你把它放在一个每小时计划的任务中,你需要小心,因为你可以用原始文件夹的副本快速填满你的驱动器。在 bitbucket 等之前,我使用类似的脚本来离线保存我的代码。
@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=YourPrefixHere_%YYYY%%MM%%DD%@%HH%%Min%
rem you could for example want to create a folder in Gdrive and save backup there
cd C:\YourGoogleDriveFolder
mkdir %stamp%
cd %stamp%
xcopy C:\FolderWithDataToBackup\*.* /s
回答by user3820643
Renames all .pdf
files based on current system date. For example a file named Gross Profit.pdf
is renamed to Gross Profit 2014-07-31.pdf
. If you run it tomorrow, it will rename it to Gross Profit 2014-08-01.pdf
.
.pdf
根据当前系统日期重命名所有文件。例如,一个名为的文件Gross Profit.pdf
被重命名为Gross Profit 2014-07-31.pdf
. 如果您明天运行它,它将重命名为Gross Profit 2014-08-01.pdf
.
You could replace the ?
with the report name Gross Profit
, but it will only rename the one report. The ?
renames everything in the Conduit
folder. The reason there are so many ?
, is that some .pdf
s have long names. If you just put 12 ?
s, then any name longer than 12 characters will be clipped off at the 13th character. Try it with 1 ?
, then try it with many ?
s. The ?
length should be a little longer or as long as the longest report name.
您可以将 替换为?
报告名称Gross Profit
,但它只会重命名一份报告。在?
重命名的一切Conduit
文件夹中。之所以有这么多?
,是因为有些.pdf
s的名字很长。如果您只输入 12 ?
s,那么任何超过 12 个字符的名称都将在第 13 个字符处被剪掉。用 1 试试?
,然后用很多?
s试试。该?
长度应长一点或者只要最长的报告名称。
@ECHO OFF
SET NETWORKSOURCE=\flcorpfile\shared\"SHORE Reports"14\Conduit
REN %NETWORKSOURCE%\*.pdf "????????????????????????????????????????????????? %date:~-4,4%-%date:~-10,2%-%date:~7,2%.pdf"
回答by krowe
Yes, to make it run in the background create a shortcut to the batch file and go into the properties. I'm on a Linux machine ATM but I believe the option you are wanting is in the advanced tab.
是的,要使其在后台运行,请创建批处理文件的快捷方式并进入属性。我在 Linux 机器 ATM 上,但我相信您想要的选项位于高级选项卡中。
You can also run your batch script through a vbs script like this:
您还可以通过 vbs 脚本运行批处理脚本,如下所示:
'HideBat.vbs
CreateObject("Wscript.Shell").Run "your_batch_file.bat", 0, True
This will execute your batch file with no cmd window shown.
这将在不显示 cmd 窗口的情况下执行您的批处理文件。