如何与遍历文件夹(在批)移动文件夹? [英] how to move folders with a loop over folders (in batch)?

查看:222
本文介绍了如何与遍历文件夹(在批)移动文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我尝试在外壳移动循环中的文件,但我的code是行不通的。

I try to move files inside a loop in shell but my code is not working.

for /D %%F in (*) do (
   if "%%F" NEQ "%directoryToPutFilesIn%" (
     move /y "%%F" "%directoryToPutFilesIn%"
  )
)

在测试它的时间,我意识到这是因为%% F被指向的文件夹,因此该文件无法移动。

After hours of testing it, I realized it's because %%F is pointing to the folder, hence the file cannot be moved.

坏的解决方案:

我做了工作,并证实了我的怀疑的方法是保存在其他变量%% F的值,并使用在下一回合该变量移动的文件。请注意,什么是需要如下%precedentFile%的初始化的第一个回合。

The way I made it work and confirmed my suspicions is by saving the value of %%F in another variable and using that variable on next turn to move the file. Note, what is following needs an initialisation of %precedentFile% for the first turn.

for /D %%F in (*) do (
move /y "%precedentFile%" "%directoryToPutFilesIn%"
   if "%%F" NEQ "%directoryToPutFilesIn%" (
     move /y "%%F" "%directoryToPutFilesIn%"
     set precedentFile=%%F
  )

问题:

这解决方案是不实际的,感觉冤屈。有没有一种方法,以适应我现在的code做到这一点,或者干脆另一种方式?

This solution is not practical and feels wrongs. Is there a way to adapt my current code to do it, or simply another way?

推荐答案

尝试下面code从一个文件夹移动到另一个文件中的批处理脚本:

Try below code to move files from one folder to another in batch script:

for /f %%a in ('dir /a:-D /b') do move /Y "%%~fa" "%directoryToPutFilesIn%"

说明:

dir /a:-D /b : This command will list all files in the directory 
move /Y "%%~fa" "%directoryToPutFilesIn%" : This will move all files in the directory where this command is executed to the destination you have mentioned.
%%~fa : This command will get full qualified path of the file with it's name.

试试下面code移动目录:
下面的命令将在该命令被执行以提供目的地的路径移动目录。在此,这将是H:\\驱动器,相应地更改它

Try Below code Move directories : Below command will move directories in the Path where this command is executed to the destination provided. In this that will be H:\ Drive, Change it accordingly

for /D %%b in (*) do move /Y "%%~fb" "H:\"

这篇关于如何与遍历文件夹(在批)移动文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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