The code:

for %%X in ("C:\Info\AUGUST 2011\Checklists\DET1__*") do (echo %%X)

lists the name of the files that I would like to:

  1. Create a folder with the rest of the filename (after DET1__) as the title of the folder
  2. Move that file into that specific folder

I know it shouldn't be that hard, but I am really not familiar with using Windows for shell scripting, and I don't have access to a UNIX-style shell at work.

3 Answers 3


This is about a hundred times easier in PowerShell, but this should work. Better explanations of what's going on can be found in setlocal /?, set /? and for /?. Batch isn't a good language, so we have to do things like using ENABLEDELAYEDEXPANSION and the use !FILE! instead of %FILE% to prevent the batch file from setting the varibales once and never updating it. One key to remember is that filenames and foldernames cannot be the same. So you can't have both a file and a folder foo in C:\, for example.

I have not tested these really, so please do so on your own before doing anything.

FOR %%X IN ("C:\Info\AUGUST 2011\Checklists\DET1__*") DO (
    SET TEMPFILE=%~fX.tmp

PowerShell verion:

$Files = Get-ChildItem -Path 'C:\Info\AUGUST 2011\Checklists\' -Filter 'DET1__*'
$Files | ForEach-Object {
    $FileFullName = $_.FullName
    $TempFileName = "$($FileFullName).tmp"
    $DestinationFileName = "$FileFullName\$($_.Name)"
    Move-Item $FileFullName $TempFileName
    New-Item -Path $FileFullName -ItemType Directory
    Move-Item $TempFileName $DestinationFileName
  • Looks like you forgot to extract the DET1__ part off the file name before creating the directory (was going to ask why the creation of the temp file until I realized that you were going for a directory identically named after the file). +1 for the batch code that I could never have come up with :) Jul 6, 2011 at 4:43
  • @matrix Ah, I missed that part of the original question. Let me update it. Have to use environment substitution as well, I suspect.
    – Bacon Bits
    Jul 6, 2011 at 4:49
  • Let me update it in the morning if I remember... just saw the time. Have to use environment substitution (like SET FILENAME=!FILENAME:~7!) as well, I suspect (see set /?). Your PowerShell solution is better, however, @Matrix.
    – Bacon Bits
    Jul 6, 2011 at 4:56
  • I get this error: The following usage of the path operator in batch-parameter substitution is invalid: %~fx. Unfortunately Powershell is disabled by local and group policy, so stuck with BAT Jul 6, 2011 at 5:00
  • i will check back in the morning, thanks for the assistance! Jul 6, 2011 at 5:24

If you're willing to use powershell instead of batch scripting, you can do the following:

$files = get-childitem "C:\Info\AUGUST 2011\Checklists\DET1__*"
ForEach ($file in $files) {
    $dirname = $file.fullname.replace("DET1__","")
    New-Item -Path $dirname -type directory
    Move-Item $file $dirname

Depending on which version of Windows, you may have powershell installed by default (Win7/Vista). If you have XP, then you'll have to download it from the Windows Management Framework Core link.


Or just use http://skwire.dcmembers.com/wb/pages/software/files-2-folder.php that creates a single folder with your selected (explorer) files, or a folder per file, without the extension, and moves the files inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.