2017-04-03 39 views
0

我需要将几个文件从一个文件夹移动到子文件夹。我的文件夹结构已经准备就绪。比较文件名子串与文件夹名称子串并移动

文件当前文件夹:D:\AB\*.*
的文件名是:    SS-AA-Report-Temp File for Script Testing-Daily-31March.txt
目标文件夹:D:\AB\Pm 1.1 File For Script\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 
+0

RDate在哪里设置?你知道'fold1'将包含一个前导空间吗? “p1.1”在哪里出现?您是否希望删除“Temp”和“Testing”字符串 - 这些是唯一的字符串,它们会一直出现吗? – Magoo

+1

第一个错误是等号'set Path1 = C:\ Main'之后的空格,Windows命令解释程序在将字符串分配给环境变量时不会省略该空格。删除此空格字符。第二个错误是,整个目录/文件字符串必须用双引号括起来,而不是其中的部分。因此,使用'mkdir'%Path1%\%FoldOne%\%FoldTwo%\%RDate%“'和'移动”%Path1%\%FName%。*“”%Path1%\%FoldOne%\%FoldTwo%\% RDATE%\“'。要获得任何命令的帮助,请打开命令提示符窗口并使用'/?'作为参数运行命令。用'set /?'和'for /?'试试看,并阅读输出帮助页。 – Mofi

+0

@Magoo感谢您的回复,Rdate是在系统日期设置的文件夹名称。 Temp和Testing只是用来解释需求。 我的基础要求是我需要匹配文件夹名称的子字符串与文件名的子字符串,然后将该文件移动到各自的文件夹中,我也会有这样的多个文件,并希望将它们全部移动。 – Ashu

回答

0

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

但是,我编写了两种可能的解决方案,使用部分不同的方法进行相同的操作。

第一完整批次代码:

@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 

对于理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并完全读取显示每个命令的所有帮助页面非常谨慎。

  • call /?
  • cd /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • mkdir /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

阅读也是微软的文章关于Using Command Redirection Operators2>nul>nul的解释和对问题Single line with multiple commands using Windows batch file回答经营者的意义在Windows命令行上运行。

相关问题