2017-10-16 56 views
0

我有一個批處理腳本已經工作了幾個月。該腳本的目的是基於文件名創建一個文件夾,並根據特定目的相應地重命名該文件夾。但是,它會停止將文件移動到循環中創建的文件夾。我在其他機器上測試過它,它工作正常,但在特定的機器上;它只是不工作。爲什麼批處理文件在某些​​機器上沒有按預期工作之前工作?

我能做些什麼來使循環生效,爲什麼在工作了好幾個月後批量停止工作(將文件移動到文件夾)?

setlocal EnableDelayedExpansion 

for /F %%a in ('dir "C:\Program Files\WinSCP\Unconverted" /a-d /b') do (
    if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a" 

:func 
set file=%~1 
set dir=%file:~0,49% 
mkdir "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 2>nul 

rem ECHO "%file%" 
rem ECHO "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 

move /Y "C:\Program Files\WinSCP\Unconverted\%file%" "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 
) 

start "" "C:\Program Files\WinSCP\hide_conversion_window.exe" 
+1

你看到的錯誤是什麼?我已經可以看到一個括號)在開始的for循環中缺少。 – oldabl

+0

我在刪除@echo後沒有看到任何錯誤。請告訴我缺少的括號)是因爲我找不到丟失的地方。謝謝 – great77

+0

謝謝,我已經發現了它,現在會讓你知道它是否工作...。如果不是「%%〜dpnxa」==「%〜dpnx0」,則調用:func「%/ a」中的/ F %% a(「dir」C:\ Program Files \ WinSCP \ Unconverted「/ ad/b')do( ) %〜a「) – great77

回答

1

我重寫,並評論該批處理文件,它包含了幾個問題,其中大多數是沒有問題的,只要這個批處理文件存儲在%ProgramFiles%\WinSCP\Unconverted這個目錄也是作爲在批處理文件執行當前目錄雙擊批處理文件。

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 
set "SourceFolder=%ProgramFiles%\WinSCP\Unconverted" 

rem Process all files in source folder found by command DIR with ignoring 
rem subfolders and listed in bare format which means only file names with 
rem file extension but without file path. The batch file itself is skipped 
rem if being also stored in the source folder specified above. 

for /F "delims=" %%I in ('dir "%SourceFolder%\*" /A-D /B 2^>nul') do (
    if /I not "%SourceFolder%\%%I"=="%~f0" call :MoveFile "%SourceFolder%\%%I" 
) 

rem Execute converter through AutoIt in a separate command process and 
rem while conversion is running continue with batch processing which means 
rem restoring previous environment and finally exiting batch file processing. 

start "" "%ProgramFiles%\WinSCP\hide_conversion_window.exe" 
endlocal 
goto :EOF 

rem MoveFile is a subroutine which expects to be called with one argument 
rem being the name of the file to move with full file name which means 
rem with file path, file name and file extension. 

rem The first 49 characters of the file name define the name for target 
rem folder on which "_fdc" must be appended for completion. This folder 
rem is created without verification on success and then the file is 
rem moved into this folder again without verification on success. 

:MoveFile 
set "FileName=%~nx1" 
set "FolderName=%FileName:~0,49%_fdc" 
mkdir "%~dp1\%FolderName%" 2>nul 
move /Y "%~1" "%~dp1\%FolderName%\" >nul 
goto :EOF 

這個批處理文件適用於批處理文件被存儲在不同的文件夾的源的文件夾或當前目錄是不同的目錄比含有批處理文件或一個找到的文件的文件夾包含空格字符或任何其它特殊字符像&()[]{}^=;!'+,`~

爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • mkdir /?
  • move /?
  • set /?
  • setlocal /?
  • start /?

閱讀也是微軟文章關於Using Command Redirection Operators

0

感謝您的建議oldabi。有時候事情會發揮作用,而且我們認爲在事件崩潰之前它們都是完美的。感謝您的建議。我剛剛意識到我錯失了支架的錯誤。

SETLOCAL ENABLEDELAYEDEXPANSION 

for /F %%a in ('dir "C:\Program Files\WinSCP\Unconverted" /a-d /b') do (
    if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a") 

goto conversion 

    :conversion 
rem ::execute converter through autoit 
start "" "C:\Program Files\WinSCP\hide_conversion_window.exe" 


:func 
set file=%~1 
set dir=%file:~0,49% 
mkdir "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 2>nul 

rem ECHO "%file%" 
rem ECHO "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 

MOVE /Y "C:\Program Files\WinSCP\Unconverted\%file%" "C:\Program Files\WinSCP\Unconverted\%dir%_fdc" 
相關問題