简体   繁体   English

如何自动创建名称为当天的文件夹并将现有文件和子文件夹移动到该文件夹​​中?

[英]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 文件。

  1. 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

  2. Move existing files or folders into folder 13 .将现有文件或文件夹移动到文件夹13 中

    Example:例子:

    Before batch file execution the current folder contains:在批处理文件执行之前,当前文件夹包含:

    • Folder a文件夹a
    • Folder b文件夹 b
    • File c文件 c
    • File d文件d

    After batch file execution the current folder should contain:批处理文件执行后,当前文件夹应包含:

    • 17 17
      • Folder a文件夹a
      • Folder b文件夹 b
      • File c文件 c
      • File d文件d

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 ifif /?
  • help for or for /? help forfor /?
  • help set or set /? help setset /?
  • help move or move /? help movemove /?
  • help mkdir or help md or mkdir /? help mkdirhelp mdmkdir /? 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:在批处理文件执行之前,当前文件夹包含:

  • Folder a文件夹a
  • Folder b文件夹 b
  • Folder c文件夹 c
  • Folder MONTH 11文件夹 MONTH 11
  • Folder d文件夹 d
  • File e文件e
  • .... ....

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、...

  • Folder 17文件夹 17
    • Folder L1文件夹 L1
      • Folder a文件夹a
      • Folder b文件夹 b
      • Folder c文件夹 c
  • Folder MONTH 11文件夹 MONTH 11
  • Folder d文件夹 d
  • File e文件e
  • .... ....

3. Move folder "17" into folder has name "MONTH 11": 3. 将文件夹“17”移动到名称为“MONTH 11”的文件夹中:

Before batch file execution the current folder contains:在批处理文件执行之前,当前文件夹包含:

  • Folder 17文件夹 17
    • Folder L1文件夹 L1
      • Folder a文件夹a
      • Folder b文件夹 b
      • Folder c文件夹 c
  • Folder MONTH 11文件夹 MONTH 11
  • Folder d文件夹 d
  • File e文件e
  • .... ....

After batch file execution the current folder should contain:批处理文件执行后,当前文件夹应包含:

  • Folder MONTH 11文件夹 MONTH 11
    • folder 17文件夹 17
      • Folder L1文件夹 L1
        • Folder a文件夹a
        • Folder b文件夹 b
        • Folder c文件夹 c
  • Folder d文件夹 d
  • File e文件e
  • .... ....

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.

相关问题 如何以多个文件的名称创建文件夹,将文件移动到该文件夹中,然后重命名所有这些文件 - How to create a folder the name of multiple files, to move files into that folder, and then rename all of those files 将子文件夹中的文件移动到文件夹并重命名Windows提示符 - Move files in subfolders to a folder and rename windows prompt 如何从文件名的一部分创建批处理文件夹,将文件移动到具有修改名称的文件夹中? - How do I create batch folder from part of file name, move files into folder with a modified name? 如何在批处理 (.bat) 文件中创建名称为当前日期的文件夹 - How to create a folder with name as current date in batch (.bat) files 根据部分文件名批量创建文件夹并将文件移动到文件夹 - Batch create folder based on partial file name and move files to folder 如何从文件名创建文件夹并将文件移动到文件夹中? - How do I create folder from file name and move files into folder? 如何用WinRAR创建ZIP文件,且ZIP文件的名称为当前文件夹的名称? - How to create a ZIP file with WinRAR with name of ZIP file being name of current folder? 用相同的名称创建新的子文件夹并将文件移动到新文件夹 - Create new subfolder with same name and move files to the new folder 批量创建基于部分文件名的文件夹并将文件移动到该文件夹​​中 - Batch create folders based on part of file name and move files into that folder 根据部分文件名创建文件夹并将文件移动到创建的文件夹中 - Create Folders based on part of file name and move files into the created folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM