Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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 - Fatal编程技术网

Batch file 将文件名子字符串与文件夹名子字符串进行比较并移动

Batch file 将文件名子字符串与文件夹名子字符串进行比较并移动,batch-file,Batch File,我需要将几个文件从一个文件夹移动到子文件夹。我的文件夹结构已经准备好了 文件当前文件夹:D:\AB\*. 文件名为:SS AA Report Temp file for Script Testing-Daily-31March.txt 目标文件夹:D:\AB\Pm 1.1脚本文件\Daily\ 如何将文件名子字符串与文件夹名子字符串检查并移动 注意,我有多个这样的文件 set Path1= d:\AB Pushd %Path1% echo %Path1% for %%i in (*.*) do

我需要将几个文件从一个文件夹移动到子文件夹。我的文件夹结构已经准备好了

文件当前文件夹:
D:\AB\*.

文件名为:
SS AA Report Temp file for Script Testing-Daily-31March.txt

目标文件夹:
D:\AB\Pm 1.1脚本文件\Daily\

如何将文件名子字符串与文件夹名子字符串检查并移动

注意,我有多个这样的文件

set Path1= d:\AB
Pushd %Path1%
echo %Path1%
for %%i in (*.*) do SET "FName=%%~ni"
For /F "Tokens=4-5 Delims=-" %%A In ("%FName%") Do (
    Set "FoldOne=%%A"
    Set "FoldTwo=%%B"
)
echo out %RDate%
mkdir %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%
move %Path1%\"%FName%".* %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%\
编辑:
文件名格式:

A-A-Format-Here First connectivity install on Day 0 regurlarly-Daily-All-2017-03-27-09-31-16.xls    
A-A-Format-Already First connectivity with 10 days created-Weekly-All-2016-11-28-10-01-02.csv    
A-A-Report-withname 1.2 Sample Report (Network Plan Report)-Daily-Detail-2017-01-03-23-53.xls    
A-A-Report-Nextreport 1.2 Sample Report (Network Plan Report)-Weekly-Detail-2017-01-03-23-02-53.csv    
现在,我的文件夹结构是:

D:\AB\Pm 1.1 First connectivity install on Day 0\Daily\05042017    
D:\AB\Pm 2.1 First connectivity with 10 days\Weekly\29032017    
D:\AB\Pm 1.2 Sample Report\Daily\05042017    
D:\AB\Pm 1.2 Sample Report\Weekly\29032017    
这是我已经拥有的批处理文件:

set Path1= d:\AB
Pushd %Path1%
echo %Path1%
for %%i in (*.*) do SET "FName=%%~ni"
For /F "Tokens=4-5 Delims=-" %%A In ("%FName%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B"

)
echo 1 %FoldOne%
echo 3 %FoldTwo%

IF %FoldTwo% == Daily (
echo here Daily
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('ddMMyyyy')"`
) Do (Set "RDate=%%A"
echo ffor %RDate%
)
)

IF %FoldTwo% == Weekly (
Echo Weekly 
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"`
) Do (Set "RDate=%%A"
echo %RDate%
)
)

mkdir %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%
move %Path1%\"%FName%".* %Path1%\"%FoldOne%"\"%FoldTwo%"\%RDate%\

Pushd d:\

GoTo :EOF

将文件名子字符串与文件夹名匹配的逻辑仍然非常模糊

然而,我编写了两个可能的解决方案,使用部分不同的方法实现了相同的功能

第一个完整的批次代码:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
cd /D "D:\AB"

rem Get name of each subfolder starting with string Pm, a space, two single
rem digit numbers separated by a dot, one more space and more characters to
rem indexed environment variables for later usage. And assign the substring
rem after the first 7 characters of each folder name also to an index
rem environment variable.

set FolderIndex=0
for /D %%I in ("Pm ?.? *") do (
    set "FolderName!FolderIndex!=%%I"
    set "CurrentPath=%%I
    set "FolderPart!FolderIndex!=!CurrentPath:~7!"
    set /A FolderIndex+=1
)
set "FolderCount=%FolderIndex%"
rem set Folder

rem Get date of yesterday and date of a week ago.
for /F "usebackq" %%I in (`PowerShell.exe "(Get-Date).AddDays(-1).ToString('ddMMyyyy')"`) do set "DateDaily=%%I"
for /F "usebackq" %%I in (`PowerShell.exe "(Get-Date).AddDays(-7).ToString('ddMMyyyy')"`) do set "DateWeekly=%%I"
rem set Date

rem Process now each file matching the wildcard pattern below in
rem current folder and calling a subroutine with current file name.

set "FileNotMoved=0"
for %%I in (*-*-*-*-*) do call :MoveToFolder "%%I"

endlocal & if not %FileNotMoved% == 0 pause
goto :EOF

rem This subroutine first gets fourth and fifth dash delimited part from
rem each passed double quoted file name.

rem Then it replaces in each fourth file name part each folder name part
rem by an empty string until either all folder name parts are processed
rem or the string substitution was successful meaning the file name part
rem really contains the folder name part.

rem Note: The substitution does not work correct if any folder name part
rem       contains an equal sign as this character is the delimiter
rem       between string to find and replace string for substitution.

rem In second case with substitution being successful the folder for
rem the file could be determined and the file is moved to the found
rem folder if also time part could be determined from file name.

:MoveToFolder
for /F "tokens=4,5 delims=-" %%A in ("%~1") do set "NamePart=%%A" & set "NameTime=%%B"
set "FolderIndex=0"

:FindFolder
if %FolderIndex% == %FolderCount% (
    set "FileNotMoved=1"
    echo Found no folder for: %1
    goto :EOF
)

call set "CurrentPart=%%FolderPart%FolderIndex%%%"
if "!NamePart:%CurrentPart%=!" == "!NamePart!" (
    set /A FolderIndex+=1
    goto FindFolder
)

call set "CurrentFolder=%%FolderName%FolderIndex%%%"
if /I "%NameTime%" == "Daily" (
    set "FolderTime=%DateDaily%"
) else if /I "%NameTime%" == "Weekly" (
    set "FolderTime=%DateWeekly%"
) else (
    set "FileNotMoved=1"
    echo Undefined time for:  %1
    goto :EOF
)

mkdir "%CurrentFolder%\%NameTime%\%FolderTime%" 2>nul
move "%~1" "%CurrentFolder%\%NameTime%\%FolderTime%\" >nul
if errorlevel 1 (
    set "FileNotMoved=1"
    echo Failed to move file: %1
)
goto :EOF
第二个批处理代码与第一个解决方案的区别仅在于如何对子程序
MoveToFolder
进行编码,以查找当前文件名对应的文件夹。由于这个原因,下面只发布了子程序的代码

:MoveToFolder
for /F "tokens=4,5 delims=-" %%A in ("%~1") do set "NamePart=%%A" & set "NameTime=%%B"

for /F "tokens=1* delims==" %%X in ('set FolderPart') do (
    if not "!NamePart:%%Y=!" == "%NamePart%" (
        set "FolderName=%%X"
        goto FoundFolder
    )
)

set "FileNotMoved=1"
echo Found no folder for: %1
goto :EOF

:FoundFolder
if /I "%NameTime%" == "Daily" (
    set "FolderTime=%DateDaily%"
) else if /I "%NameTime%" == "Weekly" (
    set "FolderTime=%DateWeekly%"
) else (
    set "FileNotMoved=1"
    echo Undefined time for:  %1
    goto :EOF
)

set "FolderIndex=%FolderName:~10%"
call set "CurrentFolder=%%FolderName%FolderIndex%%%"

mkdir "%CurrentFolder%\%NameTime%\%FolderTime%" 2>nul
move %1 "%CurrentFolder%\%NameTime%\%FolderTime%\" >nul
if errorlevel 1 (
    set "FileNotMoved=1"
    echo Failed to move file: %1
)
goto :EOF
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • cd/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • mkdir/?
  • 移动/?
  • 暂停/?
  • rem/?
  • 设置/?
  • setlocal/?

另请阅读Microsoft关于的文章,了解有关
2>nul
>nul
的解释,并回答有关Windows命令行上操作员
&
含义的问题。

RDate设置在何处?您知道
fold1
将包含前导空格吗?图中的
p1.1
出现在哪里?是否要删除
Temp
Testing
字符串?这些是唯一的字符串,它们是否总是出现?第一个错误,在
set Path1=C:\Main
中等号后的空格,Windows命令解释器在将字符串分配给环境变量时不会忽略该空格。删除此空格字符。第二个错误是,整个目录/文件字符串必须用双引号括起来,而不是其中的一部分。因此,使用
mkdir”%Path1%\%FoldOne%\%FoldTwo%\%RDate%“
移动“%Path1%\%FName%.*”“%Path1%\%FoldOne%\%FoldTwo%\%RDate%\”
。要获取任何命令的帮助,请打开命令提示符窗口,并以
/?
作为参数运行该命令。请使用
set/?
for/?
进行尝试,并阅读输出帮助页。@Magoo感谢您的回复,Rdate是在系统日期设置的文件夹名称。温度和测试只是用来解释需求。我的基本要求是,我需要将文件夹名的子字符串与文件名的子字符串相匹配,然后将该文件移动到相应的文件夹中,而且我将有多个这样的文件,并希望移动所有这些文件。@Mofi我尝试用双引号输入整个目录文件名,但无效,因此故意这样做。但正如我所说的,我的要求是在两端匹配子字符串,正如上面评论中所解释的那样。@Ashu,您发布的代码似乎是我之前为您提交的两个答案的一个修改不好的版本。事实上,您当前的脚本似乎应该与我在上一个问题中给出的已接受答案完全相同(除了
%Path1%
的更改位置)。不幸的是,这一次您没有为我们提供足够的信息来确定您的任务/目标。请正确解释您的文件和目录命名结构,以及您试图确定两者之间的字符串匹配。