[英]How to automatically create folder with name being current day and move existing files and subfolders into that folder?
I need a simple tool to do my repeating job.我需要一个简单的工具来完成我的重复工作。 So I wrote some commands in text file and saved the file as *.bat file.
所以我在文本文件中写了一些命令并将文件保存为 *.bat 文件。
Create a new folder with current day in month as folder name.创建一个新文件夹,以月份中的当前日期作为文件夹名称。
Example: Today is 13-11-14 (format date: dd-mm-yy)示例:今天是 13-11-14(格式日期:dd-mm-yy)
MD=%date:~0.2%
Created folder: 13创建的文件夹: 13
Move existing files or folders into folder 13 .将现有文件或文件夹移动到文件夹13 中。
Example:例子:
Before batch file execution the current folder contains:在批处理文件执行之前,当前文件夹包含:
After batch file execution the current folder should contain:批处理文件执行后,当前文件夹应包含:
I drew an image to show what I need.我画了一个图像来显示我需要的东西。
I don't know how to write that second part of the task.我不知道如何编写任务的第二部分。
Which commands are needed to move all files and subfolders in current folder to a new folder with name of current day in month?需要哪些命令将当前文件夹中的所有文件和子文件夹移动到一个名为当前日期的新文件夹?
Okay, here is the small batch file for this task with comments and an extra check.好的,这是此任务的小批处理文件,带有注释和额外检查。
@echo off
rem Get name of batch file in case of being in current directory, too. It is
rem verified if the batch file executed is really in the current directory.
set "BatchFileName="
if "%~dp0"=="%CD%\" set "BatchFileName=%~nx0"
rem Get current day of month. Works only with date formats beginning with day.
set "Day=%DATE:~0,2%"
rem Create the directory if it does not already exist.
if not exist "%Day%" mkdir "%Day%"
rem Move all subdirectories except the directory of current day.
for /D %%D in (*) do if not "%%D"=="%Day%" move /Y "%%D" "%Day%"
rem Move all files except running batch file.
for %%F in (*) do if not "%%F"=="%BatchFileName%" move /Y "%%F" "%Day%"
rem Delete the two environment variables used for this task.
set "BatchFileName="
set "Day="
The help of the used commands can be read in a command prompt window after entering:使用命令的帮助可以在命令提示符窗口中输入后阅读:
help if
or if /?
help if
或if /?
help for
or for /?
help for
或for /?
help set
or set /?
help set
或set /?
help move
or move /?
help move
或move /?
help mkdir
or help md
or mkdir /?
help mkdir
或help md
或mkdir /?
or md /?
md /?
It's working great, thanks Mofi, but not perfect :D for me.效果很好,感谢 Mofi,但对我来说并不完美:D。
I try but can't write more step.我尝试但不能写更多的步骤。 Can you edit a little bit?
你能稍微编辑一下吗?
1. Delete all empty folders 1.删除所有空文件夹
2. Move EXACTLY folders what I need to move (a, b, c): 2. 完全移动我需要移动的文件夹(a、b、c):
I have exactly 7 folders to move.我正好有 7 个文件夹要移动。
Before batch file execution the current folder contains:在批处理文件执行之前,当前文件夹包含:
After batch file execution the current folder should contain:批处理文件执行后,当前文件夹应包含:
If folder L1 already exist, create new as folder name L2, L3, L4, ...如果文件夹 L1 已存在,则新建文件夹名称为 L2、L3、L4、...
3. Move folder "17" into folder has name "MONTH 11": 3. 将文件夹“17”移动到名称为“MONTH 11”的文件夹中:
Before batch file execution the current folder contains:在批处理文件执行之前,当前文件夹包含:
After batch file execution the current folder should contain:批处理文件执行后,当前文件夹应包含:
Edit by Mofi莫菲编辑
For the next time, never ask something new in an answer of a question.下一次,不要在回答问题时提出新的问题。 Create instead a new question with a reference (link) to the former, similar question.
而是创建一个新问题,其中包含对前一个类似问题的引用(链接)。 This gives you and other users the possibility to earn more reputation.
这使您和其他用户有可能赢得更多声誉。
I post here below the batch file code for your new requirements although it is not possible for you to accept my answer now.尽管您现在无法接受我的回答,但我在此处发布了您的新要求的批处理文件代码。
@echo off
rem Delete empty folders. Command rmdir does not delete a folder without
rem parameter /S if it contains a file or a subfolder. The error message
rem output if folder is not empty is redirected to NUL device to suppress it.
rmdir "Folder a" 2>nul
rmdir "Folder b" 2>nul
rmdir "Folder c" 2>nul
rem Get directory for current day in current month.
set "DayFolder=MONTH %DATE:~3,2%\%DATE:~0,2%"
rem Create the day folder and if necessary automatically also the month folder.
if not exist "%DayFolder%" mkdir "%DayFolder%"
rem Check existence of L* subfolders in day folder.
set "Number=1"
:Loop
if exist "%DayFolder%\L%Number%" set /A "Number+=1" & goto Loop
rem Create the next L* subfolder in day folder.
mkdir "%DayFolder%\L%Number%"
rem Move the remaining folders into new L* subfolder in day folder.
if exist "Folder a" move /Y "Folder a" "%DayFolder%\L%Number%"
if exist "Folder b" move /Y "Folder b" "%DayFolder%\L%Number%"
if exist "Folder c" move /Y "Folder c" "%DayFolder%\L%Number%"
rem Delete the last L* day folder if it does not contain anything.
rmdir "%DayFolder%\L%Number%" 2>nul
rem Delete the current day folder in current month if still empty.
rmdir "%DayFolder%" 2>nul
rem Delete the current month folder if still empty.
rmdir "MONTH %DATE:~3,2%" 2>nul
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.