初学者代码警告:
我有一个基于多个面具的PDF文件夹
ex:*-*-*.pdf
我正在尝试根据文件名创建目录,然后根据该目录中的创建日期将文件移动到月份文件夹中
例如:
%source%\%%a-%%b-%%c.pdf
%destination%\%%a\%%b\%month%\%%a-%%b-%%c.pdf
这是我到目前为止的代码,其中我使用(dir / t:c * .pdf)来抓住月份
@ECHO OFF
title Invoice/Packslip Mover
::mode con: cols=16 lines=8
SETLOCAL
:Top
cls
move C:\customers\Test\invoice\*.pdf "C:\customers\Test\invoice-temp" >nul 2>&1
:Invoice1
:: *-*-*.pdf
SET "source=C:\customers\Test\invoice-temp"
SET "destination=C:\customers\Test\cloud\customers\invoice"
PUSHD %source%
FOR /f "tokens=1-3 delims=-" %%a IN (
'dir /b /a-d "*-*-*.pdf"'
) DO (
dir /t:c *.pdf | find "%%a-%%b-%%c">test.txt
FINDSTR "07/*/* " test.txt
IF %ERRORLEVEL% EQU 0 SET month=July
echo %month%
pause
mkdir "%destination%\%%a\%%b\%month%" >nul 2>&1
MOVE /Y "%%a-%%b-%%c" "%destination%\%%a\%%b\%month%" >nul 2>&1
)
POPD
执行以下声明时:
IF %ERRORLEVEL% EQU 0 SET month=July
echo %month%
即使返回错误级别
,它也不会回显变量July这只是我想到的一种方式来获取创建日期并将其保存为值
我对vanilla CMD中的任何其他方法持开放态度来解决这个问题
答案 0 :(得分:1)
您必须设置setlocal enableDelayedExpansion。
setlocal enableDelayedExpansion
FOR /f "tokens=1-3 delims=-" %%a IN (
'dir /b /a-d "*-*-*.pdf"'
) DO (
dir /t:c *.pdf | find "%%a-%%b-%%c">test.txt
FINDSTR "07/*/* " test.txt
IF !ERRORLEVEL! EQU 0 SET month=July
echo !month!
pause
mkdir "%destination%\%%a\%%b\!month!" >nul 2>&1
MOVE /Y "%%a-%%b-%%c" "%destination%\%%a\%%b\!month!" >nul 2>&1
)
这对我有用,但是,你可能想要达到这个目的:
FOR /f "tokens=1-3 delims=-" %%a IN (
'dir /b /a-d "*-*-*.pdf"'
) DO (
dir /t:w *.pdf | find "%%a-%%b-%%c">test.txt
FINDSTR "07/*/* " test.txt
IF !ERRORLEVEL! EQU 0 (
SET month=July
echo !month!
pause
mkdir "%destination%\%%a\%%b\!month!" >nul 2>&1
MOVE /Y "%%a-%%b-%%c" "%destination%\%%a\%%b\!month!" >nul 2>&1
)
)