0

I need to move few files from one folder to sub folder. My folder structure is already ready.

File current folder: D:\AB\*.*
The file name is:   SS-AA-Report-Temp File for Script Testing-Daily-31March.txt
Destination folder: D:\AB\Pm 1.1 File For Script\Daily\

How to check file name substring with folder name substring and move?

Note I have multiple files like this.

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%\

Edit:
File names format:

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    

Now my folder structure is:

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    

And here is the batch file I have already:

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
Mofi
  • 46,139
  • 17
  • 80
  • 143
Ashu
  • 37
  • 3
  • 9
  • Where is RDate being set up? You are aware that `fold1` will contain a leading space? Where does `p1.1` come into the picture?Do you wish to remove the `Temp` and `Testing` strings - are these the only strings, and will they always appear? – Magoo Apr 03 '17 at 04:39
  • 1
    First mistake, the space after equal sign in `set Path1= C:\Main` which is not omitted by Windows command interpreter on assigning the string to the environment variable. Remove this space character. Second mistake, the entire directory/file string must be enclosed in double quotes and not parts of it. So use `mkdir "%Path1%\%FoldOne%\%FoldTwo%\%RDate%"` and `move "%Path1%\%FName%.*" "%Path1%\%FoldOne%\%FoldTwo%\%RDate%\"`. To get help on any command open a command prompt window and run the command with `/?` as parameter. Try it out with `set /?` and `for /?` and read output help pages. – Mofi Apr 03 '17 at 05:56
  • @Magoo Thanks for reply, Rdate is folder name set on system date. Temp and Testing is just used to explain requirement. My basis rerquirement is I need to match substring of folder name with substring of File name and then move that file in respective folder, also I will have multiple files like this and want to move all of them. – Ashu Apr 03 '17 at 10:05
  • @Mofi I tried to enter entire directory file name in double quotes but it was not working so did that purposely. but as told my requirement is to match substring on both end as explained on above comments. – Ashu Apr 03 '17 at 10:06
  • @Ashu, the code you've posted seems to be a poorly altered version of the two answers I've previously submitted for you. In fact, it appears that your current script should be exactly the same as the accepted answer I gave you in your last question, _(other than the changed location of `%Path1%`)_. Unfortunately this time you have not provided sufficient information for us to determine your task/goal. Please explain properly your file and directory naming structures and what you are trying to determine as string matches between the two. – Compo Apr 03 '17 at 14:39
  • @Compo, correct You answer was very usefull, but now my requirement got slightly changed. after editing a bit your last answers my requirement was fulfilled as expected, but here its bit changed now. – Ashu Apr 04 '17 at 05:01
  • Unfortunately your task is still too vague. You have to at least provide a few example file names with their matching folder names. If you already have the folder structure in place then there should be no reason not to. – Compo Apr 04 '17 at 06:08
  • @Mofi as I explained I want to match string "File for Script" and find folder having same string and move file in that folder. so here folder is "P1.1 File For Script". In my code it is not matching substring rather moves only if whole Name after delims "-" matches. I have added whole script above with example. Need to modify script for substring matching. Note- there is no fix format for substring length and pattern on both side – Ashu Apr 05 '17 at 06:21

1 Answers1

0

The logic for matching file name substrings with folder name is still very fuzzy.

However, I coded two possible solutions doing both the same using partly different methods.

The first complete batch code:

@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

The second batch code differs from first solution only on how subroutine MoveToFolder is coded for finding the corresponding folder for current file name. For that reason just the code of the subroutine is posted below.

: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

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

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

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and >nul and answer on question Single line with multiple commands using Windows batch file for meaning of operator & on Windows command lines.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143