1

I'm new to batch and only have a very basic understanding of what to do.

I have hundreds of files that I would like to sort into folders based on the name. An example of the filename would be:

346479_2009-01-01_2009-12-31_Distribution_Report

For example, there is 2 Distribution Reports (one pdf, on csv) for 2009 through 2014, I want to move (or copy, it doesn't matter) all the Distribution Reports into one folder, labeled Distribution Report.

If someone could help I would really appreciate it!

I was working off this post but not having much luck... this was all I got, and I don't think it is at all correct:

    @ECHO OFF
    SETLOCAL
    SET "sourcedir=C:\Users\emcaleer\Desktop\New folder"
    PUSHD %sourcedir%
    FOR /f "tokens=1*" %%a IN (
     'dir /b /a-d "*_*_*-*-*-*_*-*-*-*_"'
     ) DO (  
     MD %%a
     MOVE "%%a %%b" .\%%a\
    )
    POPD
    GOTO :EOF
Community
  • 1
  • 1
emcaleer
  • 11
  • 3
  • Please show what you have done so far. – Rick Smith Dec 01 '15 at 18:36
  • I was working off this post - http://stackoverflow.com/questions/19992530/batch-create-folders-based-on-part-of-file-name-and-move-files-into-that-folder but not having much luck... this was all I got, and I don't think it is at all correct: @ECHO OFF SETLOCAL SET "sourcedir=C:\Users\emcaleer\Desktop\New folder" PUSHD %sourcedir% FOR /f "tokens=1*" %%a IN ( 'dir /b /a-d "*_*_*-*-*-*_*-*-*-*_"' ) DO ( MD %%a MOVE "%%a %%b" .\%%a\ ) POPD GOTO :EOF – emcaleer Dec 01 '15 at 18:39
  • Go ahead and copy the exact code you are using into this question along with what the code is doing. This makes it easier for people to see what you are doing, what is happening, and how they can help. – Rick Smith Dec 01 '15 at 18:42
  • Edit your question to include the code, otherwise it gets crazy hard to read. – Rick Smith Dec 01 '15 at 18:43
  • 1
    If it were me, I'd look at doing this in powershell.. something like.. get-childitem -path c:\somefolder -filter "`*Distribution`*" | move-item -destination c:\distributionreports – Chris N Dec 01 '15 at 18:43
  • 2
    Normally I'd do what @ChrisN is doing and push for PowerShell, but you can do your example copy in a command prompt without any big scripting or looping. Just `mkdir "Distribution Reports"` and then `"copy *Distribution_Report* "Distribution Reports"`. Have you tried a simple pattern match like this, or are your examples more complicated and it won't work for some? In fact, you could search for "Distribution_Report" in Windows Explorer and click and drag them into the new folder... is it instead that you want to take the last part of each filename and make new folders, for lots of names? – TessellatingHeckler Dec 01 '15 at 18:50
  • I'm open to trying powershell, I'm not attached to what I was working on, it was the latest attempt in a long string of attempts. This is accurate: "is it instead that you want to take the last part of each filename and make new folders, for lots of names?" – emcaleer Dec 01 '15 at 18:55
  • Makes more sense; didn't think of that until after I'd written the rest. Sooo in *"346479_2009-01-01_2009-12-31_Distribution_Report"*, if you cut it where the dashes `-` are and took the last bit you would get `31_Distribution_Report`, and if you cut it where the underscores are `_` and took the last bit you would get `Report`. Is there a fixed pattern where the name is? Like, the last two parts with underscores, or the last bit after any numbers? – TessellatingHeckler Dec 01 '15 at 18:57
  • The fixed pattern is mostly in the beginning ... the titles of the reports all have the underscores between words rather than spaces, but they could be any number of words - here are some examples: 346479_2014-01-01_2014-12-31_Summary_of_Fees_and_Compensation, 346478_2014-01-01_2014-12-31_Schedule_D_Filing_Summary, 346478_2014-01-01_2014-12-31_Participant_Contribution_Investment_Allocation_Report – emcaleer Dec 01 '15 at 19:02
  • 2
    to claryfy: they all are `___different_numbers_of_words.`? – Stephan Dec 01 '15 at 19:13

1 Answers1

1

for Syntax can be a bit confusing for beginners. You specified Tokens, but no delimiter. Because there are None of the Default delimiters in the string, the second token never got any value. Try this:

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%i in ('dir /b /a-d *_*_*_*.*') do (
  set file=%%~ni
  set folder=!file:*-31_=!
  md !folder! 2>nul
  move "%%i" !folder!
)
Stephan
  • 53,940
  • 10
  • 58
  • 91