2016-09-23 15 views
0

我遇到問題,試圖重命名並移動一個文件,一旦拖過我的批處理文件。如何更改文件擴展名並將文件移動到一個批處理文件中的某個目錄?

我已經看過了四個非常相似的帖子,沒有遭受任何幫助,對我說:

  1. How to rename and move files to new directory

  2. Windows Script to Rename and move files

  3. .bat file to rename and move files with prompt

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

第四例子是最接近於我試圖完成,但隨機重命名文件,並將其複製到多個目錄。我想要做的就是將文件拖到.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

所以...什麼是你的問題?你有什麼代碼?它有什麼問題? – TessellatingHeckler

+0

我試圖結合這兩個代碼重命名,然後將文件移動到特定的目錄,但我遇到了麻煩,因爲我不太確定這樣做。 >'關閉@echo 組/ P VAR =移至:%VAR% CLS 移動%1 「%VAR%」 pause' >'關閉@echo 組/ P VAR =文件名:% var% cls set/p var2 =文件類型:%var2% 重命名%1「%var%。%var2%' – FleetSakes

+1

如果您將原始問題添加到原始問題中,將會更好。儘可能從頭開始,這樣我們可以更快地理解你的問題,並制定出答案。 – rlam12

回答

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" 
0

將文件移到另一個文件夾改變文件擴展名可以與也做

  • 選擇文件並按下克Ctrl + C鍵(複印),
  • 將目標文件夾並按下Ctrl + V鍵(粘貼)
  • 按壓F2用於開始重命名文件,
  • 改變文件擴展名和按壓返回

或可替代

  • 選擇文件並單擊移動到Windows資源管理器命令,
  • 在打開的窗口中選擇目標文件夾,然後點擊按鈕移動
  • 導航到目標文件夾和
  • 更改文件擴展名有F2,修改文件擴展名並按RETURN

而對於這樣的任務,最有用的是一個文件管理器像總指揮官同時移動選定的文件(S)從左至右或從右到左文件夾窗格它支持改變文件擴展名的使用。

但是,這裏是一個批處理腳本,您可能會使用它幷包含一些擴展,以檢測用戶必須手動輸入路徑和文件擴展名時可能出現的一些可能的錯誤。它可以通過更多的錯誤檢查來增強。

@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 /?
+0

*「我真的不明白什麼應該是好處」* - 聽說過任務自動化嗎? – SomeWittyUsername

+0

什麼?自動化通常是用腳本構建的,無論是perl,bash,windows批處理還是其他相關的 語言。沒有人強制每次手動輸入路徑,它可以作爲另一個批處理文件輸出的輸入提供,例如 – SomeWittyUsername

+1

是的,我已閱讀它。他詢問了一件非常具體的事情,這可能是也可能不是更大的事情的一部分。它可以只是一個簡化的示例,可以省略不必要的細節。在任何情況下,假設普通用戶(尤其是想要編寫批處理腳本的人)知道如何處理基本任務(如使用Windows GUI移動和重命名文件)是合理的。 – SomeWittyUsername

相關問題