函数返回批处理文件中的变量

时间:2017-03-30 09:50:29

标签: function batch-file

我正在尝试编写一个代码,该代码将根据文件名创建一个文件夹树,然后将其移动到相应的文件夹中,最后一个子文件夹将在日期。 我面临的问题是Call :SubtractDate %Year% %Month% %Day% -10 Ret 这里ret没有从函数中捕获传递的值。

到目前为止创建的代码::

@echo Off
SetLocal

set Path1= d:\A
Pushd %Path1%

for %%i in (*.*) do SET "FName=%%~ni"
For /F "Tokens=4-5 Delims=-" %%A In ("%FName%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B"
)

IF %FoldTwo% == ABC (
echo Daily
Call :GetDateTime Year Month Day
Call :SubtractDate %Year% %Month% %Day% -10 Ret
Echo %Year% %Month% %Day%
echo %Ret%
exit /b
)

if not exist %Path1%\"%FoldOne%"\"%FoldTwo%"\%Ret% (
  mkdir %Path1%\"%FoldOne%"\"%FoldTwo%"\%Ret% )
  move %Path1%\"%FName%".* %Path1%\"%FoldOne%"\"%FoldTwo%"\%Ret%\

Pushd d:\

GoTo :EOF


:SubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
@echo off & setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3* ((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*    (L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY=I, MM=100+J, DD=100+K
set MM=%MM:~-2,2% & set DD=%DD:~-2%
set ret=%DD: =%%MM: =%%YYYY: =%
endlocal & set %~5=%ret%
echo %ret%
exit /b


:GetDateTime Year Month Day Hour Minute Second
@echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
     IF "%~1" NEQ "" set "%~1=%YYYY%" 
     IF "%~2" NEQ "" set "%~2=%MM%" 
     IF "%~3" NEQ "" set "%~3=%DD%"
     IF "%~4" NEQ "" set "%~4=%HH%" 
     IF "%~5" NEQ "" set "%~5=%Min%"
     IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b

1 个答案:

答案 0 :(得分:1)

根据我给你的最后一个回复,,你似乎没有真正关注,你可以尝试这样的事情:

@Echo Off
Set "Path1=D:\A"
Set "RelDays=-10"
Set "_="
If /I Not "%CD%"=="%Path1%" (Pushd "%Path1%" 2>Nul & Set "_=T") || Exit/B
For /F UseBackQ %%A In (
    `PowerShell "(Get-Date).AddDays(%RelDays%).ToString('ddMMyyy')"`
) Do Set "RDate=%%A"
For %%A In ("*-*-*-*-*.*") Do Call :Sub "%%A"
If Defined _ PopD
Timeout -1
GoTo :EOF

:Sub
For /F "Tokens=4-5Delims=-" %%A In ("%~n1") Do If Not "%%A"=="" (
    If "%%B"=="ABC" (If Not Exist "%%A\%%B\%RDate%\" MD "%%A\%%B\%RDate%"
        Move %1 "%%A\%%B\%RDate%"))

我使用powershell来计算日期而不是你找到的函数。