Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 familar with using Windows for shell scripts, but UNIX style shell is not available at work.

share|improve this question
up vote 5 down vote accepted

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
share|improve this answer
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 :) – Matrix Mole Jul 6 '11 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 '11 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 '11 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 – Hortinstein Jul 6 '11 at 5:00
i will check back in the morning, thanks for the assistance! – Hortinstein Jul 6 '11 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.

share|improve this answer

Or just use that creates a single folder with your selected (explorer) files, or a folder per file, without the extension, and moves the files inside.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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