从文件夹名称中删除最后 2 个变量并合并具有相似名称的文件夹
Removing the last 2 variables from a folder name and merging folders with a similar names
我找到了这段代码,它完成了我想做的事情的 50%。
setlocal enabledelayedexpansion enableextensions
for /d %%f in (*) do (
set N=%%f
set N=!N:~0,-2!
ren "%%f" "!N!"
)
我正在使用的文件夹结构有 5 位项目编号,后跟一个下划线和一个修订字母。上面的代码删除了下划线和修订字母,只保留项目编号作为名称。然而,当有一个项目编号有多个修订时,它只是忽略它们。
文件夹名称示例:
12000_A
10200_A
10200_B
10200_C
50000_A
运行 代码后的文件夹名称结果。
12000
10200
10200_B
10200_C
50000
我希望将具有多个修订的文件夹中的所有文件合并到 1 个父文件夹中。
预期结果:
12000
10200 (folder contains A,B,C versions of the files)
50000
这可能吗?提前致谢
ren
command fails if the new file or directory name already exist, so you cannot use it to merge directories. The move
command 也不是很有用,因为当您尝试将目录移动到现有目录时,它会将整个源目录而不是其内容移动到目标目录。
您可以选择 xcopy
command to first copy and merge the source directories, together with the rd
command 最终删除成功复制的源目录然后:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Iterate through all the directories in the current location:
for /D %%I in (".\?????_?") do (
rem // Split off the first `_` and everything behind from the directory name:
for /F "delims=_ eol=_" %%J in ("%%~nxI") do (
rem // Copy the contents of the source directory into the new one:
xcopy /E /I /Y "%%~I" "%%~dpI%%J" > nul && (
rem // Remove the source directory upon copying success:
ECHO rd /S /Q "%%~I"
)
)
)
endlocal
exit /B
经过测试,删除大写 ECHO
命令 以便真正删除目录。
我找到了这段代码,它完成了我想做的事情的 50%。
setlocal enabledelayedexpansion enableextensions
for /d %%f in (*) do (
set N=%%f
set N=!N:~0,-2!
ren "%%f" "!N!"
)
我正在使用的文件夹结构有 5 位项目编号,后跟一个下划线和一个修订字母。上面的代码删除了下划线和修订字母,只保留项目编号作为名称。然而,当有一个项目编号有多个修订时,它只是忽略它们。
文件夹名称示例:
12000_A
10200_A
10200_B
10200_C
50000_A
运行 代码后的文件夹名称结果。
12000
10200
10200_B
10200_C
50000
我希望将具有多个修订的文件夹中的所有文件合并到 1 个父文件夹中。
预期结果:
12000
10200 (folder contains A,B,C versions of the files)
50000
这可能吗?提前致谢
ren
command fails if the new file or directory name already exist, so you cannot use it to merge directories. The move
command 也不是很有用,因为当您尝试将目录移动到现有目录时,它会将整个源目录而不是其内容移动到目标目录。
您可以选择 xcopy
command to first copy and merge the source directories, together with the rd
command 最终删除成功复制的源目录然后:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Iterate through all the directories in the current location:
for /D %%I in (".\?????_?") do (
rem // Split off the first `_` and everything behind from the directory name:
for /F "delims=_ eol=_" %%J in ("%%~nxI") do (
rem // Copy the contents of the source directory into the new one:
xcopy /E /I /Y "%%~I" "%%~dpI%%J" > nul && (
rem // Remove the source directory upon copying success:
ECHO rd /S /Q "%%~I"
)
)
)
endlocal
exit /B
经过测试,删除大写 ECHO
命令 以便真正删除目录。