我每个月都会生成一堆.xlsx文件。我希望能够将文件批量移动到基本相同名称的文件夹。
示例:123456 Action.xlsx,123456 RC.xlsx,123456 PF.xlsx将是文件。该文件夹将是123456随机中心。
有没有办法通过命令提示符使用批处理命令或其他东西将这些文件移动到该文件夹?
以下是我尝试使用/修改的代码。
@echo off
pushd "C:\New folder"
rem Process all files in this folder separating the names at " "
for /F "tokens=1* delims=-" %%a in ('dir /B .xlsx') do (
rem At this point %%a have the name before the " " and %%b the rest after " "
rem Create the folder, if not exists
if not exist "%%a" md "%%a"
rem Move the file there
move "%%a-%%b" "%%a"
)
popd
这会创建一个名为%% a的文件夹,但不会放入任何内容。我被困住了,需要一些帮助。
答案 0 :(得分:2)
首先,欢迎使用Stack Overflow
在您提供的代码中,您尝试使用dir
的输出循环遍历文件,并立即使用空格将其拆分。取而代之的是,你应该使用for循环遍历所有以* .xlsx结尾的文件,然后在空格之前和之后制作它。
试试这个:
@echo off
pushd "C:\New folder"
FOR %%G IN (*.xlsx) DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
if not exist "%%a Random Center" md "%%a Random Center"
move "%%G" "%%a Random Center"
)
)
popd
pause
在这段代码中,我首先遍历所有以xlsx结尾的文件,循环遍历 xlsx(是一个通配符)而没有/
开关。之后,我使用/F
开关将%% G(wchich是文件名)循环为字符串。
请注意,您尝试使用-
作为分隔符,而不是。您在移动命令中犯了同样的错误。如果文件使用的是
-
而不是,那么您也应该更改我的代码中的分隔符。
修改强>
这会查看是否有一个文件夹以与文件相同的单词开头并将其移动到那里:
@echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.xlsx) DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a Random Center"
for /D %%i in (*.*) do (
for /F "tokens=1 delims= " %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause