Below is the new version; these are the changes:
1- The problem with names that begin with numbers is solved. Remember that
this version is just the second one on the number problem, so it is no time to give up yet! The trick is to extract the string placed between the number and the name; the new version use space, dash and dot characters to assemble this separator. If you find a new case with another character, just insert it between quotes in the FOR command at line #18. The only case that can not be managed this way is when there is
NOT a separator between the number and the name, like in "1example.doc". However, if you have many cases like this one, an additional small program can solve it.
2- The problem with names that include exclamation marks is solved.
3- I devised an entirely new method to create the list of file names that should solve all your problems. The new method uses a two-phases approach: in the first phase each word of the filename is extracted and the rest is used as wild-card for filenames search; this should find the right file if the original name have errors in one word, it has one additional word (like "The" at beginning) or several missed words. If this phase don't found the desired file, the second phase use each word of the given filename as wild-card for the search. This guarantee that the right file will be found if at least one word of its name is correctly written!
In the new version all the names of not found files are reported in the log file with the name of the copied file (or "FILE NOT COPIED" message). If is there just one file found in the list, it is not automatically copied like before (this feature can be reinserted later, if you wish).
I think this new version will solve all your problems so the two-stage execution you requested is no longer needed. Test the program and report the results!
Code: Select all
@echo off
if "%~1" equ "While" goto %2
setlocal EnableDelayedExpansion
set While=for /L %%a in () do if
set Do=(
set WEnd=) else exit
set RunWhile=cmd /V:ON /Q /C "%~F0" While
cd /D "%~DP1"
set newFolder=%~N1
if not exist "%newFolder%" ECHO md "%newFolder%"
if exist "%newFolder%.log" del "%newFolder%.log"
set cutNumber=
set /P firstLine=< "%~NX1"
if "%firstLine:~0,1%" neq "1" goto start
set n=0
:nextChar
set /A n+=1
for %%a in (" " "-" ".") do if "!firstLine:~%n%,1!" equ %%a goto nextChar
set /A n-=1
set cutNumber=!firstLine:~1,%n%!
:start
setlocal DisableDelayedExpansion
for /F "usebackq delims=" %%a in ("%~NX1") do (
set "fileName=%%~Na"
setlocal EnableDelayedExpansion
if defined cutNumber (
set "fileName=!fileName:*%cutNumber%=!"
)
if exist "!fileName!.doc" (
ECHO copy "!fileName!.doc" "%newFolder%"
echo File "!fileName!.doc" copied
) else (
echo/
echo FILE NOT FOUND: "!fileName!.doc"
echo/>> "%newFolder%.log"
echo File not found: "!fileName!.doc">> "%newFolder%.log"
%RunWhile% ChooseName Phase1
if errorlevel 1 (
set /P name=< nameChoosen
ECHO copy "!name!" "%newFolder%"
echo File "!name!" copied
echo File "!name!" copied>> "%newFolder%.log"
) else (
%RunWhile% ChooseName Phase2
if errorlevel 1 (
set /P name=< nameChoosen
ECHO copy "!name!" "%newFolder%"
echo File "!name!" copied
echo File "!name!" copied>> "%newFolder%.log"
) else (
echo FILE NOT COPIED
echo FILE NOT COPIED>> "%newFolder%.log"
)
)
)
endlocal
)
if exist "%newFolder%.log" (
echo/
echo/
echo CONTENTS OF LOG FILE:
type "%newFolder%.log"
del nameChoosen
)
PAUSE
goto :EOF
:ChooseName
set nWord=0
for %%a in (!fileName!) do (
set /A nWord+=1
set word[!nWord!]=%%a
)
set "originalName= !fileName! "
set iWord=0
set option=
%While% not defined option %Do%
if !iWord! lss !nWord! (
set /A iWord+=1
for %%A in (!iWord!) do (
if %3 equ Phase1 (
for %%B in (!word[%%A]!) do set "wildCard=!originalName: %%B =*!"
) else (
set "wildCard= !word[%%A]! "
)
)
set i=0
for %%f in ("*!wildCard:~1,-1!*.doc") do (
set /A i+=1
echo !i!- %%f
set "name[!i!]=%%f"
)
if !i! gtr 0 (
set /P "option=Enter number (0 cancel this phase), or press Enter to continue searching: "
if defined option (
for %%A in (!option!) do echo !name[%%A]!> nameChoosen
) else (
echo/
echo CONTINUE SEARCHING: "!fileName!.doc"
)
)
) else (
set option=0
)
%WEnd% !option!
exit /B
Antonio