Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件循环不';不包括嵌套循环_Batch File_Cmd - Fatal编程技术网

Batch file 批处理文件循环不';不包括嵌套循环

Batch file 批处理文件循环不';不包括嵌套循环,batch-file,cmd,Batch File,Cmd,编辑1: 因此,我的循环似乎运行到最后一条记录,然后执行mkdir和MOVE命令。我觉得我好像在什么地方漏掉了括号 FOR %%i IN (%folderPath%\*.Pdf) DO SET fileName=%%i ECHO %fileName% FOR /f "tokens=3-4 delims=_" %%a IN ("%fileName%") DO SET fileClientID=%%a_%%b ECHO %fileClientID% REM Check to see if

编辑1:

因此,我的循环似乎运行到最后一条记录,然后执行mkdir和MOVE命令。我觉得我好像在什么地方漏掉了括号

FOR %%i IN (%folderPath%\*.Pdf) DO SET fileName=%%i
  ECHO %fileName%
FOR /f "tokens=3-4 delims=_" %%a IN ("%fileName%") DO SET fileClientID=%%a_%%b
  ECHO %fileClientID%

REM Check to see if folders exist, and if they do not, create them
IF NOT EXIST "%folderPath%\%fileYear%" mkdir %folderPath%\%fileYear%

IF NOT EXIST "%folderPath%\%fileYear%\%fileMonth%" mkdir %folderPath%\%fileYear%\%fileMonth%

IF NOT EXIST "%folderPath%\%fileYear%\%fileMonth%\%fileClientID%" mkdir %folderPath%\%fileYear%\%fileMonth%\%fileClientID%

REM Moves the file from source path to destination path
SET fileSourcePath="%fileName%"
SET fileDestPath="%folderPath%\%fileYear%\%fileMonth%\%fileClientID%"

MOVE "%fileSourcePath%" "%fileDestPath%"

PAUSE

因此,我终于完成了代码,它的所有工作,因为它应该。我真的很感谢大家帮助我做到这一点,因为这是我写的第一个批处理文件脚本。下面是最终产品,当然,如果有任何最佳实践或更有效的写作方法,我希望得到反馈。再次感谢

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

SET folderPath="C:\Users\nha\Desktop\FolderMover\Test"

FOR %%a IN (%folderPath%\*.Pdf) DO (
  SET fileDate=%%~ta
  SET fileName=%%a
FOR /f "tokens=1,3 delims=/, " %%a IN ("!fileDate!") DO (
  SET fileYear=%%b
  SET fileMonth=%%a
For /f "tokens=3-4 delims=_" %%a IN ("!fileName!") DO (
  SET fileClientID=%%a_%%b
IF NOT EXIST !folderPath!\!fileYear!\!fileMonth!\!fileClientID! mkdir !folderPath!\!fileYear!\!fileMonth!\!fileClientID!
  SET fileSourcePath=!fileName!
  SET fileDestPath=!folderPath!\!fileYear!\!fileMonth!\!fileClientID!
  MOVE !fileSourcePath! !fileDestPath!
    )
  )
) 

不要动几次。评估必要的数据创建结构并仅移动一次。使用
for
迭代文件夹中的文件,并使用
for/f
解析名称。用于元变量
%%x
~z
修饰符将返回文件日期。这里有上百个类似的例子。请编辑这个问题并粘贴在你试图使其生效的代码文本中。不用担心现在的样子。我们都从某个地方开始。您可以通过编写批处理文件来学习编写批处理文件。@请确保filename变量需要包含实际存在的文件名。也许
SET“fileName=GRGID\u ABM000001\u ClientID\u 113\u GlxyABM.pdf”
Hmm好吧,我现在将其设置为静态(尽管将来我需要它在目录的文件中循环),当我尝试调用变量时,它仍然返回Echo is Off错误。作为“最佳实践”:始终用引号括住路径(避免包含空格或其他有毒字符时出现语法错误)。请使用
set
set“var=value”
(注意引号的位置)为了避免出现意外空格和有毒字符的语法错误,请只使用一个
setlocal enableextensions enabledelayedexpansion
@Stephan附带问题。如果我想将folderPath更改为一个文件夹,该文件夹位于一个看起来像这样的网络驱动器上\\gfrgts.gfrg.local\PrintDocuments我需要一些k吗有什么特别的关键词可以让它发挥作用?