所以我有下面的批处理文件。
@ECHO OFF
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
for %%i in (%*) do (
move "%%~i" "C:\%FolderName%"
)
因此,当我将多个文件拖放到批处理文件中时,它将获取包含我拖放的文件的文件夹的名称,并在C:\上创建一个具有相同名称的新文件夹,然后移动文件到C:\
的新文件夹中示例:包含我要移动的文件的文件夹是名称Shop。然后文件夹Shop位于
... \地面\钟\塔\店
使用批处理文件将在C:\
处创建一个新文件夹名称Shop实施例
C:\店
当我一次拖放大约100个文件时,批处理文件可以正常工作。问题是,当我一次拖放300个文件时,它会返回错误,表示"文件名或扩展名太长"。我可以手动移动文件,所以我知道文件名太长也不会导致文件。
然后我创建了一个新的批处理文件,认为这可能是move
命令或我的for循环的问题。所以我在下面编写了批处理文件。
@echo off
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
MOVE "%cd%\*.*" "C:\%FolderName%"
现在,上面的第二个批处理文件与第一个批处理文件大致相同。就像第二个批处理文件一样,我只需要从我要移动的文件夹中拖放一个文件,它会将第一个文件夹中的所有文件移动到C:\的新文件夹中,即使第一个文件夹有1000个文件。
我的问题如果我一次拖放太多文件,为什么第一批文件会失败。使用第二个批处理文件工作,所以它不能是因为move
命令还是它?由于我将所有文件从一个文件夹移动到另一个文件夹,因此第二个批处理文件符合我的需要,并且想知道第二个批处理文件是否存在任何问题或更好的方法。
答案 0 :(得分:0)
如果我没弄错的话,您正在尝试使用父文件夹作为名称创建一个文件夹。所以在你再次在另一篇文章中提出同样的问题之前,我会编写你想要的内容。
@echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul ( mkdir "%destination%\%parent%" )
move "%%~i" "%destination%%parent%"
)
此外,具有许多参数的命令在前面的代码中不是问题。
例如:使用以下代码编写批处理文件
@echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul (echo mkdir "%destination%\%parent%")
echo move "%%~i" "%destination%%parent%"
)
将其命名为test-move.bat
并将以下250个参数的长命令行放在cmd:
test-move.bat TestFile001.txt TestFile002.txt TestFile003.txt TestFile004.txt TestFile005.txt TestFile006.txt TestFile007.txt TestFile008.txt TestFile009.txt TestFile010.txt TestFile011.txt TestFile012.txt TestFile013.txt TestFile014.txt TestFile015.txt TestFile016.txt TestFile017.txt TestFile018.txt TestFile019.txt TestFile020.txt TestFile021.txt TestFile022.txt TestFile023.txt TestFile024.txt TestFile025.txt TestFile026.txt TestFile027.txt TestFile028.txt TestFile029.txt TestFile030.txt TestFile031.txt TestFile032.txt TestFile033.txt TestFile034.txt TestFile035.txt TestFile036.txt TestFile037.txt TestFile038.txt TestFile039.txt TestFile040.txt TestFile041.txt TestFile042.txt TestFile043.txt TestFile044.txt TestFile045.txt TestFile046.txt TestFile047.txt TestFile048.txt TestFile049.txt TestFile050.txt TestFile051.txt TestFile052.txt TestFile053.txt TestFile054.txt TestFile055.txt TestFile056.txt TestFile057.txt TestFile058.txt TestFile059.txt TestFile060.txt TestFile061.txt TestFile062.txt TestFile063.txt TestFile064.txt TestFile065.txt TestFile066.txt TestFile067.txt TestFile068.txt TestFile069.txt TestFile070.txt TestFile071.txt TestFile072.txt TestFile073.txt TestFile074.txt TestFile075.txt TestFile076.txt TestFile077.txt TestFile078.txt TestFile079.txt TestFile080.txt TestFile081.txt TestFile082.txt TestFile083.txt TestFile084.txt TestFile085.txt TestFile086.txt TestFile087.txt TestFile088.txt TestFile089.txt TestFile090.txt TestFile091.txt TestFile092.txt TestFile093.txt TestFile094.txt TestFile095.txt TestFile096.txt TestFile097.txt TestFile098.txt TestFile099.txt TestFile100.txt TestFile101.txt TestFile102.txt TestFile103.txt TestFile104.txt TestFile105.txt TestFile106.txt TestFile107.txt TestFile108.txt TestFile109.txt TestFile110.txt TestFile111.txt TestFile112.txt TestFile113.txt TestFile114.txt TestFile115.txt TestFile116.txt TestFile117.txt TestFile118.txt TestFile119.txt TestFile120.txt TestFile121.txt TestFile122.txt TestFile123.txt TestFile124.txt TestFile125.txt TestFile126.txt TestFile127.txt TestFile128.txt TestFile129.txt TestFile130.txt TestFile131.txt TestFile132.txt TestFile133.txt TestFile134.txt TestFile135.txt TestFile136.txt TestFile137.txt TestFile138.txt TestFile139.txt TestFile140.txt TestFile141.txt TestFile142.txt TestFile143.txt TestFile144.txt TestFile145.txt TestFile146.txt TestFile147.txt TestFile148.txt TestFile149.txt TestFile150.txt TestFile151.txt TestFile152.txt TestFile153.txt TestFile154.txt TestFile155.txt TestFile156.txt TestFile157.txt TestFile158.txt TestFile159.txt TestFile160.txt TestFile161.txt TestFile162.txt TestFile163.txt TestFile164.txt TestFile165.txt TestFile166.txt TestFile167.txt TestFile168.txt TestFile169.txt TestFile170.txt TestFile171.txt TestFile172.txt TestFile173.txt TestFile174.txt TestFile175.txt TestFile176.txt TestFile177.txt TestFile178.txt TestFile179.txt TestFile180.txt TestFile181.txt TestFile182.txt TestFile183.txt TestFile184.txt TestFile185.txt TestFile186.txt TestFile187.txt TestFile188.txt TestFile189.txt TestFile190.txt TestFile191.txt TestFile192.txt TestFile193.txt TestFile194.txt TestFile195.txt TestFile196.txt TestFile197.txt TestFile198.txt TestFile199.txt TestFile200.txt TestFile201.txt TestFile202.txt TestFile203.txt TestFile204.txt TestFile205.txt TestFile206.txt TestFile207.txt TestFile208.txt TestFile209.txt TestFile210.txt TestFile211.txt TestFile212.txt TestFile213.txt TestFile214.txt TestFile215.txt TestFile216.txt TestFile217.txt TestFile218.txt TestFile219.txt TestFile220.txt TestFile221.txt TestFile222.txt TestFile223.txt TestFile224.txt TestFile225.txt TestFile226.txt TestFile227.txt TestFile228.txt TestFile229.txt TestFile230.txt TestFile231.txt TestFile232.txt TestFile233.txt TestFile234.txt TestFile235.txt TestFile236.txt TestFile237.txt TestFile238.txt TestFile239.txt TestFile240.txt TestFile241.txt TestFile242.txt TestFile243.txt TestFile244.txt TestFile245.txt TestFile246.txt TestFile247.txt TestFile248.txt TestFile249.txt TestFile250.txt