将匹配文件的列表输出到.txt,然后移动文件,使用txt将文件稍后返回到原始文件夹 [英] Output list of matching files to .txt then move files, use txt to later return files to original folder

查看:93
本文介绍了将匹配文件的列表输出到.txt,然后移动文件,使用txt将文件稍后返回到原始文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题确实有两部分:

There is really 2 parts to this question:

  • 我有一个充满文件的文件夹,需要将其临时移到 新位置.
  • 但是以后我需要将文件移回原始文件 位置.
  • I have a folder full of files that I need to temporarily move to a new location.
  • However I will later need to move the files back to their original location(s).

我有一个文件夹,里面充满了文件和类似这样的文件夹:

C:\VIDEO\My Video 1\My Video 1.mkv
C:\VIDEO\MyVideo2\MyVideo2.mkv
C:\VIDEO\My.Video.3\My.Video.3.mkv

我需要:

1. Recursively find all *.mkv files within C:\VIDEO folder
2. Output a list of the existing Dir structure/file and folder names/paths to a .txt file
3. Then Move all *.mkv files from C:\VIDEO to another folder C:\Temp
(I do not want to retain the original folder structure during this move)

稍后,我需要:

4. Search for all *.mkv files in C:\Temp
5. Use the .txt file to help move each *.mkv file back into their original location

我想这可能需要2个单独的批处理文件.

I guess this will probably require 2 separate batch files.

这是我当前的进度:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

:: Setup
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"

:: Output Items To Txt File
for /f "delims=\" %%A in ('dir "%SourcePath%"\*.mkv') DO echo "%SourcePath%">>"%DestPath%"\output.txt

:: Move Matching Items
for /f "tokens=*" %%a IN ('dir "%SourcePath%"\*.mkv') DO move /y "%SourcePath%\%%a" "%DestPath%"

有人可以帮忙吗?

推荐答案

您想将树移到平坦的目标位置(知道以后要移回树并必须重新创建树)吗?为什么在地球上会那样做...

You want to move a tree to a flat destination (knowing you later want to move it back and have to recreate the tree)? Why on earth would one do that...

但是可以:

@echo off
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
echo @echo off > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
  ECHO move "%%~fA" "%DestPath%\"
  >> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
echo done. To move them back, execute MoveBack.bat

与其将移动的文件记录到文本文件中,然后再遍历该文件,不如构建一个恢复"脚本会更容易.

Instead of logging the moved files to a text file and later iterate over that file, it's easier to just build a "restore" script.

有关%%~修饰符的说明,请阅读for /?

For a description of the %%~ modifiers, read the output of for /?

注意:出于安全原因,我撤消了move命令.如果您确定它可以按预期工作,请删除ECHO.

NOTE: I disarmed the move command for security reasons. If you are sure it works as intended, remove the ECHO.

注意:文件夹树中可能有重复的文件名.该脚本无法解决这个问题(例如:在这种情况下,您可能会丢失数据)

Note: it's possible to have duplicate file names in a folder tree. This script does not account for that (saying: you may lose data in this case)

这篇关于将匹配文件的列表输出到.txt,然后移动文件,使用txt将文件稍后返回到原始文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆