我假设在C:\ 中有一个文件夹XYZ,其中有很多子文件夹,其中一些(或全部)有File1.txt 来创建一个文件夹并将其移动到那里,您可能需要:
@echo off
setlocal EnableDelayedExpansion
for /R "C:\XYZ\" %%A IN (File1.txt) do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpA"
rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=_!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fA" "F:\Destination\!formatted!"
)
以上代码以F:\Destination\C_XYZ_etc\File1.txt 格式生成路径。如 cmets 中所述,您可能还需要:
@echo off
setlocal EnableDelayedExpansion
for /R "C:\XYZ\" %%A IN (File1.txt) do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpA"
rem /* In this %%~dpA, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fA" "F:\Destination\!formatted!"
)
格式为F:\Destination\CXYZETC\File1.txt。
如果您要检查多个文件:(使用set /p [来自用户的输入]):
@echo off
setlocal EnableDelayedExpansion
:files
set /p files=Please enter the files you want to check separated by spaces. Quote all filenames:
if not defined files (goto:files)
:loop
rem Loop through user input (filenames):
for %%A IN (%files%) do (
for /R "C:\XYZ\" %%B IN ("%%A") do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpB"
rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fB" "F:\Destination\!formatted!"
)
)
带参数(更简单):
@echo off
setlocal EnableDelayedExpansion
:argument_check
if [%1] == [] (echo Action requires arguments^^! Please rerun from cmd specifying arguments^^! Remember to quote each filename^^! & exit /b 1)
:loop
rem Loop through arguments (filenames):
for %%A IN (%*) do (
for /R "C:\XYZ\" %%B IN ("%%A") do (
rem /* Find path of file excluded filename (dp=drive and path): */
set "drive_path=%%~dpB"
rem /* In this %%~dpB, replace '\' and ':\' according to OP's requirements: */
set "formatted=!drive_path:\=!" & set "formatted=!formatted::=!"
rem /* Make the folder: */
md "F:\Destination\!formatted!"
rem /* Move the file there: */
move "%%~fB" "F:\Destination\!formatted!"
)
)