2017-10-16 15 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 ") – 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に関するMicrosoftの記事。

0

提案のおかげでよかったです。時々物事はうまくいくと思って、それが崩壊するまで完璧です。提案していただきありがとうございます。私は括弧がないことに間違いを感じました。それはほとんど、このバッチファイルとして限り、問題はなかったことにより、いくつかの問題が含まれているとして、私はバッチファイルを書き直したとコメントし

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"