2017-10-16 55 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" 
相关问题