Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:59:40+00:00 2026-06-08T05:59:40+00:00

I have a batch file here: StackOverflows code window doesn’t show anything when I

  • 0

I have a batch file here:
StackOverflows “code” window doesn’t show anything when I press it so hopefully it doesn’t butcher it:

@echo off
echo Moving files to respective folders.
DIR C:\ArcConverted\adam\shapefiles\ /A:-D /B > C:\ArcConverted\adam\filelist.txt
echo File start > C:\ArcConverted\adam\output.txt
FOR /f %%a IN (C:\ArcConverted\adam\filelist.txt) DO (
    FOR /f "tokens=1 delims=_" %%b IN ('%%a') do echo A:%%a B: %%b
    IF NOT EXIST C:\ArcConverted\adam\shapefiles\%%~na\ mkdir C:\ArcConverted\adam\shapefiles\%%~na\
    echo Moving file %%a into C:\ArcConverted\adam\shapefiles\%%~na\
    move C:\ArcConverted\adam\shapefiles\%%a C:\ArcConverted\adam\shapefiles\%%~na\%%b
)
pause

I have files in C:\ArcConverted\adam\shapefiles that are newly forward-converted ArcGIS shapefiles, and their filenames are now:

  • thisthing_arc.adf
  • thisthing_tic.adf
  • thisthing_shp.shp
  • thatthing_shp.shp

… and so on.

I have 83 of these different object sets, and each has about 16 files. I am trying to make a batch script (and yes, it has to be batch) that reads all the files in the folder, puts it in a file, then reads the list of files and and creates folders for each of them if you take out everything past the _ (the _ is added to it from the converter program).
My script does not work though. The second for loop opens each file and reads everything and prints out hundreds of thousands of lines. I can’t seem to figure out how to do string delimiting on file names using batch (I don’t care about whats in the file).

Summary:

I have file sets that all belong in the same folder thrown into the same folder. I am trying to progmatically (through batch) get the name of the set from the file name (everything that occurs before the _), make that folder, then move the file into that folder. Any files that also belong in that folder will get moved there too as the loop advances.

Am I heading in the right direction?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:59:43+00:00Added an answer on June 8, 2026 at 5:59 am

    You were definitely on a good track, but you have both some syntactical errors as well as some logic errors.

    In your 2nd FOR loop you are trying to parse a string, but your code is executing a command instead because your IN clause is enclosed in single quotes. You want double quotes to signify a string.

    Your whole logic is off starting with your 2nd FOR loop. The string you want to parse is the name of the file, without extension. So your IN clause should be IN ("%%~na"). Then after that loop you try to create folders named "%%~na" and move the file to that folder with a new file name %%b. But %%b is no longer defined because it is not in your 2nd loop. Actually you want to create folders named "%%b" and move the file there all within the 2nd loop.

    Lastly you have some issues with your FOR /F options. The 1st FOR will break the name at the 1st space character, which is not what you want. Of course if you never have spaces in your file names then it is a non-issue. But why take the chance? Also the default EOL option of ; will ignore any file names that begin with ;. Again unlikely, but why take the chance. File names cannot contain :, so that is a good character for EOL.

    I believe the following code is what you were looking for.

    @echo off
    echo Moving files to respective folders.
    dir C:\ArcConverted\adam\shapefiles\ /A:-D /B > C:\ArcConverted\adam\filelist.txt
    echo File start > C:\ArcConverted\adam\output.txt
    for /f "eol=: delims=" %%a in (C:\ArcConverted\adam\filelist.txt) do (
      for /f "eol=_ tokens=1 delims=_" %%b in ("%%a") do (
        echo A:%%a B:%%b
        if not exist "C:\ArcConverted\adam\shapefiles\%%b\" mkdir "C:\ArcConverted\adam\shapefiles\%%b\"
        echo Moving file "%%a" into "C:\ArcConverted\adam\shapefiles\%%b\"
        move "C:\ArcConverted\adam\shapefiles\%%a" "C:\ArcConverted\adam\shapefiles\%%b\"
      )
    )
    pause
    

    However, I would simplify the code. I don’t see a need for a filelist.txt file – you can use a simple FOR loop instead of writing the file and reading it with FOR /F. The code is also made much smaller and simpler if you simply change your directory (pushd) to your shapefiles root.

    One last thing is to put a filter on the files you attempt to move. I don’t think you want to create folders using the whole name if the file name does not contain a "_" character. So my modified code explicitly looks for "_" in the name.

    @echo off
    setlocal
    pushd "C:\ArcConverted\adam\shapefiles" || exit /b
    echo Moving files to respective folders.
    for %%A in ("*_*.*") do (
      for /f "eol=_ tokens=1 delims=_" %%B in ("%%~nA") do (
        if not exist "%%B\" mkdir "%%B\"
        echo Moving file "%%A" into "%%B\"
        move "%%A" "%%B\"
      )
    )
    pause
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I have 2 requirements: Need a batch file to start a process on
I have a batch file to shutdown the PC @echo off Shutdown /s /f
I have a batch file with the following code: for /f tokens=* %%a in
Here's a minimal batch file, demo.bat , to illustrate my problem: @ECHO off set
I have a simple batch file here which will print a word document from
I have this batch file @ECHO OFF ECHO Please Enter Path of the View,
I have a batch file that requires the user to enter a file path.
I have a batch file with a FOR loop. In the loop I must
I have a batch file that runs several python scripts that do table modifications.
I have a batch file that installs WinVNC in about 1 second and starts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.