如何根据用户输入文件扩展名将某些文件移动到另一个文件夹,并在原始目录中为每个移动的文件创建快捷方式?



我想创建一个要求文件扩展名和路径的.bat文件,然后扫描.bat文件本身所在的目录和子文件夹,查找提供扩展名的所有文件,并将它们移动到指定路径,同时在原始位置创建.lnk。

例如,.bat的行为如下:

输入文件扩展名:mkv
输入新路径:e:movies

然后,它会扫描.bat文件所在的文件夹和子文件夹,并将所有 .mkv 文件移动到新路径,同时在原始源文件夹中为每个已移动的文件创建一个.lnk。

我还想将原始源结构复制到新的源结构中。例如,如果我有一个.mkv文件d:star trekmovie1a.mkv我希望有e:moviesstar trekmovie1a.mkv然后d:star trekmovie1a.lnk..lnk应链接到驱动器e:上文件夹中的移动文件。

任何想法将不胜感激。


作为第一步,我想创建一个简单的批处理,要求用户提供扩展名,然后只列出具有该扩展名的所有文件。

我写道:

@echo off
set /p type= File type?
dir *.type > list.txt

但这行不通。有什么建议吗?


我在这里找到了一些代码如何复制目录结构,但只包含某些文件(使用 Windows 批处理文件(,我只是在学习它。我已经写下了这个简单的改编,但我不明白为什么最后一个回显没有输出我想要的东西,例如整个目标路径和文件名。我知道我一定在做什么愚蠢的事情!

@echo off
cls
setlocal enabledelayedexpansion
set SOURCE_DIR=K:source
set DEST_DIR=K:dest
set FILENAMES_TO_COPY=*.txt
for /R "%SOURCE_DIR%" %%F IN (%FILENAMES_TO_COPY%) do (
if exist "%%F" (
echo source: %%F
set FILE_DIR=%%~dpF
set FILE_INTERMEDIATE_DIR=!FILE_DIR:%SOURCE_DIR%=!
set FILE_NAME_EXT=%%~nxF
set FILE_DIR
set FILE_INTERMEDIATE_DIR
set FILE_NAME_EXT
xcopy /S /I /Y /V "%%F" "%DEST_DIR%!FILE_INTERMEDIATE_DIR!"
echo destination "%DEST_DIR%!FILE_INTERMEDIATE_DIR%FILE_NAME_EXT!"
pause
)
)

对于下面的批处理文件,请从Optimal X Downloads - Freeware Utilities下载非常小的ZIP文件快捷方式.zip,将ZIP文件解压缩到临时目录,读取此免费软件实用程序的文件ReadMe.txtShortcut.exe移动到与批处理文件相同的文件夹中。

以下是此任务的注释批处理代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "%~dp0Shortcut.exe" (
echo ERROR: Missing Shortcut.exe in "%~dp0".
goto EndBatch
)
:GetExtension
set "FileExt="
set /P "FileExt=Enter file extension: "
rem Has the user not entered any string?
if not defined FileExt goto GetExtension
rem Remove all double quotes.
set "FileExt=%FileExt:"=%"
rem Is there nothing left after removing double quotes?
if not defined FileExt goto GetExtension
rem Is the first character a dot, then remove it.
if "%FileExt:~0,1%" == "." set "FileExt=%FileExt:~1%"
rem Is there nothing left after removing the dot?
if not defined FileExt goto GetExtension
rem Does the entered file extension string contain a
rem character which is not allowed in a file extension?
set "TempVar="
for /F "eol=| delims=*./:<>?|" %%I in ("%FileExt%") do set "TempVar=%%I"
if not "%TempVar%" == "%FileExt%" goto GetExtension
rem Don't allow moving *.lnk shortcut files.
if /I "%FileExt%" == "lnk" goto GetExtension

:GetSourcePath
set "SourcePath=%~dp0"
set /P "SourcePath=Enter source path: "
set "SourcePath=%SourcePath:"=%"
if not defined SourcePath goto GetSourcePath
rem Replace all forward slashes by backslashes.
set "SourcePath=%SourcePath:/=%"
rem Remove a trailing backslash.
if "%SourcePath:~-1%" == "" set "SourcePath=%SourcePath:~0,-1%"
rem Recreate the environment variable if the entered source directory path
rem was just a backslash to reference root directory of current drive.
if not defined SourcePath set "SourcePath="
rem Does the entered source path string contain a
rem character which is not allowed in a folder path?
set "TempVar="
for /F "eol=| delims=*<>?|" %%I in ("%SourcePath%") do set "TempVar=%%I"
if not "%TempVar%" == "%SourcePath%" goto GetSourcePath
rem Determine full qualified source folder path.
for %%I in ("%SourcePath%") do set "SourcePath=%%~fI"
rem Remove once again a trailing backslash which can occur
rem if the source folder is the root folder of a drive.
if "%SourcePath:~-1%" == "" set "SourcePath=%SourcePath:~0,-1%"
rem The source directory must exist.
if not exist "%SourcePath%" (
echo/
echo ERROR: Source directory "%SourcePath%" does not exist.
echo/
goto GetSourcePath
)

:GetTargetPath
set "TargetPath="
set /P "TargetPath=Enter target path: "
if not defined TargetPath goto GetTargetPath
set "TargetPath=%TargetPath:"=%"
if not defined TargetPath goto GetTargetPath
rem Replace all forward slashes by backslashes.
set "TargetPath=%TargetPath:/=%"
rem Remove a trailing backslash.
if "%TargetPath:~-1%" == "" set "TargetPath=%TargetPath:~0,-1%"
rem Recreate the environment variable if the entered target directory path
rem was just a backslash to reference root directory of current drive.
if not defined TargetPath set "TargetPath="
rem Does the entered target path string contain a
rem character which is not allowed in a folder path?
set "TempVar="
for /F "eol=| delims=*<>?|" %%I in ("%TargetPath%") do set "TempVar=%%I"
if not "%TempVar%" == "%TargetPath%" goto GetTargetPath
rem Determine full qualified target folder path.
for %%I in ("%TargetPath%") do set "TargetPath=%%~fI"
rem Remove once again a trailing backslash which can occur
rem if the target folder is the root folder of a drive.
if "%TargetPath:~-1%" == "" set "TargetPath=%TargetPath:~0,-1%"
rem Is the target path identical to source folder path?
if /I "%SourcePath%" == "%TargetPath%" (
echo/
echo ERROR: Target path cannot be the source path.
echo/
goto GetTargetPath
)

rem Does the specified target folder exist?
if exist "%TargetPath%" goto GetPathLength
rem Ask user if target folder should be created or not.
echo/
echo ERROR: Folder "%TargetPath%" does not exist.
echo/
%SystemRoot%System32choice.exe /C YN /N /M "Create path (Y/N)? "
echo/
if errorlevel 2 goto GetTargetPath
md "%TargetPath%" 2>nul
if exist "%TargetPath%" goto GetPathLength
echo ERROR: Could not create "%TargetPath%".
echo/
goto GetTargetPath

rem Determine length of source path (path of batch file).
:GetPathLength
setlocal EnableDelayedExpansion
set "PathLength=0"
:GetLength
if not "!SourcePath:~%PathLength%!" == "" set /A "PathLength+=1" & goto GetLength
rem For the additional backslash after source folder path.
set /A PathLength+=1
endlocal & set "PathLength=%PathLength%"

rem Move the non-hidden files with matching file extension with exception
rem of the batch file and Shortcut.exe. Use FOR /F with a DIR command line
rem instead of FOR /R to get this code working also on FAT32 and ExFAT
rem drives and when target folder is a subfolder of the batch file.
set "FileCount=0"
for /F "eol=| delims=" %%I in ('dir "%SourcePath%*.%FileExt%" /A-D-H /B /S 2^>nul') do if /I not "%%I" == "%~f0" if /I not "%%I" == "%~dp0Shortcut.exe" call :MoveFile "%%I"
set "PluralS=s"
if %FileCount% == 1 set "PluralS="
echo Moved %FileCount% file%PluralS%.
goto EndBatch

:MoveFile
rem Do not move a file on which there is already a shortcut file
rem with same file name in source directory because then it would
rem not be possible to create a shortcut file for the moved file.
if exist "%~dpn1.lnk" goto :EOF
rem Get entire path of source file and remove path of batch file.
set "SourceFilePath=%~dp1"
call set "RelativePath=%%SourceFilePath:~%PathLength%%%"
rem Create target file path for this file.
set "TargetFilePath=%TargetPath%%RelativePath%"
echo TargetFilePath=%TargetFilePath%
rem Remove trailing backslash if there is one.
if "%TargetFilePath:~-1%" == "" set "TargetFilePath=%TargetFilePath:~0,-1%"
rem Create the target folder independent on its existence
rem and verify if the target folder really exists finally.
md "%TargetFilePath%" 2>nul
if not exist "%TargetFilePath%" (
echo ERROR: Could not create folder "%TargetFilePath%".
goto :EOF
)
rem Move the file to target folder and verify if that was really successful.
rem On error remove a just created target folder not containing a file.
move /Y %1 "%TargetFilePath%" 2>nul
if errorlevel 1 (
rd "%TargetFilePath%" 2>nul
echo ERROR: Could not move file %1.
goto :EOF
)
rem Create the shortcut file in source directory of moved file.
"%~dp0Shortcut.exe" /F:"%~dpn1.lnk" /A:C /T:"%TargetFilePath%%~nx1" /W:"%TargetFilePath%" /R:1 >nul
set /A FileCount+=1
goto :EOF

:EndBatch
endlocal
pause

批处理文件包含大量额外的代码,以尽可能防止错误的用户输入进行故障保护。

要了解使用的命令及其工作原理,请打开命令提示符窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • call /?
  • choice /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅:

  • GOTO :EOF 回到哪里?
  • %~dp0是什么意思,它是如何工作的?
  • Microsoft关于使用命令重定向运算符的文章
  • 使用Windows批处理文件的具有多个命令的单行
  • Windows 命令解释器 (CMD.EXE( 如何解析脚本?
  • 为什么在命令行上使用"set var = text"后没有带有"echo %var%"的字符串输出?
  • 如何阻止Windows命令解释器在不正确的用户输入上退出批处理文件执行?

最新更新