批处理文件变量路径不起作用

时间:2015-12-03 10:43:49

标签: batch-file

我编写了一个批处理脚本,我希望它能够查看文件夹并查找与特定模式匹配的文本文件。如果与我指定的模式匹配,我想创建一个文件夹并将匹配的文件移动到创建的文件夹中。

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

set source=Folder_Name.txt


For /f "tokens=2-4 delims=/ " %%a in ('date /t') DO (set year=%%c)
For /f "tokens=2-4 delims=/ " %%a in ('date /t') DO (set /a month=%%b-1)

if !month! == 0 (set /a year=year-1)
if !month! == 0 (set month=12)


set exception=!year!!month!

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Check Every Folder in the source for pattern matching.
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

FOR /f "tokens=* delims=" %%H IN (!source!) DO ( 

set location=%%H\Logs

 For /R "!location!" %%f in ("*!exception!*.txt") Do (echo %%f>> output.txt)


)

Folder_Name.txt

FOLDER_1

Folder_2

Folder_3

脚本将进入每个文件夹的子目录,例如(C:\ Users \ halo \ Desktop \ Scripts \ Move \ folder_1 \ logs)并查找与上个月的日期模式匹配的文件,例如yyyymm (201511)。

所以我希望找到诸如(cloud_20151101.txt或cloud_20151102.txt)等文件并将它们移动到文件夹中。

但是,当我读入文件夹名称时,我的脚本不起作用。当我改变!位置!在递归for循环到C:\ Users \ halo \ Desktop \ Scripts \ Move \ folder_1 \ logs \,它工作。我还没有实现移动文件,因为我想先解决这个问题。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

首先,检查您获取日期令牌的方式。因为你是在" /"你将从date /t命令获得3个令牌。

因此,要获得日期模式,请尝试这样做:

For /f "tokens=3 delims=/ " %%a in ('date /t') DO (set year=%%a)

For /f "tokens=2 delims=/ " %%a in ('date /t') DO (set /a month=%%a-1)

现在,对于从" Folder_Name.txt"中读取的目录中的检查,你可以这样做:

FOR /f "tokens=* delims=" %%H IN (!source!) DO ( 
    set location=%%H\Logs
    dir /b !location! | findstr /r /c:"!exception!">>output.txt
)

然后你只需要实现逻辑来移动文件。在dir行,您可以输出如下内容:

dir /b !location! | findstr /r /c:"!exception!">>output_%%~nH.txt

因此,对于您阅读的每个目录,您将拥有一个输出文件。输出文件的格式为:output_NAME-OF-THE-DIRECTORY-SEARCHED.txt

希望它有所帮助。

答案 1 :(得分:0)

date /t输出的字符串格式取决于Windows区域和语言设置。

在代码中使用环境变量 DATE 的值而不是date /t,而访问中的代码始终是当前日期。日期字符串的格式还取决于Windows区域和语言设置。例如,对于德国国家/地区,格式为dd.mm.yyyy,而在其他国家/地区,格式为dd/mm/yyyymm/dd/yyyy。在命令提示符窗口echo %DATE%中运行以查看本地日期格式。

注意:date \t的格式(通常是工作日)通常与%DATE%的格式不同(通常没有工作日)。

我对Find out if file is older than 4 hours in batch file的回答包含有关Windows上日期和时间的更多信息,还演示了如何使用wmic以区域和语言无关的格式获取日期字符串,但批处理脚本的缺点变得更慢

以下代码适用于日期格式dd.mm.yyyydd/mm/yyyyd/m/yyyy(日期或月份的前导0小于10)。

其他Windows命令处理细节也必须考虑在内,如2位或3位数字,前导0在计算时被解释为八进制数。

这是一个注释代码,应该适用于您的任务。

@echo off
setlocal EnableExtensions
set "source=Folder_Name.txt"

rem Set directory of batch file as current directory.

cd /D "%~dp0"

rem Get month and year from environment variable DATE which has for
rem countries with German as native language the format dd.mm.yyyy.
rem The code below works also for date formats dd/mm/yyyy and d/m/yyyy.

for /F "tokens=2,3 delims=./" %%A in ("%DATE%") do (
    set "month=%%A"
    set "year=%%B"
)

rem Remove leading zero from month as otherwise the value
rem would be interpreted as octal number resulting in an error
rem for 08 (August) and 09 (September) on next calculation.

if "%month:~0,1%" == "0" set "month=%month:~1%"

rem Subtract 1 from month.

set /A month-=1

rem Decrease year by 1 and set month to December if month is now 0.
rem Otherwise check if month is smaller than 10 and
rem insert a leading 0 if this condition is true.

if %month% == 0 (
    set /A year-=1
    set "month=12"
) else if %month% LSS 10 set "month=0%month%"

set "exception=%year%%month%"

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::Check every folder in the source for pattern matching.
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

rem Command DIR with parameter /B (bare format - only file name) returns
rem only file names (parameter /A-D) matching the search pattern ordered
rem by name (parameter /ON) found in specified directory or any subdirectory
rem (parameter /S) with full path because of parameter /S and always without
rem quotes even if file name with path contains 1 or more spaces.

if exist output.txt del output.txt
for /F "usebackq delims=" %%H in ("%source%") do (
    for /F "delims=" %%F in ('dir /A-D /B /ON /S "*%exception%*.txt" 2^>nul') do echo %%F>>output.txt
)

rem Restore previous environment table, restore previous
rem states for command extensions and delayed expansion
rem and restore also previous current directory.

endlocal

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • cd /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
相关问题