如何更改文件扩展名并将文件移动到一个批处理文件中的某个目录?

时间:2016-09-23 19:53:57

标签: windows batch-file

我在浏览批处理文件时尝试重命名并移动单个文件时遇到问题。

我看了四篇非常相似的帖子,没有任何对我有任何帮助:

  
      
  1. How to rename and move files to new directory

  2.   
  3. Windows Script to Rename and move files

  4.   
  5. .bat file to rename and move files with prompt

  6.   
  7. Moving and renaming files, keeping extension but include sub directories in batch file

  8.   

第四个例子最接近我想要完成的,但是随机重命名文件并将它们复制到多个目录。我想要做的就是将文件拖到.bat脚本中更改扩展名并将其移动到某个目录。

我试图将这两个代码组合起来重命名,然后将文件移动到特定目录。但我这样做有困难,因为我不太确定这样做。

@echo off
set /p var=Move To:
%var%
cls
move %1 "%var%"
pause

@echo off
set /p var=File Name:
%var%
cls
set /p var2=File Type:
%var2%
Rename %1 "%var%.%var2%

2 个答案:

答案 0 :(得分:0)

这与你一直在寻找的代码完全不同,但是:

@Echo Off
If Not Exist "%~1" Exit/B
If Exist "%~1\" Exit/B
If Not Exist "A:\Certain Directory\%~n1.ext" Move "%~1" "A:\Certain Directory\%~n1.ext"

答案 1 :(得分:0)

使用

也可以将文件移动到另一个文件扩展名更改的文件夹
  • 选择文件并按 Ctrl + C (复制),
  • 转到目标文件夹并按 Ctrl + V (粘贴)
  • F2 开始重命名文件,
  • 更改文件扩展名并按 RETURN

或者

  • 选择文件并单击 Windows资源管理器
  • 移至命令,
  • 在打开的窗口中选择目标文件夹,然后单击按钮移动
  • 导航到目标文件夹和
  • 使用 F2 更改文件扩展名,修改文件扩展名并按 RETURN

对于这样的任务最有用的是使用像 Total Commander 这样的文件管理器,它支持在从左到右或从右到左移动所选文件的同时更改文件扩展名文件夹窗格。

但是,这是一个您可能使用的批处理脚本,它包含一些扩展来检测一些可能的错误,当用户必须手动输入路径和文件扩展名时,这些错误总是会发生。通过更多的错误检查,它仍然可以得到增强。

@echo off
cls
echo.
rem Is the batch file called without an argument/parameter?
if "%~1" == "" (
    echo Call %~nx0 always with name of a file with full path.
    echo.
    echo For example:
    echo.
    echo %~nx0 "C:\Path to folder\with file to\move with new extension.txt"
    echo.
    pause
    goto :EOF
)

if not exist "%~1" (
    echo Error: There is no file
    echo.
    echo %~1
    echo.
    pause
    goto :EOF
)

if exist "%~1\" (
    echo Error: The argument
    echo.
    echo %~1
    echo.
    echo specifies a directory and not a file.
    echo.
    pause
    goto :EOF
)

setlocal EnableDelayedExpansion
echo Preparing for moving the file
echo.
echo %~1
echo.
set "TargetFolder=%USERPROFILE%\Documents"
echo Please specify the target folder.
echo.
echo Default target is: %TargetFolder%
echo.
set /P "TargetFolder=Move to: "
echo.

rem Replace all / by \ in target folder path.
set "TargetFolder=!TargetFolder:/=\!"

rem If the target folder path ends with a backslash, remove the backslash.
if "!TargetFolder:~-1!" == "\" set "TargetFolder=!TargetFolder:~0,-1!"

rem Does the target folder really exist?
if not exist "!TargetFolder!\" (
    echo Error: The target folder
    echo.
    echo !TargetFolder!
    echo.
    echo does not exist.
    echo.
    endlocal
    pause
    goto :EOF
)

set "NewExtension=txt"
echo.
echo Please specify the new file extension.
echo.
echo Default file extension is: %NewExtension%
echo.
set /P "NewExtension=New extension: "
echo.

rem If the new file extension starts with a point, remove the point.
if "!NewExtension:~0,1" == "." set "NewExtension=!NewExtension:~1!"

rem Windows interprets everything up to last point as file name and
rem everything from last point to end of name of file as file extension.
rem On *nix systems it is common that hidden files don't have a file
rem extension and start with a point (for hiding the file). To copy such
rem files with unusual file name on Windows like .htaccess also correct,
rem it is necessary to interpret the file extension as file name if the
rem file starts with a point and has no file extension.

if not "%~n1" == "" (
    set "NewNameOfFile=%~n1.!NewExtension!"
) else (
    set "NewNameOfFile=%~x1.!NewExtension!"
)

echo.
move /-Y "%~1" "!TargetFolder!\!NewNameOfFile!"
if errorlevel 1 (
    endlocal
    echo.
    pause
) else (
    endlocal
)

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

  • cls /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?