I'm trying to make a .bat to sort a large amount of files labeled "(date) (client) - (document Title).pdf" eg "12-21-11 Thompson - Case Management.pdf" into folders titled by client. So far I've gotten:

for /f "tokens=2* delims= " %%b in ('dir /b *.pdf') do (
    md %%b

so that %%b is the client name eg "Thompson" and its respective folder is made, but I'm not sure what to do with move to make it only consider %%b. The titles after "-" aren't consistent for each file, so something like move "%%a %%b %%c %%d" doesn't always work. Is there any way to do this?

Or, if it's easier, is there a way to only consider the 10th character when sorting files? So "12-21-11 Thompson - Case Management" could be sorted into a folder named "T"

有帮助吗?

解决方案

Not tested:

 for /f "tokens=2* delims= " %%b in ('dir /b *.pdf') do (
        md %%b > nul 2>&1
        for /f %%F in ('dir /b /a:-d *%%b*.pdf') do (
           move %%F %%b
        )

    )

其他提示

Use a simple FOR loop to list the files, and an inner FOR /F loop to parse out the client name.

for %%F in (*.pdf) do for /f "tokens=2" %%A in ("%%F") do (
  if not exist "%%A\" md "%%A"
  move "%%F" "%%A"
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top