批处理脚本将多个文件扩展名移动到子文件夹,将参数传递给单个子例程

时间:2015-03-21 15:54:50

标签: batch-file command-line window windows-8.1

所以,我正在尝试编写批处理脚本操作,该操作处理将某些类型的文件扩展名(照片= .jpg,.png等)移动到各自的子目录。

我在柜台的问题是当我去运行循环时没有停止,我无法跟踪某些类型是否正在循环。

有没有办法在单个子程序中执行此操作?还是更好的方法?此外,如果您可以留下有用的链接,因为我真的想要更深入地了解我的编码。

我正在运行Windows 8.1

    :cleanup
set Fdocs=%CD%\1_docs
set Fphotos=%CD%\2_photos
set Fdesigns=%CD%\3_designs
set Freviews=%CD%\4_reviews
set Ffinal=%CD%\5_final
set $ext=
set action=Cleaning

:: Create a subroutine function loop : FOR , go through array file extentions and move to destination
:: FOR /F
:fMover
for %%a in (%1) do move "%CD%\*%%a" "%CD%\%2"
echo %%a
timeout 1 >nul

:: CLEAN :: documents
set $ext=.txt .doc .docx .xls .csv .ppt .pptx
call :fMover %$ext% %Fdocs%
cls
echo %action% project folder.


:: CLEAN :: images
set $ext=.jpg .png .tiff .raw .nef .crw .dng
:: for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fphotos%"
call :fMover %$ext% %Fphotos%
cls
echo %action% project folder..
timeout 1 >nul


:: CLEAN :: designs
set $ext=.psd .psb
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\2_psd"
set $ext=.ai .svg .svgz .esp
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\1_ai"
set %ext=.indd .idml 
call :fMover %$ext% %Fdesigns%
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\3_indd"
cls
echo %action% project folder...
timeout 1 >nul


cls
echo done.
timeout 2 >nul

1 个答案:

答案 0 :(得分:0)

您的批处理结构可以如下(您应该跳过预定义的子程序并从中正确返回):

    :cleanup
set "Fdocs=%CD%\1_docs"
set "Fphotos=%CD%\2_photos"
set "Fdesigns=%CD%\3_designs"
set "Freviews=%CD%\4_reviews"
set "Ffinal=%CD%\5_final"
set "$ext="
set "action=Cleaning"

:: CLEAN :: documents
set "$ext=.txt .doc .docx .xls .csv .ppt .pptx"
call :fMover "%$ext%" "%Fdocs%"
cls
echo %action% project folder.

:: CLEAN :: images
set "$ext=.jpg .png .tiff .raw .nef .crw .dng"
:: for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fphotos%"
call :fMover "%$ext%" "%Fphotos%"
cls
echo %action% project folder..
timeout 1 >nul

:: CLEAN :: designs
set "$ext=.psd .psb"
call :fMover "%$ext%" "%Fdesigns%"
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\2_psd"
set "$ext=.ai .svg .svgz .esp"
call :fMover "%$ext%" "%Fdesigns%"
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\1_ai"
set "$ext=.indd .idml" 
call :fMover "%$ext%" "%Fdesigns%"
::for %%a in (%$ext%) do move "%CD%\*%%a" "%CD%\%Fdesigns%\3_indd"
cls
echo %action% project folder...
timeout 1 >nul

cls
echo done.
timeout 2 >nul
:: skip over predefined subroutine(s) and return from script
goto :eof

:: Create a subroutine function loop : FOR , go through array file extentions and move to destination
:: FOR /F
:fMover
for %%a in (%~1) do move "%CD%\*%%~a" "%CD%\%~2"
echo %%a
timeout 1 >nul
:: return from subroutine
goto :eof

资源: